How to properly use cv2.findContours() on opencv version 4.4.0.?

In Python/OpenCV 4.4.0, findContours returns only 2 values, you list 3.

You show:

image,contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

OpenCV 4.4.0, lists:

contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])

Please always check the documentation. See

https://docs.opencv.org/4.4.0/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0

One way to handle this in a version independent way, if all you want are the contours, is (credit to @nathancy):

contours = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]

If you do not want all nested contours, then use RETR_EXTERNAL and not RETR_LIST.

Leave a Comment