node-opencv icon indicating copy to clipboard operation
node-opencv copied to clipboard

matchTemplate

Open dominikdolancic opened this issue 9 years ago • 18 comments
trafficstars

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 avatar Mar 11 '16 10:03 dominikdolancic

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

dominikdolancic avatar Mar 14 '16 13:03 dominikdolancic

@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.

salmanulhaq avatar Mar 14 '16 13:03 salmanulhaq

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

dominikdolancic avatar Mar 14 '16 13:03 dominikdolancic

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

salmanulhaq avatar Mar 14 '16 13:03 salmanulhaq

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');

              });

 });

dominikdolancic avatar Mar 14 '16 14:03 dominikdolancic

whats the exact error?

salmanulhaq avatar Mar 14 '16 14:03 salmanulhaq

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

bobvanluijt avatar Mar 14 '16 14:03 bobvanluijt

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

dominikdolancic avatar Mar 14 '16 14:03 dominikdolancic

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');
    });
});

salmanulhaq avatar Mar 14 '16 14:03 salmanulhaq

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

dominikdolancic avatar Mar 14 '16 14:03 dominikdolancic

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.

salmanulhaq avatar Mar 14 '16 14:03 salmanulhaq

@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

bobvanluijt avatar Mar 14 '16 14:03 bobvanluijt

@salmanulhaq would be great if you could make the fix

dominikdolancic avatar Mar 14 '16 14:03 dominikdolancic

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!

ylh888 avatar Mar 26 '16 13:03 ylh888

@dominikdolancic you can take a look at https://github.com/peterbraden/node-opencv/pull/419

if not too late :).

acinader avatar Jun 18 '16 16:06 acinader

Using an image (=opencv matrix) instead image file as template: https://github.com/peterbraden/node-opencv/issues/420

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

strarsis avatar Sep 13 '16 17:09 strarsis

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

ghafran avatar Mar 13 '17 20:03 ghafran

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

developer239 avatar May 08 '17 19:05 developer239