0

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.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
himanshu sangal
  • 41
  • 1
  • 11
  • see https://stackoverflow.com/a/43283990/5294258 – sturkmen Sep 14 '17 at 18:31
  • @sturkmen thanks for your comment....same Problem I noticed there....its find the contour in the foreground not considering the contours where background color is changing....... – himanshu sangal Sep 14 '17 at 18:33
  • Sorry, i did not read your question carefully.could you show desired result on an image. – sturkmen Sep 14 '17 at 18:40
  • https://imgur.com/a/ECX3N Please find the link of desired results. – himanshu sangal Sep 14 '17 at 19:11
  • Not that you couldn't do this task with image processing and vision but wouldn't it be far easier to actually parse the HTML to get what you want? Problem with most image processing approaches to a problem like this would be color variation among templates like these. For a page like this, it seems the three most common colors will be the background color, so you could use histograms to select the three most common colors and create contours around each one individually if you did want to use pure image processing. – alkasm Sep 14 '17 at 20:36
  • @AlexanderReynolds thanks for your comment.....the requirement is like I have to do with the image only not with the HTML......I thought about the histograms but the problem is like in some image, there is only one common colour in some we have...its not fixed....Can you suggest some other approach ? – himanshu sangal Sep 15 '17 at 05:26
  • You'll need to add some more examples to illustrate the case, otherwise it's hard to suggest without knowing what you could encounter. – alkasm Sep 15 '17 at 06:42

1 Answers1

1

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] )
himanshu sangal
  • 41
  • 1
  • 11