Add match temple method

Add match temple method that accept a matrix
This commit is contained in:
Sen 2016-07-25 13:16:32 -04:00
parent 870ffb56c2
commit d8de6a86b8
2 changed files with 75 additions and 7 deletions

View File

@ -93,6 +93,7 @@ void Matrix::Init(Local<Object> target) {
Nan::SetPrototypeMethod(ctor, "equalizeHist", EqualizeHist);
Nan::SetPrototypeMethod(ctor, "floodFill", FloodFill);
Nan::SetPrototypeMethod(ctor, "matchTemplate", MatchTemplate);
Nan::SetPrototypeMethod(ctor, "matchTemplateByMatrix", MatchTemplateByMatrix);
Nan::SetPrototypeMethod(ctor, "templateMatches", TemplateMatches);
Nan::SetPrototypeMethod(ctor, "minMaxLoc", MinMaxLoc);
Nan::SetPrototypeMethod(ctor, "pushBack", PushBack);
@ -2240,6 +2241,72 @@ NAN_METHOD(Matrix::TemplateMatches) {
info.GetReturnValue().Set(probabilites_array);
}
// @author Evilcat325
// MatchTemplate accept a Matrix
// Usage: output = input.matchTemplateByMatrix(matrix. method);
NAN_METHOD(Matrix::MatchTemplateByMatrix) {
Nan::HandleScope scope;
Matrix *self = Nan::ObjectWrap::Unwrap<Matrix>(info.This());
Matrix *templ = Nan::ObjectWrap::Unwrap<Matrix>(info[0]->ToObject());
Local<Object> out = Nan::New(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *m_out = Nan::ObjectWrap::Unwrap<Matrix>(out);
int cols = self->mat.cols - templ->mat.cols + 1;
int rows = self->mat.rows - templ->mat.rows + 1;
m_out->mat.create(cols, rows, CV_32FC1);
/*
TM_SQDIFF =0
TM_SQDIFF_NORMED =1
TM_CCORR =2
TM_CCORR_NORMED =3
TM_CCOEFF =4
TM_CCOEFF_NORMED =5
*/
int method = (info.Length() < 2) ? (int)cv::TM_CCORR_NORMED : info[1]->Uint32Value();
cv::matchTemplate(self->mat, templ->mat, 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;
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->mat.cols;
unsigned int roi_height = templ->mat.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
// Match Template filter
// Usage: output = input.matchTemplate("templateFileString", method);
@ -2271,19 +2338,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);
cv::normalize(m_out->mat, m_out->mat, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
double minVal;
double maxVal;
cv::Point minLoc;
double minVal;
double maxVal;
cv::Point minLoc;
cv::Point maxLoc;
cv::Point matchLoc;
minMaxLoc(m_out->mat, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());
if(method == CV_TM_SQDIFF || method == CV_TM_SQDIFF_NORMED) {
matchLoc = minLoc;
if(method == CV_TM_SQDIFF || method == CV_TM_SQDIFF_NORMED) {
matchLoc = minLoc;
}
else {
matchLoc = maxLoc;
else {
matchLoc = maxLoc;
}
//detected ROI

View File

@ -109,6 +109,7 @@ public:
JSFUNC(FloodFill)
JSFUNC(MatchTemplate)
JSFUNC(MatchTemplateByMatrix)
JSFUNC(TemplateMatches)
JSFUNC(MinMaxLoc)