在OpenCV的轮廓比较(皈依从C到C ++) [英] Contour comparison in OpenCV (Convertion from C to C++)
问题描述
我还是用C ++新的,现在我需要一些地方从这个<一个转换href=\"http://stackoverflow.com/questions/12971093/better-algorithm-for-edge-filter-in-video-programming\">old雷从C到C计划 ++,因为我想申请 BackgroundSubtractorMOG2
在我的程序,因为它仅适用于C ++。基本上这个程序会从一台摄像机的基础上<一轮廓检测href=\"http://docs.opencv.org/trunk/modules/video/doc/motion_analysis_and_object_tracking.html#backgroundsubtractormog2\"相对=nofollow>背景减除并选择最大的轮廓可用。
I am still new in C++ and now I need to convert some parts from this old program of mine from C to C++ because I want to apply BackgroundSubtractorMOG2
in my program since it only available in C++. Basically this program will detect contours from a video camera based on background subtraction and choose the largest contours available.
我有一个问题,尤其是在这个部分(从<一所href=\"http://stackoverflow.com/questions/12971093/better-algorithm-for-edge-filter-in-video-programming\">old计划):
I have a problem particularly on this part (taken from the 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
每个轮廓,发现其面积和比较,previous最大的轮廓。我的问题是如何获得 current_contour
,其面积并跳转到C ++中的下一个轮廓?同时,什么是<指示code> contours.size()在C ++?是否扫描轮廓的数量或轮廓的总面积?
This part is where the program will scan each contour available as current_contour
, find its area and compare it to previous largest contour. My question is how to get the current_contour
, its area and jump to the next contour in C++? Also, what is indicated by contours.size()
in C++? Is it the number of contours scanned or the total area of the contours?
这是我迄今所做的:
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进出口。
PS: The old program got some bugs in it but the algorithm works just fine. Free to as me if you need the updated program. Currently using OpenCV 2.4.3 + VS C++ 2010 Exp.
编辑:
感谢大家谁正在试图帮助我,但我已经得到了这是从的此处。尽管如此,对于那些怎么还不知道: OpenCV的在 C
不是完全一样的OpenCV在 C ++
Thanks to everybody who're trying to help me but I already got the answer which is from here. Still, for those how still don't know: OpenCV in C
IS NOT EXACTLY THE SAME AS OpenCV in C++
.
推荐答案
这是code,其中我发现图像上的所有轮廓和calcilate他们的周长和面积的一部分:
This is a part of the code, where I am finding all contours on image and calcilate their perimeter and area:
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的文档:
From OpenCV documentation:
该函数cvFindContours检索来自二进制图象轮廓和返回检索轮廓的数量。指针CvSeq *轮廓= 0由函数填充。它将包含一个指针到第一最外轮廓或NULL,如果没有检测到轮廓(如果图像全黑)。其它轮廓可以从first_contour使用h_next和v_next链接来。
The function cvFindContours retrieves contours from the binary image and returns the number of retrieved contours. The pointer CvSeq* contours=0 is filled by the function. It will contain a pointer to the first outermost contour or NULL if no contours are detected (if the image is completely black). Other contours may be reached from first_contour using the h_next and v_next links.
这篇关于在OpenCV的轮廓比较(皈依从C到C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!