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     friend class boost::serialization::detail::singleton_wrapper<CvWindows>;
00068   private:
00069     CvWindows() {}
00070     boost::mutex mutex_;
00071     std::map<std::string, std::string> windows_;
00072   };
00073   typedef boost::serialization::singleton<CvWindows> CvWindowsSingleton;
00074 
00075   void ShowScaled(
00076       const std::string& name,
00077       const cv::Mat& mat,
00078       const cv::Mat& mask,
00079       double a,
00080       double b)
00081   {
00082     if (mat.empty())
00083     {
00084       return;
00085     }
00086 
00087     CvWindowsSingleton::get_mutable_instance().RegisterWindow(name);
00088 
00089     cv::Mat scaled;
00090 
00091     // Autoscale if a is negative
00092     if(a < 0.0)
00093     {
00094       double min, max;
00095       cv::minMaxLoc(mat, &min, &max, 0, 0, mask);
00096 
00097       if(mat.type() == CV_8UC1)
00098       {
00099         a = 255.0 / std::max(max - min, DBL_EPSILON);
00100         b = -min * a;
00101         mat.convertTo(scaled, CV_8U, a, b);
00102       }
00103       else if(mat.type() == CV_32FC1)
00104       {
00105         a = 255.0 / std::max(max - min, DBL_EPSILON);
00106         b = -min * a;
00107         mat.convertTo(scaled, CV_8U, a, b);
00108         if (!mask.empty())
00109         {
00110           cv::Mat color;
00111           cv::cvtColor(scaled, color, CV_GRAY2BGR);
00112           color.setTo(cv::Scalar(0.0,0.0,255.0), mask == 0);
00113           scaled = color;
00114         }
00115       }
00116       else if(mat.type() == CV_32FC3)
00117       {
00118         a = 255.0 / std::max(max - min, DBL_EPSILON);
00119         b = -min * a;
00120         mat.convertTo(scaled, CV_8UC3, a, b);
00121       }
00122       else if(mat.type() == CV_8UC3)
00123       {
00124         a = 255.0 / std::max(max - min, DBL_EPSILON);
00125         b = -min * a;
00126         mat.convertTo(scaled, CV_8UC3, a, b);
00127       }
00128     }
00129     else
00130     {
00131       mat.convertTo(scaled, CV_8U, a, b);
00132     }
00133 
00134     cv::imshow(name, scaled);
00135   }
00136 }
00137 


swri_opencv_util
Author(s): Marc Alban
autogenerated on Tue Oct 3 2017 03:19:24