convert.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/convert.h>
00031 
00032 #include <opencv2/imgproc/imgproc.hpp>
00033 
00034 #include <ros/ros.h>
00035 
00036 namespace swri_opencv_util
00037 {
00038   cv::Mat ToBgra8(
00039       const cv::Mat& mat,
00040       const cv::Mat& mask,
00041       bool is_rgb,
00042       double a,
00043       double b)
00044   {
00045     if (mat.empty())
00046     {
00047       return mat;
00048     }
00049 
00050     cv::Mat scaled;
00051 
00052     // Autoscale if a zero
00053     if(a == 0)
00054     {
00055       double min, max;
00056       cv::minMaxLoc(mat, &min, &max, 0, 0, mask);
00057 
00058       if(mat.type() == CV_8UC1)
00059       {
00060         a = 255.0 / std::max(max - min, DBL_EPSILON);
00061         b = -min * a;
00062         mat.convertTo(scaled, CV_8U, a, b);
00063 
00064         cv::Mat color;
00065         cv::cvtColor(scaled, color, CV_GRAY2BGRA);
00066         SetAlpha(color, 255);
00067         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
00068         scaled = color;
00069       }
00070       else if(mat.type() == CV_32FC1)
00071       {
00072         a = 255.0 / std::max(max - min, DBL_EPSILON);
00073         b = -min * a;
00074         mat.convertTo(scaled, CV_8U, a, b);
00075  
00076         cv::Mat color;
00077         cv::cvtColor(scaled, color, CV_GRAY2BGRA);
00078         SetAlpha(color, 255);
00079         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
00080         scaled = color;
00081       }
00082       else if(mat.type() == CV_32FC3)
00083       {
00084         a = 255.0 / std::max(max - min, DBL_EPSILON);
00085         b = -min * a;
00086         mat.convertTo(scaled, CV_8UC3, a, b);
00087 
00088         cv::Mat color;
00089         
00090         if (is_rgb)
00091         {
00092           cv::cvtColor(scaled, color, CV_RGB2BGRA);
00093         }
00094         else
00095         {
00096           cv::cvtColor(scaled, color, CV_BGR2BGRA);
00097         }
00098         
00099         SetAlpha(color, 255);
00100         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
00101         scaled = color;
00102       }
00103       else if(mat.type() == CV_8UC3)
00104       {
00105         a = 255.0 / std::max(max - min, DBL_EPSILON);
00106         b = -min * a;
00107         mat.convertTo(scaled, CV_8UC3, a, b);
00108 
00109         cv::Mat color;
00110         
00111         if (is_rgb)
00112         {
00113           cv::cvtColor(scaled, color, CV_RGB2BGRA);
00114         }
00115         else
00116         {
00117           cv::cvtColor(scaled, color, CV_BGR2BGRA);
00118         }
00119         
00120         SetAlpha(color, 255);
00121         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
00122         scaled = color;
00123       }
00124       else if(mat.type() == CV_8UC4)
00125       {
00126         a = 255.0 / std::max(max - min, DBL_EPSILON);
00127         b = -min * a;
00128         mat.convertTo(scaled, CV_8UC4, a, b);
00129 
00130         cv::Mat color;
00131 
00132         if (is_rgb)
00133         {
00134           cv::cvtColor(scaled, color, CV_RGBA2BGRA);
00135         }
00136         else
00137         {
00138           color = scaled;
00139         }
00140 
00141         SetAlpha(color, 255);
00142         if (!mask.empty())
00143         {
00144           color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
00145         }
00146         
00147         scaled = color;
00148       }
00149     }
00150     else
00151     {
00152       if(mat.type() == CV_8UC3)
00153       {
00154         mat.convertTo(scaled, CV_8UC3, a, b);
00155         
00156         cv::Mat color;
00157         if (is_rgb)
00158         {
00159           cv::cvtColor(scaled, color, CV_RGB2BGRA);
00160         }
00161         else
00162         {
00163           cv::cvtColor(scaled, color, CV_BGR2BGRA);
00164         }
00165         
00166         SetAlpha(color, 255);
00167         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
00168         scaled = color;
00169       }
00170       else if(mat.type() == CV_8UC4)
00171       {
00172         mat.convertTo(scaled, CV_8UC4, a, b);
00173         
00174         cv::Mat color;
00175         if (is_rgb)
00176         {
00177           cv::cvtColor(scaled, color, CV_RGBA2BGRA);
00178         }
00179         else
00180         {
00181           color = scaled;
00182         }
00183 
00184         SetAlpha(color, 255);
00185         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
00186         scaled = color;
00187       }
00188       else
00189       {
00190         mat.convertTo(scaled, CV_8U, a, b);
00191 
00192         cv::Mat color;
00193         cv::cvtColor(scaled, color, CV_GRAY2BGRA);
00194         SetAlpha(color, 255);
00195         color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
00196         scaled = color;
00197       }
00198     }
00199     
00200     return scaled;
00201   }
00202   
00203   void SetAlpha(cv::Mat& mat, uint8_t alpha)
00204   {
00205     if (mat.type() == CV_8UC4)
00206     {
00207       for (int r = 0; r < mat.rows; r++)
00208       {
00209         for (int c = 0; c < mat.cols; c++)
00210         {
00211           mat.at<cv::Vec4b>(r, c)[3] = alpha;
00212         }
00213       }
00214     }
00215   }
00216 }
00217 


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