Skip to content Skip to sidebar Skip to footer

Opencv3.3.0 Findcontours Error

I reinstall opencv today, and run my code I've written before. I got the error: OpenCV Error: Assertion failed (_contours.empty() || (_contours.channels() == 2 && _contour

Solution 1:

  1. What is (2,2)? The fourth positional argument for findContours() is the output contours array. But you're not passing it a valid format for the contours array (which is an array of points). If it's supposed to be the offset and you don't want to provide the additional positional arguments, you need to call it by keyword like offset=(2,2). This is the reason for the actual error. I'm not sure why this worked in the previous version, as it accepted the same arguments in the same order, and Python has been like this forever; if the arguments are optional, you need to provide enough positional arguments up to the argument, or provide it a keyword.

  2. findContours() returns three values in OpenCV 3 (in OpenCV 2 it was just two values), contours is the second return value; should be

    _, contours, _ = findContours(...) 
    

    Also you don't have to wrap into a tuple in python for assignment, you can just do x, y, z = fun(), no need to do (x, y, z) = fun(). Additionally you could just ask for the second return value by indexing the result like

    contours = cv2.findContours(...)[1]
    

So this should clear you up:

cnts = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE, offset=(2,2))[1]

These docs for OpenCV 3 have the Python syntax, so you can browse there if any of your other prior code breaks, and see if the syntax has changed.

Post a Comment for "Opencv3.3.0 Findcontours Error"