Skip to content Skip to sidebar Skip to footer

Trying To Segment Characters Using Opencv - Contour Problem

My code is detecting another box when there is no letter! # Create sort_contours() function to grab the contour of each digit from left to right def sort_contours(cnts,reverse = Fa

Solution 1:

Using morphological reconstruction, you can easily remove elements that touch the edges from a binary image. This is similar to the imclearborder() function from Matlab / Octave.

import cv2
import numpy as np

img = cv2.imread('CpO0b.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)[1]
marker=thresh.copy()
marker[1:-1,1:-1]=0
kernel = np.ones((3,3),np.uint8)

while True:
    tmp=marker.copy()
    marker=cv2.dilate(marker, kernel)
    marker=cv2.min(thresh, marker)
    difference = cv2.subtract(marker, tmp)
    if cv2.countNonZero(difference) == 0:
        break
mask=cv2.bitwise_not(marker)
out=cv2.bitwise_and(thresh, mask)
cv2.imwrite('out.png', out)
cv2.imshow('result', out )

enter image description here

Solution 2:

When segmenting characters by binarisation, finding alien blobs is the rule rather than the exception.

You can get rid of many of them by simple size/area/aspect ratio… criteria. In the given case, you can use the fact that they go alongside the edges of the image, or that they are shifted up wrt the other characters.

Anyway, there can be blobs that resist to any form of by-feature rejection just for the reason that they look like characters.

The next level of filtering is recognition proper, which will tell you if the blob has the shape of a known character (this is font dependent).

Last but not least, you can even have blobs taking the appearance of legit characters and being in-line with the string. Then there is nothing you can do.

Post a Comment for "Trying To Segment Characters Using Opencv - Contour Problem"