Template matching is not working correctly?Is there a way to solve this

asked 2014-06-10 05:51:23 -0500

kirubhaRK gravatar image

updated 2014-06-10 06:05:55 -0500

berak gravatar image


I am using opencv for template matching.Actually i want to match the left hand or right hand of the person exactly by comparing it with sample left hand or right hand template that i have took and seperate his hand alone from that original image.

The following function is used for template matching:-

public void Template_Matching(Image template)


using (Image result = openCVImg.MatchTemplate(template, Emgu.CV.CvEnum.TM_TYPE.CV_TM_CCOEFF))


        double[] minValues, maxValues;

        System.Drawing.Point[] minLocations, maxLocations;

        result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);

        var bytes = result.Bytes;

        if (maxValues[0] > 1.1)
            istracking = true;

            // This is a match. Do something with it, for example draw a rectangle around it.
         //   System.Drawing.Rectangle match = new System.Drawing.Rectangle(maxLocations[0].X, maxLocations[0].Y, template.Width / 2, template.Height);
            System.Drawing.Rectangle match = new System.Drawing.Rectangle(new System.Drawing.Point(maxLocations[0].X, maxLocations[0].Y), new System.Drawing.Size(template.Width, template.Height));

            openCVImg.Draw(match, new Bgr(System.Drawing.Color.Red), 1);
            //   imageToShow.DrawPolyline(minLocations, false, new Bgr(System.Drawing.Color.Red), 3);

            istracking = false;



But the above function is not working correctly.For example in the below picture,

Fig 1:- i have compared the hand template with the person showing hand.Now it is matching correctly and the rectangle is drawn over the hand of the person.

Fig 2:- I have compared the hand template with closed hand of that person.But here it is highlighting the head of the person.(Why?)

Consider the person is in sleeping mode, My goal is to remove the left hand and right hand of the person simultaneously by matching the left hand of right hand template of any other person.I want to do pixel by pixel comparison of these two pictures and remove the left hand and right hand completely from the original image.

Can you please suggest me any other approach to acheive my goal?

handtrack.png handtrackwrong.png

edit retag flag offensive close merge delete


template matching is for 'pixel exact' things. it won't work, when there's any scaling, rotation, shearing or perspective is involved

also, for emgu related things, better ask here

berak gravatar imageberak ( 2014-06-10 06:09:57 -0500 )edit

Thanks berak, Yes i asked in emgu forum too, waiting for reply.In the meanwhile can you suggest me some other approach so that i could match my moving hand with any other hand template and mark exactly my hand.

kirubhaRK gravatar imagekirubhaRK ( 2014-06-10 06:33:34 -0500 )edit

didn't meant to bite you away with the forum remark, it's just a 3rd party api, and they might know better.

but i'd try some shape-based approach first, look at findContours() and matchShapes()

berak gravatar imageberak ( 2014-06-10 06:39:25 -0500 )edit

Why don't you use a second template of a closed hand?

GilLevi gravatar imageGilLevi ( 2014-06-10 07:54:22 -0500 )edit

Actually i am trying to match the hand template like this exactly during the different poses while he is sleeping.I tried matchtemplate,findcontour methods but it is not exactly matching during motion of that person.


Can you please suggest any method , which will identify the hand like this exactly.

kirubhaRK gravatar imagekirubhaRK ( 2014-06-10 10:08:38 -0500 )edit

If you know the expected size (or rotation) of the object in image, try to resize (rotate) the template image to the expected size/angle.

icedecker gravatar imageicedecker ( 2014-06-11 14:32:40 -0500 )edit

hand size will vary for person to person.Can you suggest me any suitable approach for my experiment , so that it will be very helpful to achieve my goal?

kirubhaRK gravatar imagekirubhaRK ( 2014-06-12 02:11:01 -0500 )edit

perhaps HOG+SVM? or Haar cascades?

GilLevi gravatar imageGilLevi ( 2014-06-12 02:25:53 -0500 )edit

Or Haar cascades?

GilLevi gravatar imageGilLevi ( 2014-06-12 02:26:59 -0500 )edit