findcontours导致程序崩溃

cv::findcontours causes a program crash

本文关键字:崩溃 程序 findcontours      更新时间:2023-10-16

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

 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错误:不支持的格式或格式组合([Start]FindContours只支持8uC1和32sC1图像)在未知功能,文件......srcopencV modulesimgprocsrccontours.cpp,第196行

@Niko谢谢你的帮助,我想我必须将helpframe转换为另一种类型。当我使用结果时,我得到forHelpframe.type () => 5而对于frame,我得到0
我不知道这是什么意思?但是我会试着找到一个方法来转换helpframe。

将helpframe转换为:helpframe.convertTo (helpframe2 CV_8U)

我没有得到任何帮助frame2是= 0 ??当我尝试相同的框架,而不是结果框架的转换工作??

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

在您可以识别轮廓之前,您需要将图像缩减为二值图像。这可以通过应用某种边缘检测算法或通过简单的阈值处理来实现:

// 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, ...);