Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding contours of a two-part letter

Suppose that I have an image of letters and I want to find the region of those letters.

I have wrote this code:

MIN_CONTOUR_AREA = 10   
img = cv2.imread("alphabets.png")     
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)    
blured = cv2.blur(gray, (5,5), 0)    
img_thresh = cv2.adaptiveThreshold(blured, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
imgContours, Contours, Hierarchy = cv2.findContours(img_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in Contours:
    if cv2.contourArea(contour) > MIN_CONTOUR_AREA:
        [X, Y, W, H] = cv2.boundingRect(contour)
        cv2.rectangle(img, (X, Y), (X + W, Y + H), (0,0,255), 2)
cv2.imshow('contour', img)

But the code above has this output: result

What can I do to find contour for letters that are not continuous like 'i' or Arabic letters?

like image 863
M.Mehranian Avatar asked May 04 '18 21:05

M.Mehranian


People also ask

How do you find contours?

To draw the contours, cv. drawContours function is used. It can also be used to draw any shape provided you have its boundary points. Its first argument is source image, second argument is the contours which should be passed as a Python list, third argument is index of contours (useful when drawing individual contour.

How do you find contours in C++?

Use the findContours() function to detect the contours in the image.


1 Answers

Before finding the contours, you can use some segmentation methods:

rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 10))
threshed = cv2.morphologyEx(img_thresh, cv2.MORPH_CLOSE, rect_kernel)

enter image description here

and after applying cv2.findContours the result will be like this:

enter image description here

like image 183
Salman Avatar answered Oct 11 '22 11:10

Salman