Search code examples
c++opencvcontourmat

Error while using findContours in opencv


I wanted to process a black and white image using findContours.

Mat frame= imread(argv[1]);
cvtColor(frame, frame, CV_BGR2GRAY);
threshold(frame,frame, 150, 255, THRESH_BINARY);
vector<vector<Point2f>> contours;
findContours(frame.clone(), contours, noArray(), CV_RETR_EXTERNAL, 
CV_CHAIN_APPROX_SIMPLE , Point(0, 0));

But I am getting the below exception :

OpenCV Error: Assertion failed (mtype == type0 || (((((mtype) & ((512 - 1) << 3)) >> 3) + 1) == ((((type0) & ((512 - 1) << 3)) >> 3) + 1) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /home/nvidia/opencv/modules/core/src/matrix.cpp, line 2578 terminate called after throwing an instance of 'cv::Exception' what(): /home/nvidia/opencv/modules/core/src/matrix.cpp:2578: error: (-215) mtype == type0 || (((((mtype) & ((512 - 1) << 3)) >> 3) + 1) == ((((type0) & ((512 - 1) << 3)) >> 3) + 1) && ((1 << type0) & fixedDepthMask) != 0) in function create.

Image used to findContours:

enter image description here

.Any pointers on how to solve this issue would be highly appreciated. Thanks.


Solution

  • I ran your code and found the problem. You are using a cv::Point2f to store the contours, while you should be using cv::Point. Just simply edit the 4th line to:

    vector<vector<Point>> contours