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?
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.
Use the findContours() function to detect the contours in the image.
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)
and after applying cv2.findContours
the result will be like this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With