0

我还是 C++ 的新手,现在我需要将我的这个旧程序中的一些部分从 C 转换为 C++,因为我想BackgroundSubtractorMOG2在我的程序中应用它,因为它只在 C++ 中可用。基本上,该程序将基于背景减法检测摄像机的轮廓并选择可用的最大轮廓。

我在这部分特别有问题(取自旧程序):

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。

编辑:

感谢所有试图帮助我的人,但我已经得到了来自这里的答案。尽管如此,对于那些仍然不知道的人来说:OpenCV in CIS NOT EXACTLY THE SAME as OpenCV inC++ .

4

1 回答 1

0

这是代码的一部分,我在其中找到图像上的所有轮廓并计算它们的周长和面积:

IplImage* bin = cvCreateImage( cvGetSize(_image), IPL_DEPTH_8U, 1);
cvConvertImage(_image, bin, CV_BGR2GRAY);
cvCanny(bin, bin, 50, 200);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* contours=0;

//Number of all contours on image @contoursCont@
int contoursCont = cvFindContours( bin, storage,&contours,sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));
assert(contours!=0);

// iterate through all contours --> current = current->h_next
 for( CvSeq* current = contours; current != NULL; current = current->h_next )
 {
     //calculate perimeter and area of each contour
     double area = fabs(cvContourArea(current));
     double perim = cvContourPerimeter(current);
     cvDrawContours(_image, current, cvScalar(0, 0, 255), cvScalar(0, 255, 0), -1, 1, 8);
     //the rest code 
  }

来自 OpenCV 文档:

函数 cvFindContours 从二值图像中检索轮廓并返回检索到的轮廓数。指针 CvSeq* contours=0 由函数填充。如果没有检测到轮廓(如果图像完全是黑色),它将包含指向第一个最外层轮廓的指针或 NULL。可以使用 h_next 和 v_next 链接从 first_contour 到达其他轮廓。

于 2013-01-29T11:30:06.013 回答