【发布时间】:2013-01-29 08:41:30
【问题描述】:
我还是 C++ 的新手,现在我需要将我的 old program 的一些部分从 C 转换为 C++,因为我想在我的程序中应用 BackgroundSubtractorMOG2,因为它只在 C++ 中可用。基本上,这个程序会根据background subtraction 检测来自摄像机的轮廓,并选择可用的最大轮廓。
我在这部分特别有问题(取自old program):
double largestArea = 0; //Const. for the largest area
CvSeq* largest_contour = NULL; //Contour for the largest area
while (current_contour != NULL){ //If the current contour available
double area = fabs(cvContourArea(current_contour,CV_WHOLE_SEQ, false)); //Get the current contour's area as "area"
if(area > largestArea){ //If "area" is larger than the previous largest area
largestArea = area;
largest_contour = current_contour;
}
current_contour = current_contour->h_next; //Search for the next contour
}
这部分是程序将扫描每个可用的轮廓current_contour,找到它的区域并将其与之前的最大轮廓进行比较。我的问题是如何获得current_contour,它的区域并跳转到C++ 中的下一个轮廓?另外,C++ 中contours.size() 表示什么? 是吗?扫描的轮廓数或轮廓的总面积?
这是我到目前为止所做的:
for(;;)
{
cap >> frame; // get a new frame from camera
if( frame.empty() )
break;
image=frame.clone();
mog(frame,foreground,-1);
threshold(foreground,foreground,lowerC,upperC,THRESH_BINARY);
medianBlur(foreground,foreground,9);
erode(foreground,foreground,Mat());
dilate(foreground,foreground,Mat());
findContours(foreground,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
if(contours.empty())
continue;
//Starting this part
double largest_area = 0;
for(int i= 0; i < contours.size(); i++){
double area = contourArea(contours);
if(area >= largest_area){
largest_area = area;
largest_contours = contours;
}
}
//Until this part
drawContours(image,largest_contours,-1,Scalar(0,0,255),2);
imshow( "Capture",image );
imshow("Contours",foreground);
if(waitKey(30) >= 0) break;
}
提前致谢。
PS:旧程序有一些错误,但算法运行良好。如果您需要更新的程序,请像我一样免费。目前使用OpenCV 2.4.3 + VS C++ 2010 Exp.
编辑:
感谢所有试图帮助我的人,但我已经得到了来自here 的答案。不过,对于那些还不知道的人:C 中的 OpenCV 与 C++ 中的 OpenCV 并不完全相同。
【问题讨论】:
-
您可以描述您如何解决问题并将其发布并接受它作为答案。这样我们就知道了。它已解决,并且 b。你是怎么解决的。
标签: c++ c opencv video-processing