convert.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, 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 
31 
32 #include <opencv2/imgproc/imgproc.hpp>
33 
34 #include <ros/ros.h>
35 
36 namespace swri_opencv_util
37 {
38  cv::Mat ToBgra8(
39  const cv::Mat& mat,
40  const cv::Mat& mask,
41  bool is_rgb,
42  double a,
43  double b)
44  {
45  if (mat.empty())
46  {
47  return mat;
48  }
49 
50  cv::Mat scaled;
51 
52  // Autoscale if a zero
53  if(a == 0)
54  {
55  double min, max;
56  cv::minMaxLoc(mat, &min, &max, 0, 0, mask);
57 
58  if(mat.type() == CV_8UC1)
59  {
60  a = 255.0 / std::max(max - min, DBL_EPSILON);
61  b = -min * a;
62  mat.convertTo(scaled, CV_8U, a, b);
63 
64  cv::Mat color;
65  cv::cvtColor(scaled, color, CV_GRAY2BGRA);
66  SetAlpha(color, 255);
67  color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
68  scaled = color;
69  }
70  else if(mat.type() == CV_32FC1)
71  {
72  a = 255.0 / std::max(max - min, DBL_EPSILON);
73  b = -min * a;
74  mat.convertTo(scaled, CV_8U, a, b);
75 
76  cv::Mat color;
77  cv::cvtColor(scaled, color, CV_GRAY2BGRA);
78  SetAlpha(color, 255);
79  color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
80  scaled = color;
81  }
82  else if(mat.type() == CV_32FC3)
83  {
84  a = 255.0 / std::max(max - min, DBL_EPSILON);
85  b = -min * a;
86  mat.convertTo(scaled, CV_8UC3, a, b);
87 
88  cv::Mat color;
89 
90  if (is_rgb)
91  {
92  cv::cvtColor(scaled, color, CV_RGB2BGRA);
93  }
94  else
95  {
96  cv::cvtColor(scaled, color, CV_BGR2BGRA);
97  }
98 
99  SetAlpha(color, 255);
100  color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
101  scaled = color;
102  }
103  else if(mat.type() == CV_8UC3)
104  {
105  a = 255.0 / std::max(max - min, DBL_EPSILON);
106  b = -min * a;
107  mat.convertTo(scaled, CV_8UC3, a, b);
108 
109  cv::Mat color;
110 
111  if (is_rgb)
112  {
113  cv::cvtColor(scaled, color, CV_RGB2BGRA);
114  }
115  else
116  {
117  cv::cvtColor(scaled, color, CV_BGR2BGRA);
118  }
119 
120  SetAlpha(color, 255);
121  color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
122  scaled = color;
123  }
124  else if(mat.type() == CV_8UC4)
125  {
126  a = 255.0 / std::max(max - min, DBL_EPSILON);
127  b = -min * a;
128  mat.convertTo(scaled, CV_8UC4, a, b);
129 
130  cv::Mat color;
131 
132  if (is_rgb)
133  {
134  cv::cvtColor(scaled, color, CV_RGBA2BGRA);
135  }
136  else
137  {
138  color = scaled;
139  }
140 
141  SetAlpha(color, 255);
142  if (!mask.empty())
143  {
144  color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
145  }
146 
147  scaled = color;
148  }
149  }
150  else
151  {
152  if(mat.type() == CV_8UC3)
153  {
154  mat.convertTo(scaled, CV_8UC3, a, b);
155 
156  cv::Mat color;
157  if (is_rgb)
158  {
159  cv::cvtColor(scaled, color, CV_RGB2BGRA);
160  }
161  else
162  {
163  cv::cvtColor(scaled, color, CV_BGR2BGRA);
164  }
165 
166  SetAlpha(color, 255);
167  color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
168  scaled = color;
169  }
170  else if(mat.type() == CV_8UC4)
171  {
172  mat.convertTo(scaled, CV_8UC4, a, b);
173 
174  cv::Mat color;
175  if (is_rgb)
176  {
177  cv::cvtColor(scaled, color, CV_RGBA2BGRA);
178  }
179  else
180  {
181  color = scaled;
182  }
183 
184  SetAlpha(color, 255);
185  color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
186  scaled = color;
187  }
188  else
189  {
190  mat.convertTo(scaled, CV_8U, a, b);
191 
192  cv::Mat color;
193  cv::cvtColor(scaled, color, CV_GRAY2BGRA);
194  SetAlpha(color, 255);
195  color.setTo(cv::Scalar(0, 0, 0, 0), mask == 0);
196  scaled = color;
197  }
198  }
199 
200  return scaled;
201  }
202 
203  void SetAlpha(cv::Mat& mat, uint8_t alpha)
204  {
205  if (mat.type() == CV_8UC4)
206  {
207  for (int r = 0; r < mat.rows; r++)
208  {
209  for (int c = 0; c < mat.cols; c++)
210  {
211  mat.at<cv::Vec4b>(r, c)[3] = alpha;
212  }
213  }
214  }
215  }
216 }
217 
void SetAlpha(cv::Mat &mat, uint8_t alpha)
Definition: convert.cpp:203
cv::Mat ToBgra8(const cv::Mat &mat, const cv::Mat &mask=cv::Mat(), bool is_rgb=false, double a=0.0, double b=0.0)
Definition: convert.cpp:38


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