Skip to content

Commit 49b5f34

Browse files
committed
Merge pull request #389 from salmanulhaq/matchtemplateupdate
Updated matchTemplate and added an example
2 parents ead733b + 5102116 commit 49b5f34

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

examples/files/car1_template.jpg

22.4 KB
Loading

src/Matrix.cc

+38-2
Original file line numberDiff line numberDiff line change
@@ -2249,7 +2249,7 @@ NAN_METHOD(Matrix::MatchTemplate) {
22492249
v8::String::Utf8Value args0(info[0]->ToString());
22502250
std::string filename = std::string(*args0);
22512251
cv::Mat templ;
2252-
templ = cv::imread(filename, CV_8S);
2252+
templ = cv::imread(filename, -1);
22532253

22542254
Local<Object> out = Nan::New(Matrix::constructor)->GetFunction()->NewInstance();
22552255
Matrix *m_out = Nan::ObjectWrap::Unwrap<Matrix>(out);
@@ -2268,8 +2268,44 @@ NAN_METHOD(Matrix::MatchTemplate) {
22682268

22692269
int method = (info.Length() < 2) ? (int)cv::TM_CCORR_NORMED : info[1]->Uint32Value();
22702270
cv::matchTemplate(self->mat, templ, m_out->mat, method);
2271+
cv::normalize(m_out->mat, m_out->mat, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
2272+
double minVal;
2273+
double maxVal;
2274+
cv::Point minLoc;
2275+
cv::Point maxLoc;
2276+
cv::Point matchLoc;
22712277

2272-
info.GetReturnValue().Set(out);
2278+
minMaxLoc(m_out->mat, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());
2279+
2280+
if(method == CV_TM_SQDIFF || method == CV_TM_SQDIFF_NORMED) {
2281+
matchLoc = minLoc;
2282+
}
2283+
else {
2284+
matchLoc = maxLoc;
2285+
}
2286+
2287+
//detected ROI
2288+
unsigned int roi_x = matchLoc.x;
2289+
unsigned int roi_y = matchLoc.y;
2290+
unsigned int roi_width = templ.cols;
2291+
unsigned int roi_height = templ.rows;
2292+
2293+
//draw rectangle
2294+
if(info.Length() >= 3) {
2295+
cv::Rect roi(roi_x,roi_y,roi_width,roi_height);
2296+
cv::rectangle(self->mat, roi, cv::Scalar(0,0,255));
2297+
}
2298+
2299+
m_out->mat.convertTo(m_out->mat, CV_8UC1, 255, 0);
2300+
2301+
v8::Local <v8::Array> arr = Nan::New<v8::Array>(5);
2302+
arr->Set(0, out);
2303+
arr->Set(1, Nan::New<Number>(roi_x));
2304+
arr->Set(2, Nan::New<Number>(roi_y));
2305+
arr->Set(3, Nan::New<Number>(roi_width));
2306+
arr->Set(4, Nan::New<Number>(roi_height));
2307+
2308+
info.GetReturnValue().Set(arr);
22732309
}
22742310

22752311
// @author ytham

0 commit comments

Comments
 (0)