blend.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2018, 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/blend.h>
31 
32 #include <opencv2/imgproc/imgproc.hpp>
33 
34 namespace swri_opencv_util {
35 
36 cv::Mat blend(
37  const cv::Mat& src1,
38  const cv::Mat& alpha1,
39  const cv::Mat& src2,
40  const cv::Mat& alpha2)
41 {
42  int out_type = src1.type();
43  cv::Mat s1, s2, a1, a2;
44  alpha1.convertTo(a1, CV_32F);
45  alpha2.convertTo(a2, CV_32F);
46  src1.convertTo(s1, CV_32F);
47  src2.convertTo(s2, CV_32F);
48  cv::Mat w1;
49  cv::divide(a1, a1 + a2, w1);
50  cv::Mat w2 = -w1 + 1.0;
51 
52  cv::Mat blended = s1.mul(w1) + s2.mul(w2);
53  cv::Mat blended_out;
54  blended.convertTo(blended_out, out_type);
55  return blended_out;
56 }
57 
58 cv::Mat blend(
59  const cv::Mat& overlay,
60  const cv::Mat& base,
61  double alpha)
62 {
63  alpha = std::min(1.0, alpha);
64  alpha = std::max(0.0, alpha);
65  cv::Mat blended;
66  cv::addWeighted(overlay, alpha, base, 1.0 - alpha, 0, blended);
67  return blended;
68 }
69 
70 cv::Mat overlayColor(
71  const cv::Mat& src,
72  const cv::Mat& mask,
73  const cv::Scalar& color,
74  double alpha)
75 {
76  alpha = std::min(1.0, alpha);
77  alpha = std::max(0.0, alpha);
78 
79  cv::Size size = src.size();
80  cv::Mat color_image;
81 
82  if (src.type() == CV_8U)
83  {
84  cv::cvtColor(src, color_image, cv::COLOR_GRAY2BGR);
85  }
86  else if (src.type() == CV_32F || src.type() == CV_16U)
87  {
88  cv::Mat tmp;
89  src.convertTo(tmp, CV_8U);
90  cv::cvtColor(tmp, color_image, cv::COLOR_GRAY2BGR);
91  }
92  else if (src.type() == CV_32FC3 || src.type() == CV_16UC3)
93  {
94  src.convertTo(color_image, CV_8UC3);
95  }
96  else if (src.type() != CV_8UC3)
97  {
98  color_image = src;
99  }
100  else
101  {
102  return cv::Mat();
103  }
104 
105  // Create the color overlay image.
106  cv::Mat overlay(size, CV_8UC3);
107  overlay.setTo(color);
108 
109  // Create the alpha channel for the overlay image.
110  cv::Mat overlay_alpha = cv::Mat::zeros(size, CV_32F);
111  overlay_alpha.setTo(alpha, mask);
112 
113  // Create the alpha channel for the base image.
114  cv::Mat base_alpha(size, CV_32F);
115  base_alpha = 1.0 - alpha;
116 
117  // Blend the images based on the relative alpha values at each pixel.
118  return swri_opencv_util::blend(overlay, overlay_alpha, color_image, base_alpha);
119 }
120 
121 }
122 
cv::Mat blend(const cv::Mat &src1, const cv::Mat &alpha1, const cv::Mat &src2, const cv::Mat &alpha2)
Definition: blend.cpp:36
cv::Mat overlayColor(const cv::Mat &src, const cv::Mat &mask, const cv::Scalar &color, double alpha)
Definition: blend.cpp:70


swri_opencv_util
Author(s): Marc Alban
autogenerated on Fri Jun 7 2019 22:05:46