I'm still working on the last program and although I finally found out how to solve the problem (on how to filter the biggest contour), I now have a new question, or rather a problem.
As you can see I am using Canny algorithm for searching the edges in the video. But the object I will using for the detection has no particular color so when the object's color is about the same as the surrounding's color (for example if the object is silver and the background is white) the object's edge will disappear and I cannot get the object's contour.
For now I will test every edge filtering algorithm available in OpenCV but to cut my work short, I need your help to recommend the best(or at least better) algorithm than canny. Now I have tested Sobel but the result is no better than canny's. If possible, please link me to some good example for reference.
The code:
int main( int argc, char** argv )
{
CvCapture *cam;
CvMoments moments;
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* contours = NULL;
CvSeq* contours2 = NULL;
CvPoint2D32f center;
int i;
cam=cvCaptureFromCAM(0);
if(cam==NULL){
fprintf(stderr,"Cannot find any camera.
");
return -1;
}
while(1){
IplImage *img=cvQueryFrame(cam);
if(img==NULL){return -1;}
IplImage *src_gray= cvCreateImage( cvSize(img->width,img->height), 8, 1);
cvCvtColor( img, src_gray, CV_BGR2GRAY );
cvSmooth( src_gray, src_gray, CV_GAUSSIAN, 5, 11);
cvCanny(src_gray, src_gray, 70, 200, 3);
cvFindContours( src_gray, storage, &contours, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
if(contours==NULL){ contours=contours2;}
contours2=contours;
CvSeq* current_contour = contours;
double largestArea = 0;
CvSeq* largest_contour = NULL;
while (current_contour != NULL){
double area = fabs(cvContourArea(current_contour,CV_WHOLE_SEQ, false));
if(area > largestArea){
largestArea = area;
largest_contour = current_contour;
}
current_contour = current_contour->h_next;
}
cvMoments(largest_contour, &moments, 1);
double m_00 = cvGetSpatialMoment( &moments, 0, 0 );
double m_10 = cvGetSpatialMoment( &moments, 1, 0 );
double m_01 = cvGetSpatialMoment( &moments, 0, 1 );
float gravityX = (m_10 / m_00)-150;
float gravityY = (m_01 / m_00)-150;
if(gravityY>=0&&gravityX>=0&&m_00>=3000){
printf("center point=(%.f, %.f), Area = %.f
",gravityX,gravityY,m_00); }
if(m_00>=3000){
CvScalar color = CV_RGB(250,0,0);
cvDrawContours(img,largest_contour,color,color,-1,-1, 8, cvPoint(0,0));
}
cvShowImage( "Input", img );
cvShowImage( "Contours", src_gray );
cvClearMemStorage(storage);
if(cvWaitKey(33)>=0) break;
}
cvDestroyWindow("Contours");
cvDestroyWindow("Source");
cvReleaseCapture(&cam);
}
...and finally, the long waited example pictures:
First, the good one(my black wallet)
Second, the failure(an orange box)
And last, another failure(a white box)
P.S., Some notes:
- The object has no particular shape, color or size so IMO the best bet is to find the edge of the object rather than filtering it by colors.
- I will hold the object so maybe my finger can cause the object's edge to change or disappear.
- I'm working on a video processing program so the shorter the processing time and the lesser the processing power needed, the better.
- My program will filter out the biggest contour and fill it with red color (see the first picture).
Thanks in advance. Cheers
See Question&Answers more detail:
os