11/* ********************************************************************
2- * Software License Agreement (BSD License)
3- *
4- * Copyright (c) 2017, Ryosuke Tajima.
5- * All rights reserved.
6- *
7- * Redistribution and use in source and binary forms, with or without
8- * modification, are permitted provided that the following conditions
9- * are met:
10- *
11- * * Redistributions of source code must retain the above copyright
12- * notice, this list of conditions and the following disclaimer.
13- * * Redistributions in binary form must reproduce the above
14- * copyright notice, this list of conditions and the following
15- * disclaimer in the documentation and/or other materials provided
16- * with the distribution.
17- * * Neither the name of the Ryosuke Tajima nor the names of its
18- * contributors may be used to endorse or promote products derived
19- * from this software without specific prior written permission.
20- *
21- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32- * POSSIBILITY OF SUCH DAMAGE.
33- *********************************************************************/
2+ * Software License Agreement (BSD License)
3+ *
4+ * Copyright (c) 2017, Ryosuke Tajima.
5+ * All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * * Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * * Redistributions in binary form must reproduce the above
14+ * copyright notice, this list of conditions and the following
15+ * disclaimer in the documentation and/or other materials provided
16+ * with the distribution.
17+ * * Neither the name of the Ryosuke Tajima nor the names of its
18+ * contributors may be used to endorse or promote products derived
19+ * from this software without specific prior written permission.
20+ *
21+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+ * POSSIBILITY OF SUCH DAMAGE.
33+ *********************************************************************/
3434
3535// https://github.com/opencv/opencv/raw/master/samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp
36+ /* *
37+ * @file MatchTemplate_Demo.cpp
38+ * @brief Sample code to use the function MatchTemplate
39+ * @author OpenCV team
40+ */
41+
42+ #include < ros/ros.h>
43+ #include " opencv_apps/nodelet.h"
44+ #include < image_transport/image_transport.h>
45+ #include < cv_bridge/cv_bridge.h>
46+ #include < sensor_msgs/image_encodings.h>
47+
3648#include < opencv2/highgui/highgui.hpp>
3749#include < opencv2/imgproc/imgproc.hpp>
3850#include < opencv2/imgproc/types_c.h>
@@ -70,6 +82,11 @@ class MatchTemplateNodelet:public opencv_apps::Nodelet
7082 bool debug_view_;
7183 ros::Time prev_stamp_;
7284 cv::Mat templ_;
85+ // mask is only supported since opencv 3.2 (https://github.com/opencv/opencv/pull/3554)
86+ #if (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 2)
87+ bool use_mask_;
88+ cv::Mat mask_;
89+ #endif
7390
7491 std::string scene_window_name_, templ_window_name_;
7592 static bool mouse_update_;
@@ -164,7 +181,17 @@ class MatchTemplateNodelet:public opencv_apps::Nodelet
164181 mouse_update_ = false ;
165182 }
166183 // Match template
167- matchTemplate (frame, templ_, score, config_.match_method );
184+ bool method_accepts_mask = (config_.match_method == CV_TM_SQDIFF || config_.match_method == CV_TM_CCORR_NORMED);
185+ #if (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 2)
186+ if (use_mask_ && method_accepts_mask)
187+ {
188+ matchTemplate (frame, templ_, score, config_.match_method , mask_);
189+ }
190+ else
191+ #endif
192+ {
193+ matchTemplate (frame, templ_, score, config_.match_method );
194+ }
168195 // / Localizing the best match with minMaxLoc
169196 int remove_margin_x = templ_.cols / 2 ;
170197 int remove_margin_y = templ_.rows / 2 ;
@@ -290,10 +317,16 @@ class MatchTemplateNodelet:public opencv_apps::Nodelet
290317 pnh_->param (" template_file" , templ_file, std::string (" " ));
291318 if (!templ_file.empty ())
292319 {
293- NODELET_INFO (" template_file: %s\n " , templ_file.c_str ());
294320 templ_ = imread (templ_file, cv::IMREAD_COLOR);
295321 }
296-
322+ #if (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 2)
323+ std::string mask_file;
324+ pnh_->param (" mask_file" , mask_file, std::string (" mask.png" ));
325+ if (use_mask_)
326+ {
327+ mask_ = imread (mask_file, cv::IMREAD_COLOR);
328+ }
329+ #endif
297330 if (debug_view_)
298331 {
299332 always_subscribe_ = true ;
@@ -310,6 +343,7 @@ class MatchTemplateNodelet:public opencv_apps::Nodelet
310343 reconfigure_server_->setCallback (f);
311344
312345 scene_img_pub_ = advertiseImage (*pnh_, " image" , 1 );
346+ // template is global topic, so mouse selection publish it globaly.
313347 templ_img_pub_ = advertiseImage (*nh_, " template" , 1 );
314348 score_img_pub_ = advertiseImage (*pnh_, " score_image" , 1 );
315349 msg_pub_ = advertise < geometry_msgs::PointStamped > (*pnh_, " pixel_position" , 1 );
@@ -318,7 +352,6 @@ class MatchTemplateNodelet:public opencv_apps::Nodelet
318352 onInitPostProcess ();
319353 }
320354};
321- // bool MatchTemplateNodelet::need_config_update_ = false;
322355bool MatchTemplateNodelet::mouse_update_ = false ;
323356int MatchTemplateNodelet::mouse_event_ = 0 ;
324357int MatchTemplateNodelet::mouse_x_ = 0 ;
0 commit comments