Ask Your Question
0

Matching template on video

asked 2014-06-18 13:23:25 -0600

updated 2014-06-18 13:47:35 -0600

unxnut gravatar image

Hi all,

I am a beginner in c + + and opencv, I try to apply this algorithm : http://answers.opencv.org/answers/18440/revisions/

to an object to be detected in a video, I change the code :

int main()
{
    Mat scene = imread("c:\\scene.png");
    Mat object = imread("c:\\object.png");
    vector<Point> match_centers = tempMatch(object,scene,CV_TM_SQDIFF_NORMED,0.9);
    drawMatch(object,scene,match_centers);
    imshow("result",scene);
    waitKey(0);
    return 0;
}

by this :

int main()
{
    Mat object = imread("C:\\object.png");
    string filename = "C:\\video.flv";
    VideoCapture cap(filename);

    Mat edges;
    namedWindow("edges",1);

    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera

        vector<Point> match_centers = tempMatch(object,frame,CV_TM_SQDIFF_NORMED,0.9);
        drawMatch(object,frame,match_centers);
        imshow("edges", frame);

        if(waitKey(10) >= 0) break;
    }

    return 0;
}

In my modified code i try to match template in the video frame by frame trough the loop for, it work perfectly, the rectangle select the object to match when the object appear in the video, "my object is an ads sometime is appearing and sometime no".

The problem, when the object to match is not in the video the algorithme continues to draw rectangles i think because : draw Match(object,frame,match_centers); is in the loop, any ideas how i can fix that ?

Some image for more details :

1 - Video without algorithme applied :

image description

2 - Here The code is looking for the template to match, as you see algorithm draw many rectangles and the object is not yet display, "I want to remove it" :

image description

3 - Here the code is work and the object is matched :

image description

Best regards!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-06-18 13:57:47 -0600

unxnut gravatar image

Change your drawMatch statement to

if ( match_centers.size() > 0 )
    drawMatch(object,frame,match_centers);

and that should fix it. You may also want to reset the vector to size 0 before you do tempMatch.

edit flag offensive delete link more

Comments

Thank you for your response, How i can reset the vector to size 0 ? I tried with res.size() == 0 but same problem ... i think res is the name of my vector but i'am not sure.

Thank you again!

dagotaa gravatar imagedagotaa ( 2014-06-18 14:05:18 -0600 )edit

I tried to put :

vector<Point> res; res.size() == 0;

before : if ( match_centers.size() > 0 ) drawMatch(object,frame,match_centers);

But, it does not work :(

dagotaa gravatar imagedagotaa ( 2014-06-18 15:25:06 -0600 )edit

Question Tools

Stats

Asked: 2014-06-18 13:23:25 -0600

Seen: 227 times

Last updated: Jun 18 '14