Reputation: 41
I am working on this image:
I used the following code:
input_img = cv2.imread(input_image)
img = cv2.imread(input_image, 0)
kernel = np.ones((5,5),np.uint8)
# morphological_img = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
# # morphological_img = cv2.threshold(morphological_img, morphological_img, 128, 255, cv2.THRESH_BINARY_INV)
# # img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
# morphological_img = cv2.medianBlur(morphological_img, 5)
canny_img = cv2.Canny(input_img, 100, 200)
_, contours, hierarchy = cv2.findContours(canny_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # get contours
and got these contours:
I have tried all these features like blurring, thresholding, etc. but I'm not getting the contours I am expecting. I need to find the black contour and a violet contour like containers but what I am getting contours around the text instead of their background container.
Sorry for my bad english. If you need anything else, please ask.
Upvotes: 0
Views: 598
Reputation: 41
I am answering my own question after researching a lot thinking that it can help someone who is stuck at same problem.
As the question is here...we are not able to find background containers but with all the present techniques(like thresholding, blurring etc)...I was not able to get.
So the approach is to add borders(size=10 or more will work) and choose the colour which is not present in the image will give you all the contours you need.
bordersize = 10
img = cv2.copyMakeBorder(img, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType= cv2.BORDER_CONSTANT, value=[247, 248, 188] )
Upvotes: 1