@@ -2249,7 +2249,7 @@ NAN_METHOD(Matrix::MatchTemplate) {
2249
2249
v8::String::Utf8Value args0 (info[0 ]->ToString ());
2250
2250
std::string filename = std::string (*args0);
2251
2251
cv::Mat templ;
2252
- templ = cv::imread (filename, CV_8S );
2252
+ templ = cv::imread (filename, - 1 );
2253
2253
2254
2254
Local<Object> out = Nan::New (Matrix::constructor)->GetFunction ()->NewInstance ();
2255
2255
Matrix *m_out = Nan::ObjectWrap::Unwrap<Matrix>(out);
@@ -2268,8 +2268,44 @@ NAN_METHOD(Matrix::MatchTemplate) {
2268
2268
2269
2269
int method = (info.Length () < 2 ) ? (int )cv::TM_CCORR_NORMED : info[1 ]->Uint32Value ();
2270
2270
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;
2271
2277
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);
2273
2309
}
2274
2310
2275
2311
// @author ytham
0 commit comments