Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated matchTemplate and added an example #389

Merged
merged 1 commit into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/files/car1_template.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 38 additions & 2 deletions src/Matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2249,7 +2249,7 @@ NAN_METHOD(Matrix::MatchTemplate) {
v8::String::Utf8Value args0(info[0]->ToString());
std::string filename = std::string(*args0);
cv::Mat templ;
templ = cv::imread(filename, CV_8S);
templ = cv::imread(filename, -1);

Local<Object> out = Nan::New(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *m_out = Nan::ObjectWrap::Unwrap<Matrix>(out);
Expand All @@ -2268,8 +2268,44 @@ 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);
cv::normalize(m_out->mat, m_out->mat, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
double minVal;
double maxVal;
cv::Point minLoc;
cv::Point maxLoc;
cv::Point matchLoc;

info.GetReturnValue().Set(out);
minMaxLoc(m_out->mat, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());

if(method == CV_TM_SQDIFF || method == CV_TM_SQDIFF_NORMED) {
matchLoc = minLoc;
}
else {
matchLoc = maxLoc;
}

//detected ROI
unsigned int roi_x = matchLoc.x;
unsigned int roi_y = matchLoc.y;
unsigned int roi_width = templ.cols;
unsigned int roi_height = templ.rows;

//draw rectangle
if(info.Length() >= 3) {
cv::Rect roi(roi_x,roi_y,roi_width,roi_height);
cv::rectangle(self->mat, roi, cv::Scalar(0,0,255));
}

m_out->mat.convertTo(m_out->mat, CV_8UC1, 255, 0);

v8::Local <v8::Array> arr = Nan::New<v8::Array>(5);
arr->Set(0, out);
arr->Set(1, Nan::New<Number>(roi_x));
arr->Set(2, Nan::New<Number>(roi_y));
arr->Set(3, Nan::New<Number>(roi_width));
arr->Set(4, Nan::New<Number>(roi_height));

info.GetReturnValue().Set(arr);
}

// @author ytham
Expand Down