show.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #include <swri_opencv_util/show.h>
31 
32 #include <map>
33 #include <string>
34 
35 #include <boost/serialization/singleton.hpp>
36 #include <boost/thread/mutex.hpp>
37 
38 #include <opencv2/highgui/highgui.hpp>
39 #include <opencv2/imgproc/imgproc.hpp>
40 
41 #include <ros/ros.h>
42 
43 namespace swri_opencv_util
44 {
45  class CvWindows
46  {
47  public:
49 
50  void RegisterWindow(const std::string& name)
51  {
52  boost::unique_lock<boost::mutex> lock(mutex_);
53 
54  if (windows_.empty())
55  {
56  cv::startWindowThread();
57  }
58 
59  if (windows_.count(name) == 0)
60  {
61  windows_[name] = name;
62 
63  cv::namedWindow(name.c_str(), cv::WINDOW_NORMAL);
64  }
65  }
66 
67 #if (BOOST_VERSION / 100 % 1000) >= 65 && (BOOST_VERSION / 100 % 1000) < 69
68  friend class boost::serialization::singleton<CvWindows>;
69 #else
70  friend class boost::serialization::detail::singleton_wrapper<CvWindows>;
71 #endif
72  private:
73  CvWindows() {}
74  boost::mutex mutex_;
75  std::map<std::string, std::string> windows_;
76  };
77  typedef boost::serialization::singleton<CvWindows> CvWindowsSingleton;
78 
79  void ShowScaled(
80  const std::string& name,
81  const cv::Mat& mat,
82  const cv::Mat& mask,
83  double a,
84  double b)
85  {
86  if (mat.empty())
87  {
88  return;
89  }
90 
91  CvWindowsSingleton::get_mutable_instance().RegisterWindow(name);
92 
93  cv::Mat scaled;
94 
95  // Autoscale if a is negative
96  if(a < 0.0)
97  {
98  double min, max;
99  cv::minMaxLoc(mat, &min, &max, 0, 0, mask);
100 
101  if(mat.type() == CV_8UC1)
102  {
103  a = 255.0 / std::max(max - min, DBL_EPSILON);
104  b = -min * a;
105  mat.convertTo(scaled, CV_8U, a, b);
106  }
107  else if(mat.type() == CV_32FC1)
108  {
109  a = 255.0 / std::max(max - min, DBL_EPSILON);
110  b = -min * a;
111  mat.convertTo(scaled, CV_8U, a, b);
112  if (!mask.empty())
113  {
114  cv::Mat color;
115  cv::cvtColor(scaled, color, cv::COLOR_GRAY2BGR);
116  color.setTo(cv::Scalar(0.0,0.0,255.0), mask == 0);
117  scaled = color;
118  }
119  }
120  else if(mat.type() == CV_32FC3)
121  {
122  a = 255.0 / std::max(max - min, DBL_EPSILON);
123  b = -min * a;
124  mat.convertTo(scaled, CV_8UC3, a, b);
125  }
126  else if(mat.type() == CV_8UC3)
127  {
128  a = 255.0 / std::max(max - min, DBL_EPSILON);
129  b = -min * a;
130  mat.convertTo(scaled, CV_8UC3, a, b);
131  }
132  }
133  else
134  {
135  mat.convertTo(scaled, CV_8U, a, b);
136  }
137 
138  cv::imshow(name, scaled);
139  }
140 }
141 
swri_opencv_util::CvWindows::CvWindows
CvWindows()
Definition: show.cpp:73
swri_opencv_util::CvWindowsSingleton
boost::serialization::singleton< CvWindows > CvWindowsSingleton
Definition: show.cpp:77
ros.h
swri_opencv_util::CvWindows
Definition: show.cpp:45
swri_opencv_util::CvWindows::windows_
std::map< std::string, std::string > windows_
Definition: show.cpp:75
swri_opencv_util
Definition: blend.h:35
swri_opencv_util::CvWindows::RegisterWindow
void RegisterWindow(const std::string &name)
Definition: show.cpp:50
show.h
swri_opencv_util::CvWindows::~CvWindows
~CvWindows()
Definition: show.cpp:48
swri_opencv_util::CvWindows::mutex_
boost::mutex mutex_
Definition: show.cpp:74
swri_opencv_util::ShowScaled
void ShowScaled(const std::string &name, const cv::Mat &mat, const cv::Mat &mask=cv::Mat(), double a=-1.0, double b=0.0)
Definition: show.cpp:79


swri_opencv_util
Author(s): Marc Alban
autogenerated on Fri Aug 2 2024 08:39:13