1

I am working on an image to find contours, I am successfully able to identify almost all contours in the below image. But I am not able to find certain contours like the two grey boxes on the left side. I have tried all the contour methods like Tree, List, External and also tried with thresholding.

Please suggest If I am missing something or can do to improve it

img = cv2.imread(input_image, 0)

kernel = np.ones((5, 5),np.uint8)
morphological_img = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)

canny_img = cv2.Canny(morphological_img, 200, 300)
input_imag, contours, hierarchy = 
cv2.findContours(canny_img,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # get contours
3
  • When processing an image the crucial part is the processing, in your case you are doing with threshold, but if the thresholds are deficient you will never be able to find the solution, I recommend showing the image thresh. Commented Sep 5, 2017 at 19:05
  • A better way would be to divide image into segments. The left part of the image may need different type of preprocessing before detecting contours. Commented Sep 5, 2017 at 19:06
  • @eyllanesc I have edited my question, please check. I am using kernel and Morph gradient and canny instead of threshold for this purpose Commented Sep 5, 2017 at 19:08

1 Answer 1

2

Your question does not show or link to the image that it refers to. But here are the steps to improve this:

  1. Unless your image is already binary, the standard method is to apply thresholding first, and then use morphological operations.

  2. If you are interested in only the contours, you can first do canny edge detection and then use morphological operations to enhance the contour that you want to detect.

  3. If canny_img does not show the edge corresponding to the desired contour (try cv2.imshow), findContours want be of much use, if any.

If you can edit the answer to include the image, we will be able to provide more specific answers.

Sign up to request clarification or add additional context in comments.

3 Comments

In this case, I don't seeany need to do the morphological operation. What happens if you skip that and perform Canny on img directly? You can also take cv2.HoughLines to detect straight lines around these boxes.
Thanks @Toroto will try this....I want to give u +1 but this is telling me that I need +15 reputations to do this.
Thanks, don't worry about that. I am glad that I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.