我现在正在工作的程序几乎已完成,但我对结果并不十分满意。通过使用Canny算法,我设法清楚地了解了对象的轮廓,但是程序在识别轮廓和用红线绘制轮廓时存在一些问题。该计划:
void setwindowSettings(){
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
createTrackbar("LowerC", "Contours", &lowerC, 255, NULL);
createTrackbar("UpperC", "Contours", &upperC, 255, NULL);
}
void wait(void)
{
long t=30000000;
while(t--);
}
int main(void)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat frame,foreground,image;
double pt1, pt2, area;
Rect rect;
int i;
vector<vector<Point> > contours;
vector<vector<Point> > largest_contours;
namedWindow("Capture", CV_WINDOW_AUTOSIZE);
setwindowSettings();
while(1){
cap >> frame; // get a new frame from camera
if( frame.empty() )
break;
image=frame.clone();
cvtColor(image,foreground,CV_BGR2GRAY);
GaussianBlur(foreground,foreground,Size(9,11),0,0);
Canny(foreground,foreground,lowerC,upperC,3);
findContours(foreground,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
if(contours.empty())
continue;
double largest_area = 0;
for( i= 0; i < contours.size(); i++){ // get the largest contour
area = fabs(contourArea(contours[i]));
if(area >= largest_area){
largest_area = area;
largest_contours.clear();
largest_contours.push_back(contours[i]);
}
}
if(largest_area>=3000){ // draw the largest contour if exceeded minimum largest area
drawContours(image,largest_contours,-1,Scalar(0,0,255),2);
printf("area = %.f\n",largest_area);
}
wait();
imshow( "Capture",image );
imshow("Contours",foreground);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
计划摘要:
结果:
我很少得到我想要的东西;检测到轮廓,绘制红线( GOOD ONE ):
...通常我得到了这个;未检测到轮廓,不是红线( BAD ONE ):
获得 GOOD ONE 的机会约为1/20
,这不是很好。此外,当对象周围出现红线时,Contours
屏幕中对象轮廓线将闪烁(参见GOOD ONE图片)。
我正在使用我的一个物体(一个小的黑色方框)来解决这个问题,但请注意,这个物体检测程序的主要目的是来检测物体,无论其形状或颜色如何。
所以我的问题是:
编辑:我刚刚发现轮廓线的闪烁不是因为它周围画了红线(使用drawContours
或line
函数)但是它发生在最大轮廓由findContours
函数检测并计算为最大轮廓。
关于否的问题。 3单击HERE。 VIDEO HERE, CLICK IT!!!
提前致谢。
PS:我在Visual C ++ 2010 Exp。
上使用OpenCV 2.4.3答案 0 :(得分:1)
absdiff(frame_now,frame_backgrnd,diff)
其中diff
是差异图像。为绘制积分,请尝试此
for(int i = 1;i<(int)largest_contours[0].size();i++)
line(image,largest_contours[0][i-1],largest_contours[0][i],cv::Scalar(0,0,255),2,8,0);