Skip to content

Add mask parameter to matrix.matchTemplate #419

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/files/lena_tmpl.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/files/mask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/files/stuff-template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/files/tmpl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions examples/match-template-mask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// see https://github.com/Itseez/opencv/blob/master/samples/cpp/mask_tmpl.cpp
const cv = require('../lib/opencv');

const TM_CCORR_NORMED = 3
const imageFilename = 'files/lena_tmpl.jpg';
const templateFilename = 'files/tmpl.png';
const maskFilename = 'files/mask.png';
const outFilename = 'tmp/match-template-mask.png';

cv.readImage(imageFilename, function(err, im) {
if (err) throw err;
cv.readImage(templateFilename, function (err, tmpl) {
if (err) throw err;
const res = im.matchTemplate(templateFilename, TM_CCORR_NORMED, maskFilename);
const minMax = res.minMaxLoc();
const topLeft = minMax.maxLoc;
im.rectangle([topLeft.x, topLeft.y], [tmpl.width(), tmpl.height()], [0, 255,0], 2);

im.save(outFilename);
console.log('Image saved to ' + outFilename);
});
});
20 changes: 20 additions & 0 deletions examples/match-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const cv = require('../lib/opencv');

const TM_CCORR_NORMED = 3;
const imageFilename = 'files/stuff.png';
const templateFilename = 'files/stuff-template.png';
const outFilename = 'tmp/match-template.png';

cv.readImage(imageFilename, function (err, im) {
if (err) throw err;
cv.readImage(templateFilename, function (err, tmpl) {
if (err) throw err;
const res = im.matchTemplate(templateFilename, TM_CCORR_NORMED);
const minMax = res.minMaxLoc();
const topLeft = minMax.maxLoc;
im.rectangle([topLeft.x, topLeft.y], [tmpl.width(), tmpl.height()], [0, 255,0], 2);

im.save(outFilename);
console.log('Image saved to ' + outFilename);
});
});
15 changes: 14 additions & 1 deletion src/Matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,7 @@ NAN_METHOD(Matrix::TemplateMatches) {

// @author ytham
// Match Template filter
// Usage: output = input.matchTemplate("templateFileString", method);
// Usage: output = input.matchTemplate("templateFileString"[, "method"][, "maskPath"]);
NAN_METHOD(Matrix::MatchTemplate) {
Nan::HandleScope scope;

Expand Down Expand Up @@ -2229,6 +2229,19 @@ NAN_METHOD(Matrix::MatchTemplate) {
int method = (info.Length() < 2) ? (int)cv::TM_CCORR_NORMED : info[1]->Uint32Value();
cv::matchTemplate(self->mat, templ, m_out->mat, method);

if (info.Length() < 3) {
cv::matchTemplate(self->mat, templ, m_out->mat, method);
} else {
v8::String::Utf8Value args2(info[2]->ToString());
std::string maskFilename = std::string(*args2);
cv::Mat mask;
mask = cv::imread(maskFilename, CV_8S);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be -1 instead of CV_8S??

if (mask.size().height == 0 && mask.size().width == 0) {
throw std::runtime_error(("Failed to load mask file: " + maskFilename).c_str());
}
cv::matchTemplate(self->mat, templ, m_out->mat, method, mask);
}

info.GetReturnValue().Set(out);
}

Expand Down