Skip to content

Bizzarre Bug in matchTemplate CV_TM_SQDIFF #9099

@PkLab

Description

@PkLab

System information (version)

  • OpenCV => 3.2.0
  • Operating System / Platform => Windows 64 Bit
  • Compiler => Visual Studio 2013

Detailed description

Looking for multiple exact template match I'm trying CV_TM_SQDIFF. In case of exact match, score 0 is expected but result is not always the same between consecutive calls or matching position in the image.

Steps to reproduce

A bit hard to explain but following code can reproduce the error.

  1. Use opencv-logo-small.png as template
  2. Create a flat image BGR 640x480
  3. Copy the template multiple times in the image at points Pi(x,y)
  4. Perform CV_TM_SQDIFF
  5. Repeat N times with different points set

The result in P(x,y) should be always 0 but not always it is.

    int matchTemplateBug()
    {
        Mat T = imread("OpenCV/3.2.0/sources/opencv/doc/opencv-logo-small.png");
        Rect R(Point(0, 0), T.size()); //a rect roi over the image
        Mat I(480, 640, T.type());
        // vector of exact match point
        vector<vector < Point >> locationSets = {
            { Point(0, 0), Point(100, 30) },
            { Point(0, 0), Point(100, 30) },
            { Point(1, 1), Point(100, 30), Point(150, 200) },
            { Point(0, 0), Point(100, 30), Point(150, 200) },
            { Point(0, 0), Point(101, 31), Point(150, 200) },
        };
         //duplicate 1st set ->but get different result
        locationSets.push_back(locationSets[0]);

        //loop for all locations set
        for (size_t n = 0; n < locationSets.size(); n++)
        {
            cout << endl << "Locations: ";
            for (size_t i = 0; i < locationSets[n].size(); i++)
            {
                R = Rect(locationSets[n][i], T.size());
                T.copyTo(I(R));
                cout << locationSets[n][i] << "\t";
            }
            Mat result;
            int match_method = CV_TM_SQDIFF;
            matchTemplate(I, T, result, match_method);
            cout << endl << "   Scores: ";
            for (size_t i = 0; i < locationSets[n].size(); i++)
            {
                cout << result(Rect(locationSets[n][i], Size(1, 1))) << "\t\t";
            }
            cout << endl;
        }
        return 0;
    }

Below is the output (scores should be all 0)

Locations: [0, 0]	[100, 30]	
   Scores: [0]		[0]		

Locations: [0, 0]	[100, 30]	
   Scores: [0]		[0]		

Locations: [1, 1]	[100, 30]	[150, 200]	
   Scores: [12]		[0]		[0]		

Locations: [0, 0]	[100, 30]	[150, 200]	
   Scores: [12]		[0]		[0]		

Locations: [0, 0]	[101, 31]	[150, 200]	
   Scores: [0]		[0]		[0]		

Locations: [0, 0]	[100, 30]	
   Scores: [12]		[0]		

cc @snosov1 might this be related to #6919 (sorry @sovrasov for mistake cc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions