Skip to content

Commit 5aee66e

Browse files
authored
Merge pull request #428 from Evilcat325/master
Add MatchTemplate By Matrix Method.
2 parents 72e3921 + e084213 commit 5aee66e

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

src/Matrix.cc

+38-7
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void Matrix::Init(Local<Object> target) {
9393
Nan::SetPrototypeMethod(ctor, "equalizeHist", EqualizeHist);
9494
Nan::SetPrototypeMethod(ctor, "floodFill", FloodFill);
9595
Nan::SetPrototypeMethod(ctor, "matchTemplate", MatchTemplate);
96+
Nan::SetPrototypeMethod(ctor, "matchTemplateByMatrix", MatchTemplateByMatrix);
9697
Nan::SetPrototypeMethod(ctor, "templateMatches", TemplateMatches);
9798
Nan::SetPrototypeMethod(ctor, "minMaxLoc", MinMaxLoc);
9899
Nan::SetPrototypeMethod(ctor, "pushBack", PushBack);
@@ -2246,6 +2247,36 @@ NAN_METHOD(Matrix::TemplateMatches) {
22462247
info.GetReturnValue().Set(probabilites_array);
22472248
}
22482249

2250+
// @author Evilcat325
2251+
// MatchTemplate accept a Matrix
2252+
// Usage: output = input.matchTemplateByMatrix(matrix. method);
2253+
NAN_METHOD(Matrix::MatchTemplateByMatrix) {
2254+
Nan::HandleScope scope;
2255+
2256+
Matrix *self = Nan::ObjectWrap::Unwrap<Matrix>(info.This());
2257+
Matrix *templ = Nan::ObjectWrap::Unwrap<Matrix>(info[0]->ToObject());
2258+
2259+
Local<Object> out = Nan::New(Matrix::constructor)->GetFunction()->NewInstance();
2260+
Matrix *m_out = Nan::ObjectWrap::Unwrap<Matrix>(out);
2261+
int cols = self->mat.cols - templ->mat.cols + 1;
2262+
int rows = self->mat.rows - templ->mat.rows + 1;
2263+
m_out->mat.create(cols, rows, CV_32FC1);
2264+
2265+
/*
2266+
TM_SQDIFF =0
2267+
TM_SQDIFF_NORMED =1
2268+
TM_CCORR =2
2269+
TM_CCORR_NORMED =3
2270+
TM_CCOEFF =4
2271+
TM_CCOEFF_NORMED =5
2272+
*/
2273+
2274+
int method = (info.Length() < 2) ? (int)cv::TM_CCORR_NORMED : info[1]->Uint32Value();
2275+
if (!(method >= 0 && method <= 5)) method = (int)cv::TM_CCORR_NORMED;
2276+
cv::matchTemplate(self->mat, templ->mat, m_out->mat, method);
2277+
info.GetReturnValue().Set(out);
2278+
}
2279+
22492280
// @author ytham
22502281
// Match Template filter
22512282
// Usage: output = input.matchTemplate("templateFileString", method);
@@ -2277,19 +2308,19 @@ NAN_METHOD(Matrix::MatchTemplate) {
22772308
int method = (info.Length() < 2) ? (int)cv::TM_CCORR_NORMED : info[1]->Uint32Value();
22782309
cv::matchTemplate(self->mat, templ, m_out->mat, method);
22792310
cv::normalize(m_out->mat, m_out->mat, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
2280-
double minVal;
2281-
double maxVal;
2282-
cv::Point minLoc;
2311+
double minVal;
2312+
double maxVal;
2313+
cv::Point minLoc;
22832314
cv::Point maxLoc;
22842315
cv::Point matchLoc;
22852316

22862317
minMaxLoc(m_out->mat, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());
22872318

2288-
if(method == CV_TM_SQDIFF || method == CV_TM_SQDIFF_NORMED) {
2289-
matchLoc = minLoc;
2319+
if(method == CV_TM_SQDIFF || method == CV_TM_SQDIFF_NORMED) {
2320+
matchLoc = minLoc;
22902321
}
2291-
else {
2292-
matchLoc = maxLoc;
2322+
else {
2323+
matchLoc = maxLoc;
22932324
}
22942325

22952326
//detected ROI

src/Matrix.h

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class Matrix: public node_opencv::Matrix{
109109
JSFUNC(FloodFill)
110110

111111
JSFUNC(MatchTemplate)
112+
JSFUNC(MatchTemplateByMatrix)
112113
JSFUNC(TemplateMatches)
113114
JSFUNC(MinMaxLoc)
114115

test/unit.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ test(".norm", function(assert){
127127

128128
var errorL2 = im.norm(im2, cv.Constants.NORM_L2);
129129
assert.equal(errorL2, 7295.591339980605);
130-
131130
errorL2 = im.norm(im, cv.Constants.NORM_L2);
132131
assert.equal(errorL2, 0);
133132
assert.end();
@@ -366,7 +365,31 @@ test('Mean', function(assert) {
366365
assert.end();
367366
});
368367

368+
test('MatchTemplateByMatrix', function(assert) {
369+
var cv = require('../lib/opencv');
370+
var targetFilename = "./examples/files/car1.jpg";
371+
var templateFilename = "./examples/files/car1_template.jpg";
372+
cv.readImage(targetFilename, function(err, target){
373+
cv.readImage(templateFilename, function(err, template){
374+
var TM_CCORR_NORMED = 3;
375+
var res = target.matchTemplateByMatrix(template, TM_CCORR_NORMED);
376+
var minMax = res.minMaxLoc();
377+
var topLeft = minMax.maxLoc;
378+
assert.ok(topLeft, "RGB Found Match");
379+
assert.equal(topLeft.x, 42, "match location x === 42");
380+
assert.equal(topLeft.y, 263, "match location y === 263");
381+
target.canny(5,300);
382+
template.canny(5,300);
383+
res = target.matchTemplateByMatrix(template, TM_CCORR_NORMED);
384+
minMax = res.minMaxLoc();
385+
topLeft = minMax.maxLoc;
386+
assert.ok(topLeft, "Canny edge Found Match");
387+
assert.equal(topLeft.x, 42, "match location x === 42");
388+
assert.equal(topLeft.y, 263, "match location y === 263");
389+
assert.end();
390+
});
391+
})
392+
});
393+
369394
// Test the examples folder.
370395
require('./examples')()
371-
372-

0 commit comments

Comments
 (0)