首页 > 解决方案 > 在opencv中使用findContours时出错

问题描述

我想使用 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));

但我得到以下异常:

OpenCV 错误:断言失败 (mtype == type0 || (((((mtype) & ((512 - 1) << 3)) >> 3) + 1) == ((((type0) & ((512 - 1) << 3)) >> 3) + 1) && ((1 << type0) & fixedDepthMask) != 0)) 在创建文件/home/nvidia/opencv/modules/core/src/matrix。 cpp,第 2578 行终止在抛出“cv::Exception”实例后调用什么():/home/nvidia/opencv/modules/core/src/matrix.cpp:2578:错误:(-215)mtype == type0 || (((((mtype) & ((512 - 1) << 3)) >> 3) + 1) == ((((type0) & ((512 - 1) << 3)) >> 3) + 1) && ((1 << type0) & fixedDepthMask) != 0) 在函数创建中。

用于查找轮廓的图像:

在此处输入图像描述

.任何有关如何解决此问题的指示将不胜感激。谢谢。

标签: c++opencvmatopencv-contour

解决方案


我运行了您的代码并发现了问题。您正在使用 acv::Point2f来存储轮廓,而您应该使用cv::Point. 只需将第 4 行编辑为:

vector<vector<Point>> contours

推荐阅读