Ask Your Question

Revision history [back]

Python findContours and contourArea

I'm writing a motion triggered camera system on a Raspberry Pi, using Python and OpenCV 3.

The following command seems to work:

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

because sometimes I can draw those contours using:

cv2.drawContours(frame0, c, -1, (0,255,0), 3)

But it only works reliably if I enclose the drawContours statement in a try except structure. Furthermore, when I try this:

area = if cv2.contourArea(c)

It never works, and always throws an exception.

  • Am I doing something wrong?
  • Is there some way to detect if a contour returned by findContours is a valid one, and won't cause exceptions to be thrown?