0
cnts,_ = cv2.findContours(cv.rectangle(img3.copy(), (0,200) , (5000, 1150), (255,0,0), 2) ,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(rgb_img, cnts, -1, (0,255,0), 2)


conts = imutils.grab_contours(cnts)
c = max(conts, key=cv2.contourArea)

I'm trying to find the contours of two irregular edges in between the image. Else the rectangular actual edge of the image is being detected as contour max area. So i thought of using a rectangular area where I want it to exactly detect the edges.

  • what is the error? you might be using `imutils.grab_contours` wrong – Christoph Rackwitz Mar 09 '22 at 17:08
  • 1
    and why are you drawing a rectangle? – Miki Mar 09 '22 at 17:08
  • 1
    this dude has some nerve... https://github.com/PyImageSearch/imutils/blob/9f740a53bcc2ed7eba2558afed8b4c17fd8a1d4c/imutils/convenience.py#L169 error by users of *his* library, blamed on OpenCV. he doesn't even check that it's actually a tuple. in this case it's not a tuple, it's the list of contours that got passed. – Christoph Rackwitz Mar 09 '22 at 17:09
  • 1
    `cnts = cv2.findContours` `conts = imutils.grab_contours(cnts)` – Miki Mar 09 '22 at 17:11
  • "So i thought of using a rectangular area where I want it to exactly detect the edges." -- this sounds more like you want to get a roi / slice of the image , not draw a rectangle into it – berak Mar 09 '22 at 17:14
  • @miki I'm not trying to draw a rectangle. I want to use only that specific part of the pictures for finding contours. – Starcode1619 Mar 09 '22 at 18:20
  • @berak I'm not trying to draw a rectangle. I want to use only that specific part of the pictures for finding contours. – Starcode1619 Mar 09 '22 at 18:20
  • 3
    @Starcode1619 but you **are** drawing a rectangle. Use numpy slicing instead – Miki Mar 09 '22 at 18:30

1 Answers1

1

You are using imutils.grab_contours wrong.

If you do the destructuring yourself, with (cnts, *_) = cv.findContours(...), then you don't need imutils at all.

If you want to use imutils anyway, you need to pass it the entire tuple (2-tuple or 3-tuple) returned by cv.findContours, like so: cnts = imutils.grab_contours(cv.findContours(...))

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • This worked. Thanks a lot. But I'm not trying to draw a rectangle. I want to use only that specific part of the pictures for finding contours. What function can I use to do this? – Starcode1619 Mar 09 '22 at 18:21
  • I hope that was sufficiently addressed in the comments below the question ("numpy slicing"). – Christoph Rackwitz Mar 09 '22 at 23:30