node-opencv
node-opencv copied to clipboard
matchTemplate
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
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
@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.
They both are the same type and depth, template image is actually cropped copy of original. Do you have another idea?
are they RGB or single channel? You need to convert both template and input to single channel before running matchTemplate()
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');
});
});
whats the exact error?
As a sidenote, I'm looking for the documentation on matchTemplate() where can I find this or should I go through the code?
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
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');
});
});
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
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 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
@salmanulhaq would be great if you could make the fix
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!
@dominikdolancic you can take a look at https://github.com/peterbraden/node-opencv/pull/419
if not too late :).
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
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
This is cool. However I did not find any documentation mentioning finding multiple template matches on one image.