From 056b6cd68e5c80c86b1877fe8234fd7acc0a6190 Mon Sep 17 00:00:00 2001 From: Oskar Dahlberg Date: Thu, 11 Sep 2014 16:59:22 +0200 Subject: [PATCH] New method for filtering template matches --- src/Matrix.cc | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/Matrix.h | 1 + 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 8dfc669..e338449 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -96,7 +96,8 @@ Matrix::Init(Handle target) { NODE_SET_PROTOTYPE_METHOD(constructor, "floodFill", FloodFill); - NODE_SET_PROTOTYPE_METHOD(constructor, "matchTemplate", MatchTemplate); + NODE_SET_PROTOTYPE_METHOD(constructor, "matchTemplate", MatchTemplate); + NODE_SET_PROTOTYPE_METHOD(constructor, "templateMatches", TemplateMatches); NODE_SET_PROTOTYPE_METHOD(constructor, "minMaxLoc", MinMaxLoc); NODE_SET_PROTOTYPE_METHOD(constructor, "pushBack", PushBack); @@ -1719,6 +1720,87 @@ Matrix::FloodFill(const Arguments& args){ return scope.Close(Number::New( ret )); } +// @author olfox +// Returns an array of the most probable positions +// Usage: output = input.templateMatches(min_probability, max_probability, limit, ascending, min_x_distance, min_y_distance); +Handle +Matrix::TemplateMatches(const v8::Arguments& args) { + HandleScope scope; + + Matrix *self = ObjectWrap::Unwrap(args.This()); + + bool filter_min_probability = (args.Length() >= 1) ? args[0]->IsNumber() : false; + bool filter_max_probability = (args.Length() >= 2) ? args[1]->IsNumber() : false; + double min_probability = filter_min_probability ? args[0]->NumberValue() : 0; + double max_probability = filter_max_probability ? args[1]->NumberValue() : 0; + int limit = (args.Length() >= 3) ? args[2]->IntegerValue() : 0; + bool ascending = (args.Length() >= 4) ? args[3]->BooleanValue() : false; + int min_x_distance = (args.Length() >= 5) ? args[4]->IntegerValue() : 0; + int min_y_distance = (args.Length() >= 6) ? args[5]->IntegerValue() : 0; + + cv::Mat_ indices; + + if (ascending) + cv::sortIdx(self->mat.reshape(0,1), indices, CV_SORT_ASCENDING + CV_SORT_EVERY_ROW); + else + cv::sortIdx(self->mat.reshape(0,1), indices, CV_SORT_DESCENDING + CV_SORT_EVERY_ROW); + + cv::Mat hit_mask = cv::Mat::zeros(self->mat.size(), CV_64F); + v8::Local probabilites_array = v8::Array::New(); + + cv::Mat_::const_iterator begin = self->mat.begin(); + cv::Mat_::const_iterator it = indices.begin(); + cv::Mat_::const_iterator end = indices.end(); + int index = 0; + for (; (limit == 0 || index < limit) && it != end; ++it) { + cv::Point pt = (begin + *it).pos(); + + float probability = self->mat.at(pt.y, pt.x); + + if (filter_min_probability && probability < min_probability) { + if (ascending) continue; + else break; + } + + if (filter_max_probability && probability > max_probability) { + if (ascending) break; + else continue; + } + + if (min_x_distance != 0 || min_y_distance != 0) { + // Check hit mask color for for every corner + + cv::Size maxSize = hit_mask.size(); + int max_x = maxSize.width - 1; + int max_y = maxSize.height - 1; + cv::Point top_left = cv::Point(max(0, pt.x - min_x_distance), max(0, pt.y - min_y_distance)); + cv::Point top_right = cv::Point(min(max_x, pt.x + min_x_distance), max(0, pt.y - min_y_distance)); + cv::Point bottom_left = cv::Point(max(0, pt.x - min_x_distance), min(max_y, pt.y + min_y_distance)); + cv::Point bottom_right = cv::Point(min(max_x, pt.x + min_x_distance), min(max_y, pt.y + min_y_distance)); + if (hit_mask.at(top_left.y, top_left.x) > 0) continue; + if (hit_mask.at(top_right.y, top_right.x) > 0) continue; + if (hit_mask.at(bottom_left.y, bottom_left.x) > 0) continue; + if (hit_mask.at(bottom_right.y, bottom_right.x) > 0) continue; + cv::Scalar color(255.0); + cv::rectangle(hit_mask, top_left, bottom_right, color, CV_FILLED); + } + + Local x_value = v8::Number::New(pt.x); + Local y_value = v8::Number::New(pt.y); + Local probability_value = v8::Number::New(probability); + + Local probability_object = Object::New(); + probability_object->Set(String::NewSymbol("x"), x_value); + probability_object->Set(String::NewSymbol("y"), y_value); + probability_object->Set(String::NewSymbol("probability"), probability_value); + + probabilites_array->Set(index, probability_object); + index++; + } + + return scope.Close(probabilites_array); +} + // @author ytham // Match Template filter // Usage: output = input.matchTemplate(templateMatrix, method); diff --git a/src/Matrix.h b/src/Matrix.h index 3331e44..083ac6a 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -91,6 +91,7 @@ class Matrix: public node::ObjectWrap { JSFUNC(FloodFill) JSFUNC(MatchTemplate) + JSFUNC(TemplateMatches) JSFUNC(MinMaxLoc) JSFUNC(PushBack)