Template Matching issue in video(object detection in frame by frame)

asked 2015-04-22 08:16:43 -0600

Sharath gravatar image

Hi,

I am working with multiple object detection application, as part of this I am trying to detect two of the objects using Template Matching method. I have cropped two templates in video for further process. Below are the templates, image description image description

First template is railway track separater, second one is switches placed next to track. I am trying to detect this two in source video while video file is running frame by frame in loop.

My application detecting two templates when they appear in frame. The problem is even though both templates disappear from frame, still application detecting some area. Below is the frame with expected result, image description

And frames with miss match, image description image description

Could any one help me to overcome this problem.

I just want to detect the objects with respect to providing templates in video. I don't want to track the objects. Below is the code I have used, //utility functions Mat TplMatch( Mat &img, Mat &mytemplate/vector<mat>temp/) { Mat result; matchTemplate( img, mytemplate/temp/, result, CV_TM_CCOEFF_NORMED ); normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() ); return result; }

        //Localizing the best match with minMaxLoc 
        Point minmax( Mat &result )
            {
                  double minVal, maxVal;
                  Point  minLoc, maxLoc, matchLoc;
                 minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
                 matchLoc = maxLoc;
               //cout<<"minVAl of temp:"<<minVal<<"maxVAl of temp:"<<maxVal<<endl;
                  return matchLoc;
            }


     //in Main function(inside loop)

        Mat temp = imread("temp.png", 1);
    Mat result  =  TplMatch( videoFeed, temp);
   Point match = minmax(result);
   rectangle(videoFeed, match, Point(match.x + temp.cols , match.y + temp.rows), Scalar(0,255,0), 3);

help me to overcome this problem. Help would be appreciated greatly.

Regards.

edit retag flag offensive close merge delete

Comments

What is the correlation score when it is a true match and what is the correlation score when it is a false match ?

If the scores really differ, you could determine if it is a true match by thresholding the correlation score.

Eduardo gravatar imageEduardo ( 2015-04-22 14:52:23 -0600 )edit

Hi Eduardo, Thanks for your answer. How to check the correlation score? Kindly explain me. In past I have worked with template matching on offline Images, now I am implementing this method on live video.

Sharath gravatar imageSharath ( 2015-04-22 23:50:28 -0600 )edit
1

Hi Eduardo, I have completed the task with PSNR (Peak Signal to Noise Ratio) algorithm. By using PSNR we can find most valid similar pixel in both images(here images are Template and Source image). Once application finds similarity, it 'll draw rectangle around the detected template object in source image. Thank you.

Sharath gravatar imageSharath ( 2015-04-23 06:02:03 -0600 )edit

Hi Sharath, I am also working on the similar kind of scenario. I am new to OpenCV and I dont know how to proceed. I have already written code for the offline template matching. Its working fine. Now I want to do template matching in a live video. Could you please help me to complete the task. It would be great if you share your complete code so that I will get an idea and I can proceed. Thanks, Manimaran

Manimaran gravatar imageManimaran ( 2015-06-23 02:57:02 -0600 )edit

Hi Sharath, I have one more question in addition to the above one. How did you gave Multiple Template images in template matching. Can you please explain how to give my multiple template images. Thanks, Manimaran

Manimaran gravatar imageManimaran ( 2015-06-23 06:51:16 -0600 )edit
1

@Manimaran.., I can't share complete source code here as this application is confidential. And below link gives you idea and helps you to know how to push lot of templates into vector container and use it back in detection phase. http://answers.opencv.org/question/35... . If you have any doubts, come back.

Sharath gravatar imageSharath ( 2015-06-26 00:13:04 -0600 )edit

@Sharath, Thanks Sharath for your reference. It would be great if you share your email id or contact details if you dont mind ,so that i contact via email. I'm just going through the link which you have shared and let me try..The link which you have shared is for the offline template matching..Actually I have to do template matching for a live video...Actually i tried to incorporate the webcam for the live video but its not working.. I think you have done template matching for the live video..can you please help me on that. Once again i would like to thank you for the link. Regards, Manimaran

Manimaran gravatar imageManimaran ( 2015-06-26 04:34:57 -0600 )edit
Sharath gravatar imageSharath ( 2015-06-26 05:57:29 -0600 )edit