c++ - cv::findcontours 导致程序崩溃

标签 c++ opencv

我正在尝试从我的框架中获取轮廓,这就是我所做的:

 cv::Mat frame,helpframe,yframe,frame32f;
 Videocapture cap(1); 
.......
while(waitkey()){
cv::Mat result = cv::Mat::zeros(height,width,CV_32FC3);
cap >> frame; // get a new frame from camera
frame.convertTo(frame32f,CV_32FC3);
for ( int w = 0; w< 10;w ++){
            result += frame32f;
        }   
         //Average the frame values.
        result *= (1.0/10);
        result /= 255.0f;
cvtColor(result,yframe,CV_RGB2YCrCb); // converting to Ycbcr///// the code work fine when I use frame instead of result 
extractChannel(yframe,helpframe,0); //extracting the Y channel
cv::minMaxLoc(helpframe,&minValue,&maxValue,&min,&max);
std::vector<std::vector<cv::Point>> contours;
cv::findContours(helpframe, contours,CV_RETR_LIST /*CV_RETR_EXTERNAL*/, CV_CHAIN_APPROX_SIMPLE);

................................................ .....

程序在 findcontours 处崩溃,我调试时收到此错误消息:

OpenCV Error: Unsupported format or combination of formats ([Start]FindContours support only 8uC1 and 32sC1 images) in unknown function, file ......\src\openc v\modules\imgproc\src\contours.cpp, line 196

@Niko 感谢您的帮助我认为我必须将 helpframe 转换为另一种类型。 当我使用 result 我得到 helpframe.type() => 5 和 frame 我得到 0
我不知道这是什么意思?但我会尝试找到一种方法来转换 helpframe。

在转换 helpframe 之后: helpframe.convertTo(helpframe2,CV_8U)

我什么也没得到 helpframe2 is = 0 ??当我尝试使用 frame 而不是 resultframe 时,转换有效??

知道我应该如何更改 helpframe 类型,因为我使用的是结果而不是框架吗?

最佳答案

您需要先将图像缩小为二值图像,然后才能识别轮廓。这可以通过例如应用某种边缘检测算法或通过简单的阈值处理来完成:

// Binarize the image
cv::threshold(helpframe, helpframe, 50, 255, CV_THRESH_BINARY);

// Convert from 32F to 8U
cv::Mat helpframe2;
helpframe.convertTo(helpframe2, CV_8U);

cv::findContours(helpframe2, ...);

关于c++ - cv::findcontours 导致程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14213526/

相关文章:

python - 如何在python中为linux设置复杂的环境变量?

c++ - 在 OpenCV 上使用 GPU 时如何确定线程数?

c++ - 为什么我的函数返回一个空字符串?

c++ - 存在循环依赖时 #include 的正确方法?

c++ - 如何在 C++ 屏幕上打印下标/上标?

c++ - Boost python包装了一个虚拟方法

c++ - 从图像中减去区域并保留边界

c++ - Qt Creator 找不到 openCV 库

c++ - C/ObjC - 参数大小。使用指针与值

opencv - 检测颜色光环。这对分类器而言是一项好工作,还是我应该使用图像处理技术?