draw_util.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 <cstdlib>
33 #include <algorithm>
34 
35 #include <opencv2/imgproc/imgproc.hpp>
36 #include <opencv2/calib3d/calib3d.hpp>
37 
38 #include <ros/ros.h>
39 
40 #include <swri_opencv_util/show.h>
41 
42 namespace swri_image_util
43 {
44  void RandomColor(int32_t seed, double& r, double& g, double& b)
45  {
46  std::srand(seed);
47 
48  r = static_cast<double>(std::rand()) / RAND_MAX;
49  g = static_cast<double>(std::rand()) / RAND_MAX;
50  b = static_cast<double>(std::rand()) / RAND_MAX;
51  }
52 
54  unsigned char &r,
55  unsigned char &g,
56  unsigned char &b,
57  float value,
58  float min,
59  float max)
60  {
61  float max4 = (max - min) / 4.0;
62  value -= min;
63 
64  if (value == HUGE_VAL)
65  {
66  r = g = b = 255;
67  }
68  else if (value < 0)
69  {
70  r = g = b = 0;
71  }
72  else if (value < max4)
73  {
74  unsigned char c1 = 144;
75 
76  r = 0;
77  g = 0;
78  b = c1 + (unsigned char) ((255 - c1) * value / max4);
79  }
80  else if (value < 2 * max4)
81  {
82  r = 0;
83  g = (unsigned char) (255 * (value - max4) / max4);
84  b = 255;
85  }
86  else if (value < 3 * max4)
87  {
88  r = (unsigned char) (255 * (value - 2 * max4) / max4);
89  g = 255;
90  b = 255 - r;
91  }
92  else if (value < max)
93  {
94  r = 255;
95  g = (unsigned char) (255 - 255 * (value - 3 * max4) / max4);
96  b = 0;
97  }
98  else
99  {
100  r = 255;
101  g = 0;
102  b = 0;
103  }
104  }
105 
107  const std::string& title,
108  const cv::Mat& image1,
109  const cv::Mat& image2,
110  const cv::Mat& transform)
111  {
112  if (image1.rows == image2.rows && image1.cols == image2.cols)
113  {
114  cv::Mat image2_warped;
115  cv::warpAffine(
116  image2,
117  image2_warped,
118  transform,
119  cv::Size(image2.cols, image2.rows));
120 
121  cv::Mat sub = image1 - image2_warped;
122 
123  swri_opencv_util::ShowScaled(title, sub);
124  }
125  }
126 
128  cv::Mat& image_out,
129  const cv::Mat image1,
130  const cv::Mat image2,
131  const cv::Mat points1,
132  const cv::Mat points2,
133  const cv::Scalar& color,
134  bool draw_image_borders)
135  {
136  cv::Size size(image1.cols + image2.cols, std::max(image1.rows, image2.rows));
137  image_out.create(size, CV_MAKETYPE(image1.depth(), 3));
138  image_out.setTo(cv::Vec3b(0, 0, 0));
139  cv::Mat draw_image1 = image_out(cv::Rect(0, 0, image1.cols, image1.rows));
140  cv::Mat draw_image2 = image_out(cv::Rect(image1.cols, 0, image2.cols, image2.rows));
141 
142  if (image1.type() == CV_8U)
143  {
144  cvtColor(image1, draw_image1, cv::COLOR_GRAY2BGR);
145  }
146  else
147  {
148  image1.copyTo(draw_image1);
149  }
150 
151  if (image2.type() == CV_8U)
152  {
153  cvtColor(image2, draw_image2, cv::COLOR_GRAY2BGR);
154  }
155  else
156  {
157  image2.copyTo(draw_image2);
158  }
159 
160  if (draw_image_borders)
161  {
162  cv::rectangle(draw_image1,
163  cv::Point(0, 0),
164  cv::Point(image1.cols, image1.rows),
165  cv::Scalar(0, 0, 0),
166  2);
167 
168  cv::rectangle(draw_image2,
169  cv::Point(0, 0),
170  cv::Point(image2.cols, image2.rows),
171  cv::Scalar(0, 0, 0),
172  2);
173  }
174 
175  cv::RNG rng = cv::theRNG();
176  bool rand_color = color == cv::Scalar::all(-1);
177 
178  for (int i = 0; i < points1.rows; i++)
179  {
180  cv::Scalar match_color = rand_color ? cv::Scalar(rng(256), rng(256), rng(256)) : color;
181  cv::Point2f center1(
182  cvRound(points1.at<cv::Vec2f>(0, i)[0] * 16.0),
183  cvRound(points1.at<cv::Vec2f>(0, i)[1] * 16.0));
184  cv::Point2f center2(
185  cvRound(points2.at<cv::Vec2f>(0, i)[0] * 16.0),
186  cvRound(points2.at<cv::Vec2f>(0, i)[1] * 16.0));
187  cv::Point2f dcenter2(
188  std::min(center2.x + draw_image1.cols * 16.0, (image_out.cols - 1) * 16.0),
189  center2.y);
190  circle(draw_image1, center1, 48, match_color, 1, cv::LINE_AA, 4);
191  circle(draw_image2, center2, 48, match_color, 1, cv::LINE_AA, 4);
192  line(image_out, center1, dcenter2, match_color, 1, cv::LINE_AA, 4);
193  }
194  }
195 
197  const std::string& title,
198  const cv::Mat image1,
199  const cv::Mat image2,
200  const cv::Mat points1,
201  const cv::Mat points2,
202  const cv::Scalar& color,
203  bool draw_image_borders)
204  {
205  cv::Mat image_out;
206  DrawMatches(image_out,
207  image1,
208  image2,
209  points1,
210  points2,
211  color,
212  draw_image_borders);
213 
214  swri_opencv_util::ShowScaled(title, image_out);
215  }
216 
218  const std::string& title,
219  const cv::Mat image,
220  const cv::Mat points1,
221  const cv::Mat points2,
222  const cv::Scalar& color1,
223  const cv::Scalar& color2,
224  bool draw_image_borders)
225  {
226  cv::Mat draw_image;
227  if (image.type() == CV_8U)
228  {
229  cvtColor(image, draw_image, cv::COLOR_GRAY2BGR);
230  }
231  else
232  {
233  draw_image = image.clone();
234  }
235 
236  for (int i = 0; i < points1.rows; i++)
237  {
238  cv::Point2f center1(
239  cvRound(points1.at<cv::Vec2f>(0, i)[0] * 16.0),
240  cvRound(points1.at<cv::Vec2f>(0, i)[1] * 16.0));
241  cv::Point2f center2(cvRound(
242  points2.at<cv::Vec2f>(0, i)[0] * 16.0),
243  cvRound(points2.at<cv::Vec2f>(0, i)[1] * 16.0));
244  circle(draw_image, center1, 48, color1, 1, cv::LINE_AA, 4);
245  line(draw_image, center1, center2, color2, 1, cv::LINE_AA, 4);
246  }
247 
248  swri_opencv_util::ShowScaled(title, draw_image);
249  }
250 }
min
int min(int a, int b)
ros.h
swri_image_util
Definition: draw_util.h:37
swri_image_util::DrawOverlap
void DrawOverlap(const std::string &title, const cv::Mat &image1, const cv::Mat &image2, const cv::Mat &transform)
Definition: draw_util.cpp:106
swri_image_util::RandomColor
void RandomColor(int32_t seed, double &r, double &g, double &b)
Definition: draw_util.cpp:44
swri_image_util::JetColorMap
void JetColorMap(unsigned char &r, unsigned char &g, unsigned char &b, float value, float min, float max)
Map a scalar value to a color gradient.
Definition: draw_util.cpp:53
draw_util.h
swri_image_util::DrawMatches
void DrawMatches(cv::Mat &image_out, const cv::Mat image1, const cv::Mat image2, const cv::Mat points1, const cv::Mat points2, const cv::Scalar &color=cv::Scalar::all(-1), bool draw_image_borders=false)
Definition: draw_util.cpp:127
swri_opencv_util::ShowScaled
void ShowScaled(const std::string &name, const cv::Mat &mat, const cv::Mat &mask=cv::Mat(), double a=-1.0, double b=0.0)
show.h
cvtColor
CvImagePtr cvtColor(const CvImageConstPtr &source, const std::string &encoding)


swri_image_util
Author(s): Kris Kozak
autogenerated on Fri Aug 2 2024 08:39:19