show.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 
00030 #include <swri_opencv_util/show.h>
00031 
00032 #include <map>
00033 #include <string>
00034 
00035 #include <boost/serialization/singleton.hpp>
00036 #include <boost/thread/mutex.hpp>
00037 
00038 #include <opencv2/highgui/highgui.hpp>
00039 #include <opencv2/imgproc/imgproc.hpp>
00040 
00041 #include <ros/ros.h>
00042 
00043 namespace swri_opencv_util
00044 {
00045   class CvWindows
00046   {
00047   public:
00048     ~CvWindows() {}
00049 
00050     void RegisterWindow(const std::string& name)
00051     {
00052       boost::unique_lock<boost::mutex> lock(mutex_);
00053 
00054       if (windows_.empty())
00055       {
00056         cvStartWindowThread();
00057       }
00058 
00059       if (windows_.count(name) == 0)
00060       {
00061         windows_[name] = name;
00062 
00063         cvNamedWindow(name.c_str(), CV_WINDOW_NORMAL);
00064       }
00065     }
00066 
00067 #if (BOOST_VERSION / 100 % 1000) >= 65
00068     friend class boost::serialization::singleton<CvWindows>;
00069 #else
00070     friend class boost::serialization::detail::singleton_wrapper<CvWindows>;
00071 #endif
00072   private:
00073     CvWindows() {}
00074     boost::mutex mutex_;
00075     std::map<std::string, std::string> windows_;
00076   };
00077   typedef boost::serialization::singleton<CvWindows> CvWindowsSingleton;
00078 
00079   void ShowScaled(
00080       const std::string& name,
00081       const cv::Mat& mat,
00082       const cv::Mat& mask,
00083       double a,
00084       double b)
00085   {
00086     if (mat.empty())
00087     {
00088       return;
00089     }
00090 
00091     CvWindowsSingleton::get_mutable_instance().RegisterWindow(name);
00092 
00093     cv::Mat scaled;
00094 
00095     // Autoscale if a is negative
00096     if(a < 0.0)
00097     {
00098       double min, max;
00099       cv::minMaxLoc(mat, &min, &max, 0, 0, mask);
00100 
00101       if(mat.type() == CV_8UC1)
00102       {
00103         a = 255.0 / std::max(max - min, DBL_EPSILON);
00104         b = -min * a;
00105         mat.convertTo(scaled, CV_8U, a, b);
00106       }
00107       else if(mat.type() == CV_32FC1)
00108       {
00109         a = 255.0 / std::max(max - min, DBL_EPSILON);
00110         b = -min * a;
00111         mat.convertTo(scaled, CV_8U, a, b);
00112         if (!mask.empty())
00113         {
00114           cv::Mat color;
00115           cv::cvtColor(scaled, color, CV_GRAY2BGR);
00116           color.setTo(cv::Scalar(0.0,0.0,255.0), mask == 0);
00117           scaled = color;
00118         }
00119       }
00120       else if(mat.type() == CV_32FC3)
00121       {
00122         a = 255.0 / std::max(max - min, DBL_EPSILON);
00123         b = -min * a;
00124         mat.convertTo(scaled, CV_8UC3, a, b);
00125       }
00126       else if(mat.type() == CV_8UC3)
00127       {
00128         a = 255.0 / std::max(max - min, DBL_EPSILON);
00129         b = -min * a;
00130         mat.convertTo(scaled, CV_8UC3, a, b);
00131       }
00132     }
00133     else
00134     {
00135       mat.convertTo(scaled, CV_8U, a, b);
00136     }
00137 
00138     cv::imshow(name, scaled);
00139   }
00140 }
00141 


swri_opencv_util
Author(s): Marc Alban
autogenerated on Thu Jun 6 2019 20:34:45