diff --git a/examples/files/lena_tmpl.jpg b/examples/files/lena_tmpl.jpg new file mode 100644 index 00000000..0c9fc20d Binary files /dev/null and b/examples/files/lena_tmpl.jpg differ diff --git a/examples/files/mask.png b/examples/files/mask.png new file mode 100644 index 00000000..0666232d Binary files /dev/null and b/examples/files/mask.png differ diff --git a/examples/files/stuff-template.png b/examples/files/stuff-template.png new file mode 100644 index 00000000..9b598c1e Binary files /dev/null and b/examples/files/stuff-template.png differ diff --git a/examples/files/tmpl.png b/examples/files/tmpl.png new file mode 100644 index 00000000..999ac704 Binary files /dev/null and b/examples/files/tmpl.png differ diff --git a/examples/match-template-mask.js b/examples/match-template-mask.js new file mode 100644 index 00000000..c79ce102 --- /dev/null +++ b/examples/match-template-mask.js @@ -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); + }); +}); diff --git a/examples/match-template.js b/examples/match-template.js new file mode 100644 index 00000000..d533362e --- /dev/null +++ b/examples/match-template.js @@ -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); + }); +}); diff --git a/src/Matrix.cc b/src/Matrix.cc index 51525ff1..7e4dba1b 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -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; @@ -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); }