Skip to content

Commit f920053

Browse files
author
Arthur Cinader
committed
Add mask parameter to matrix.matchTemplate
add examples for matchTemplate with and without mask.
1 parent 5fb426a commit f920053

File tree

7 files changed

+56
-1
lines changed

7 files changed

+56
-1
lines changed

examples/files/lena_tmpl.jpg

77.6 KB
Loading

examples/files/mask.png

3.85 KB
Loading

examples/files/stuff-template.png

5.69 KB
Loading

examples/files/tmpl.png

5.77 KB
Loading

examples/match-template-mask.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// see https://github.com/Itseez/opencv/blob/master/samples/cpp/mask_tmpl.cpp
2+
const cv = require('../lib/opencv');
3+
4+
const TM_CCORR_NORMED = 3
5+
const imageFilename = 'files/lena_tmpl.jpg';
6+
const templateFilename = 'files/tmpl.png';
7+
const maskFilename = 'files/mask.png';
8+
const outFilename = 'tmp/match-template-mask.png';
9+
10+
cv.readImage(imageFilename, function(err, im) {
11+
if (err) throw err;
12+
cv.readImage(templateFilename, function (err, tmpl) {
13+
if (err) throw err;
14+
const res = im.matchTemplate(templateFilename, TM_CCORR_NORMED, maskFilename);
15+
const minMax = res.minMaxLoc();
16+
const topLeft = minMax.maxLoc;
17+
im.rectangle([topLeft.x, topLeft.y], [tmpl.width(), tmpl.height()], [0, 255,0], 2);
18+
19+
im.save(outFilename);
20+
console.log('Image saved to ' + outFilename);
21+
});
22+
});

examples/match-template.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const cv = require('../lib/opencv');
2+
3+
const TM_CCORR_NORMED = 3;
4+
const imageFilename = 'files/stuff.png';
5+
const templateFilename = 'files/stuff-template.png';
6+
const outFilename = 'tmp/match-template.png';
7+
8+
cv.readImage(imageFilename, function (err, im) {
9+
if (err) throw err;
10+
cv.readImage(templateFilename, function (err, tmpl) {
11+
if (err) throw err;
12+
const res = im.matchTemplate(templateFilename, TM_CCORR_NORMED);
13+
const minMax = res.minMaxLoc();
14+
const topLeft = minMax.maxLoc;
15+
im.rectangle([topLeft.x, topLeft.y], [tmpl.width(), tmpl.height()], [0, 255,0], 2);
16+
17+
im.save(outFilename);
18+
console.log('Image saved to ' + outFilename);
19+
});
20+
});

src/Matrix.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,7 @@ NAN_METHOD(Matrix::TemplateMatches) {
22002200

22012201
// @author ytham
22022202
// Match Template filter
2203-
// Usage: output = input.matchTemplate("templateFileString", method);
2203+
// Usage: output = input.matchTemplate("templateFileString"[, "method"][, "maskPath"]);
22042204
NAN_METHOD(Matrix::MatchTemplate) {
22052205
Nan::HandleScope scope;
22062206

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

2232+
if (info.Length() < 3) {
2233+
cv::matchTemplate(self->mat, templ, m_out->mat, method);
2234+
} else {
2235+
v8::String::Utf8Value args2(info[2]->ToString());
2236+
std::string maskFilename = std::string(*args2);
2237+
cv::Mat mask;
2238+
mask = cv::imread(maskFilename, CV_8S);
2239+
if (mask.size().height == 0 && mask.size().width == 0) {
2240+
throw std::runtime_error(("Failed to load mask file: " + maskFilename).c_str());
2241+
}
2242+
cv::matchTemplate(self->mat, templ, m_out->mat, method, mask);
2243+
}
2244+
22322245
info.GetReturnValue().Set(out);
22332246
}
22342247

0 commit comments

Comments
 (0)