OpenCV找到最大的轮廓
OpenCV find biggest contours?
我有一个图像,我使用cvFindContours () on-我存储在imageContours的结果。然后我试图找到两个最大的轮廓,只显示它们。但由于某种原因,它显示了多于两条等高线。我的算法有什么问题?由于
编辑:哦,还有,在控制台中,如果没有一个选项为真,我一直得到"错误"消息,我在下面编程。有人知道为什么吗?
cvFindContours (binMask, imageContoursMem, &imageContours, sizeof (CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); //find the contours in binMask, and store results in ImageContours
//following conditions used to set f_handContour to the bigger contour for the first time, and s_handContour to the smaller contour for the first time
if ( (cvContourArea (imageContours, CV_WHOLE_SEQ)) > (cvContourArea((imageContours -> h_next), CV_WHOLE_SEQ)))
{ //begin first contour bigger condition
f_handContour = imageContours;
s_handContour = (imageContours -> h_next);
} //end second contour bigger condition
else if ( (cvContourArea (imageContours, CV_WHOLE_SEQ)) < (cvContourArea((imageContours -> h_next), CV_WHOLE_SEQ)))
{ //begin second contour bigger condition
f_handContour = (imageContours -> h_next);
s_handContour = imageContours;
} //begin second contour biggger condition
else if ( (cvContourArea (imageContours, CV_WHOLE_SEQ)) == (cvContourArea((imageContours -> h_next), CV_WHOLE_SEQ)))
{ //begin contours equal condition
f_handContour = imageContours; //if contours equal assignment of contours doesn't matter
s_handContour = (imageContours -> h_next);
} //end contours equal condition
else
{ //begin error condition
cout<<"Error";
} //end error condition
while ((imageContours -> h_next) != NULL)
{ //start of biggest/second biggest contour recognition loop
imageContours = (imageContours -> h_next);
if ( cvContourArea (f_handContour, CV_WHOLE_SEQ) < cvContourArea (imageContours, CV_WHOLE_SEQ))
{ //start of next contour bigger than f_handContour condition
s_handContour = f_handContour;
f_handContour = imageContours;
} //end start of next contour bigger than f_handContour condition
else if ( cvContourArea (f_handContour, CV_WHOLE_SEQ) > cvContourArea (imageContours, CV_WHOLE_SEQ))
{ //start of next contour smaller than f_handContour condition
if ( cvContourArea (s_handContour, CV_WHOLE_SEQ) < cvContourArea (imageContours, CV_WHOLE_SEQ))
{ //startof next contour bigger than s_handContour condition
s_handContour = imageContours;
} //end of next contour bigger than s_handContour condition
else if ( cvContourArea (s_handContour, CV_WHOLE_SEQ) > cvContourArea (imageContours, CV_WHOLE_SEQ))
{ //start of next contour smaller than s_handContour condition
//just pass on contour
} //end of next contour smaller than s_handContour condition
else if ( cvContourArea (s_handContour, CV_WHOLE_SEQ) == cvContourArea (imageContours, CV_WHOLE_SEQ))
{ //start of next contour equal to s_handContour condition
//just pass on contour
} // end of next contour equal to s_handContour condition
else
{ //start of error condition
cout<<"Error";
} //end of error condition
} //end of next contour smaller than f_handContour Condition
else if ( cvContourArea (f_handContour, CV_WHOLE_SEQ) == cvContourArea (imageContours, CV_WHOLE_SEQ))
{ //start of next contour equal to f_handContour condition
if ( cvContourArea (s_handContour, CV_WHOLE_SEQ) < cvContourArea (imageContours, CV_WHOLE_SEQ))
{ //startof next contour bigger than s_handContour condition
s_handContour = imageContours;
} //end of next contour bigger than s_handContour condition
else
{ //start of error condition
cout<<"Error";
} //end of error condition
} //end of next contour equal to f_handContour condition
else
{ //start of error condition
cout<<"Error";
} //end of error condition
} //end of biggest/second biggest contour recognition loop
cvDrawContours (output, f_handContour, cvScalar (0,255,0), cvScalar (0,255,255), 1, 3,8, cvPoint (0,0)); //draws the first hand contour derived from binMask on ouput
cvDrawContours (output, s_handContour, cvScalar (0,255,0), cvScalar (0,255,255), 1, 3,8, cvPoint (0,0)); //draws the second hand contour derived from binMask on ouput
CV_RETR_EXTERNAL只检索极端的外部轮廓。对于你的算法,你应该使用CV_RETR_LIST。尝试使用cvDrawContours函数的max_level值为0,因为值1绘制当前轮廓和与它在同一水平上的所有其他轮廓。你也可以优化你的条件很多(这是一种混乱),也存储在一些局部变量的区域,而不是调用cvContourArea那么多。
相关文章:
- 如何在OpenCV中存储部分轮廓点喜欢新矢量中的左侧,该类型为<vector<vector<Point>>
- Opencv 时刻没有成员命名 m00,同时试图确定轮廓的中心
- OpenCV : findcontours() , 太多的外部轮廓
- 查找从轮廓到点OPENCV C 的距离
- 在 OpenCV 和 C++ 中查找距轮廓中心最近的黑色像素
- 排序矩形轮廓OPENCV
- 如何使用 OpenCv 从图像中"cut"轮廓?
- OpenCV c++ 断言失败调用绘制轮廓
- 过滤不需要的轮廓-C 的OPENCV
- 在OpenCV中使用遮罩和ROI计算轮廓内浮器的图像像素值(例如平均值)
- OpenCV C++:按轮廓区域对轮廓进行排序
- 在OpenCV C/C++中只过滤出一个轮廓
- 如何在 OpenCv 中使用 HSV 阈值和轮廓从图像中提取皮肤
- OpenCV查找近距离物体的轮廓
- 如何在 OpenCV C++ 中找到最大的轮廓
- 使用 OpenCV C++绘制最大轮廓的凸包
- OpenCV查找轮廓的示例代码:向量释放问题
- 如何在 OpenCV 中按顺序访问轮廓
- 使用 OpenCV 2 说明书的轮廓示例编译错误
- 为什么我没有使用 OpenCV 获得正确的轮廓区域