0

I apologize if I am posting in the wrong location. Please let me know if that is the case. Otherwise, I appreciate any help or direction given.

I have an image of an object against a backdrop that I would like to pre-process to only have the region-of-interest. I've got some preprocessing done such as cropping and contrast-enhancing it, but now I want to set the backdrop to a solid color. I'm using canny to generate the contour line between the edge of the object and background as such:

example image

Now, I am wondering if I can somehow use that long line in the middle as a boundary for a mask that will cover everything below said line. Every example I've seen on this site and elsewhere always uses Canny contours that are closed geometric shapes (such as squares, circles, etc.)

What I have tried to is grab the contour with the largest area (hoping that it would capture either the top or bottom half) as such:

    cnts = cv2.findContours(img_edge_top.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]

        contour = max(cnts, key = cv2.contourArea)
        cv2.drawContours(img_blur_top, [contour], -1, (0, 255, 0), 3)
        cv2.imshow("contour", img_edge_top)
        cv2.waitKey(0)

What I get below is the original image:

enter image description here

A point in the right direction or similar example would be enough. Thank you

  • Do you want to make different color top and bottom of the line correctly? or I misunderstood? – Yunus Temurlenk Jan 30 '20 at 05:22
  • If you had just the line, you could simply [`floodFill`](https://docs.opencv.org/4.2.0/d7/d1b/group__imgproc__misc.html#ga366aae45a6c1289b341d140839f18717) the lower area by setting the seed point somewhere to "y_max". But there's that little artifact in the bottom left corner, right? Finding contours would allow to get the center of that artifact, and then you could also floodfill that area. BUT, have you tried another approach than Canny in the first place? Your original image likely can be simply thresholded to obtain the desired mask. – HansHirse Jan 30 '20 at 05:27
  • @YunusTemurlenk, yes that is correct! – Nikita Albert Jan 30 '20 at 14:24
  • @HansHirse I have not yet tried any approach other than canny. I'm new to python and openCV, so if there is something else I could try let me know! I did try floodFill, but for some reason adjusting the seedPoint did not yield consistent results. I managed to do it by manually drawing a border at the edges to the white line and then using fillPoly to color within. – Nikita Albert Jan 30 '20 at 14:28
  • After canny you get the coordinates of that line. Then for each coordinate point column check the all columns and change pixel values according to reference line points. Like: are they below or above – Yunus Temurlenk Jan 30 '20 at 14:54
  • @YunusTemurlenk, what is the code snippet to get the coordinates of said canny line? – Nikita Albert Jan 30 '20 at 15:05
  • @NikitaAlbert the coordinates of the contour you used while drawing by drawContours – Yunus Temurlenk Jan 31 '20 at 14:54

0 Answers0