Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

c++ - Better algorithm for edge filter in video programming

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) The good one

Second, the failure(an orange box) Failure

And last, another failure(a white box) White failure

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem isn't the edge detection algorithm. Your problem is that you are hard-coding the algorithm parameters and expecting it to magically work for all images you throw at it. Also, smoothing the image before using cvCanny is unnecessary, as the Canny operator already performs smoothing for you.

Since it's a bit more clear what you want to achieve now, I can give a suggestion: work with the video instead of looking at each frame individually. If the camera is fixed, and the hand with the object is moving, then detecting the shape is trivial through background subtraction. If the camera is not fixed, you can still detect the hand (PDF link) and work from there. Also, use any other application-specific knowledge you may possess (e.g. item will be in the middle of the screen, hand will be below the item).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...