cv::findcontours导致程序崩溃

3

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

 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图像)在未知函数中,文件......\src\opencv\modules\imgproc\src\contours.cpp, line 196

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

通过以下方式转换helpframe:      helpframe.convertTo(helpframe2,CV_8U)

我什么都没有得到,helpframe2=0??而当我在resultframe上进行相同操作时,转换却成功了??

您有何建议应如何更改helpframe类型,因为我使用了result而不是frame?


“extractChannel”函数不是OpenCV的一部分,对吧? - Niko
你能在findContours之前检查一下helpframe.channels()报告的内容吗? - Niko
helpframe.channels() 是1,我不认为这是问题,因为当我使用帧而不是结果进行检查时,它仍然是1。 - Engine
1个回答

7

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

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

1
@引擎 添加了转换为无符号字符(应该被findContours接受)。 - Niko
1
我不明白你的意思。 - Engine
1
无论是(广泛的)文档还是错误信息都说明了"findContours"需要一个单通道8位输入图像。您已经成功提取了一个单通道,但缺少的是转换为CV_8UC1。这可以通过执行helpframe.convertTo(helpframe2, CV_8U);来实现 - 我的意思是我已经在上面的例子中添加了适当的代码。 - Niko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接