blend.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2018, 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/blend.h>
00031 
00032 #include <opencv2/imgproc/imgproc.hpp>
00033 
00034 namespace swri_opencv_util {
00035 
00036 cv::Mat blend(
00037     const cv::Mat& src1,
00038     const cv::Mat& alpha1,
00039     const cv::Mat& src2,
00040     const cv::Mat& alpha2)
00041 {
00042   int out_type = src1.type();
00043   cv::Mat s1, s2, a1, a2;
00044   alpha1.convertTo(a1, CV_32F);
00045   alpha2.convertTo(a2, CV_32F);
00046   src1.convertTo(s1, CV_32F);
00047   src2.convertTo(s2, CV_32F);
00048   cv::Mat w1;
00049   cv::divide(a1, a1 + a2, w1);
00050   cv::Mat w2 = -w1 + 1.0;
00051 
00052   cv::Mat blended = s1.mul(w1) + s2.mul(w2);
00053   cv::Mat blended_out;
00054   blended.convertTo(blended_out, out_type);
00055   return blended_out;
00056 }
00057 
00058 cv::Mat blend(
00059     const cv::Mat& overlay,
00060     const cv::Mat& base,
00061     double alpha)
00062 {
00063   alpha = std::min(1.0, alpha);
00064   alpha = std::max(0.0, alpha);
00065   cv::Mat blended;
00066   cv::addWeighted(overlay, alpha, base, 1.0 - alpha, 0, blended);
00067   return blended;
00068 }
00069 
00070 cv::Mat overlayColor(
00071     const cv::Mat& src,
00072     const cv::Mat& mask,
00073     const cv::Scalar& color,
00074     double alpha)
00075 {
00076   alpha = std::min(1.0, alpha);
00077   alpha = std::max(0.0, alpha);
00078 
00079   cv::Size size = src.size();
00080   cv::Mat color_image;
00081 
00082   if (src.type() == CV_8U)
00083   {
00084     cv::cvtColor(src, color_image, cv::COLOR_GRAY2BGR);
00085   }
00086   else if (src.type() == CV_32F || src.type() == CV_16U)
00087   {
00088     cv::Mat tmp;
00089     src.convertTo(tmp, CV_8U);
00090     cv::cvtColor(tmp, color_image, cv::COLOR_GRAY2BGR);
00091   }
00092   else if (src.type() == CV_32FC3 || src.type() == CV_16UC3)
00093   {
00094     src.convertTo(color_image, CV_8UC3);
00095   }
00096   else if (src.type() != CV_8UC3)
00097   {
00098     color_image = src;
00099   }
00100   else
00101   {
00102     return cv::Mat();
00103   }
00104 
00105   // Create the color overlay image.
00106   cv::Mat overlay(size, CV_8UC3);
00107   overlay.setTo(color);
00108 
00109   // Create the alpha channel for the overlay image.
00110   cv::Mat overlay_alpha = cv::Mat::zeros(size, CV_32F);
00111   overlay_alpha.setTo(alpha, mask);
00112 
00113   // Create the alpha channel for the base image.
00114   cv::Mat base_alpha(size, CV_32F);
00115   base_alpha = 1.0 - alpha;
00116 
00117   // Blend the images based on the relative alpha values at each pixel.
00118   return swri_opencv_util::blend(overlay, overlay_alpha, color_image, base_alpha);
00119 }
00120 
00121 }
00122 


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