Skip to content

matchTemplate #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dominikdolancic opened this issue Mar 11, 2016 · 18 comments
Open

matchTemplate #387

dominikdolancic opened this issue Mar 11, 2016 · 18 comments

Comments

@dominikdolancic
Copy link

I tried to write an example for matchTemplate but without success. Please if someone could write some example for using this function.
Thanks in advance,
Dominik

@dominikdolancic dominikdolancic changed the title templateMatching matchTemplate Mar 11, 2016
@dominikdolancic
Copy link
Author

Error what i received is
OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) in matchTemplate, file /tmp/opencv20160107-54198-1duzac3/opencv-2.4.12/modules/imgproc/src/templmatch.cpp, line 249
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv20160107-54198-1duzac3/opencv-2.4.12/modules/imgproc/src/templmatch.cpp:249: error: (-215) (img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type() in function matchTemplate

@salmanulhaq
Copy link
Contributor

@padom template's Mat and the input image should both be of same type (single channel etc) and depth (8bit etc). Make sure that's the case.

@dominikdolancic
Copy link
Author

They both are the same type and depth, template image is actually cropped copy of original. Do you have another idea?

@salmanulhaq
Copy link
Contributor

are they RGB or single channel? You need to convert both template and input to single channel before running matchTemplate()

@dominikdolancic
Copy link
Author

Thanks for helping, now I got another error.
I don't know where am I mistaken, my code looks like

Assertion failed: (object->InternalFieldCount() > 0), function Unwrap, file ../node_modules/nan/nan_object_wrap.h, line 33.
Abort trap: 6

cv.readImage("./test.jpg", function(err, im) {

              cv.readImage("./template.jpg", function(err, template) {

                var width = im.width();
                var height = im.height();
                if (width < 1 || height < 1) throw new Error('Image has no size');

                im.convertGrayscale();
                template.convertGrayscale();

                // Create the result matrix
                var result_cols = im.col() - template.col() + 1;
                var result_rows = im.row() - template.row() + 1;
                var result = new cv.Matrix(result_cols, result_rows, cv.Constants.CV_32FC1);

                im.matchTemplate(template, result, 3);

                result.save('./pic.jpg');

              });

 });

@salmanulhaq
Copy link
Contributor

whats the exact error?

@bobvanluijt
Copy link

As a sidenote, I'm looking for the documentation on matchTemplate() where can I find this or should I go through the code?

@dominikdolancic
Copy link
Author

I've just edited post above, here is the error

OpenCV Error: Assertion failed (corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1) in crossCorr, file /tmp/opencv20160107-54198-1duzac3/opencv-2.4.12/modules/imgproc/src/templmatch.cpp, line 70
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv20160107-54198-1duzac3/opencv-2.4.12/modules/imgproc/src/templmatch.cpp:70: error: (-215) corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1 in function crossCorr

@salmanulhaq
Copy link
Contributor

I just looked at the code to see what might be wrong. Here's your amended code which may work (I didn't test):

cv.readImage("./test.jpg", function(err, im) {
    im.convertGrayscale();
    im.matchTemplate("./template.jpg", 3, function(result){
        result.save('./pic.jpg');
    });
});

@dominikdolancic
Copy link
Author

Doesn't work, same error like before

OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) in matchTemplate, file /tmp/opencv20160107-54198-1duzac3/opencv-2.4.12/modules/imgproc/src/templmatch.cpp, line 249
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv20160107-54198-1duzac3/opencv-2.4.12/modules/imgproc/src/templmatch.cpp:249: error: (-215) (img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type() in function matchTemplate

@salmanulhaq
Copy link
Contributor

there's an inherent issue which the author of this function may have overlooked. He reads the template image as:

cv::imread(filename, CV_8S);

which means he's reading it as 8bit signed image while it should've been 8U and not 8S. I can create a pull request with the fix though.

P.S: also just checked and OpenCV's matchTemplate doesn't require image to be single channel anymore.

@bobvanluijt
Copy link

@salmanulhaq does this mean we can't use the function at all or is there a workaround? I also did find this: http://stackoverflow.com/a/35727588/1501285

@dominikdolancic
Copy link
Author

@salmanulhaq would be great if you could make the fix

@ylh888
Copy link

ylh888 commented Mar 26, 2016

is it possible to just use an image rather than image file as template?

also, how do I access the probabilities of the match (to compare with other images)?

Thanks!

@acinader
Copy link

@dominikdolancic you can take a look at #419

if not too late :).

@strarsis
Copy link

strarsis commented Sep 13, 2016

Using an image (=opencv matrix) instead image file as template: #420

Working example using node-opencv template matching
(albeit with an image file as template): node-opencv-templatematching-test

@ghafran
Copy link

ghafran commented Mar 13, 2017

Here is how I got this to work:

var cv = require('opencv');
cv.readImage(__dirname + '/map.jpg', function (err, im) {
    var output = im.matchTemplate(__dirname + '/sa.jpg', 3);
    console.log('top left:', output[1], output[2], ' bottom right: ', output[1] + output[3], output[2] + output[4]);
    im.line([output[1], output[2]], [output[1] + output[3], output[2] + output[4]]);
    im.save(__dirname + '/out.jpg');
});

The output seems to be an array that looks like this:
[ [ Matrix 422x1029 ], 232, 4, 172, 124 ]

Ignore first element in the array, next two are coordinates of top left, last two are lengths of the bounding box.

btw, before you can install opencv module, you need to run the following

sudo apt-get install libopencv-dev python-opencv

@developer239
Copy link

This is cool. However I did not find any documentation mentioning finding multiple template matches on one image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants