Skip to content

Commit

Permalink
Add mask parameter to matrix.matchTemplate
Browse files Browse the repository at this point in the history
add examples for matchTemplate with and without mask.
  • Loading branch information
Arthur Cinader committed Jun 23, 2016
1 parent 5fb426a commit f920053
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
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);
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

0 comments on commit f920053

Please sign in to comment.