discrete_depth_distortion_model_helpers.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2013, Alex Teichman and Stephen Miller (Stanford University)
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the <organization> nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 RTAB-Map integration: Mathieu Labbe
28 */
29 
31 #include <opencv2/imgproc/imgproc.hpp>
32 #include <opencv2/highgui/highgui.hpp>
35 
36 using namespace std;
37 using namespace Eigen;
38 
39 namespace clams
40 {
41 
42  cv::Mat DiscreteDepthDistortionModel::visualize(const std::string& dir) const
43  {
44  bool writeFiles = !dir.empty() && UDirectory::exists(dir);
45 
46  const DiscreteFrustum& reference_frustum = *frustums_[0][0];
47  int num_layers = reference_frustum.num_bins_;
48 
49  // -- Set up for combined imagery.
50  int horiz_divider = 10;
51  int vert_divider = 20;
52  cv::Mat3b overview(cv::Size(width_ * 2 + horiz_divider, height_ * num_layers + vert_divider * (num_layers + 2)), cv::Vec3b(0, 0, 0));
53  vector<int> pub_layers;
54  for(int i = 0; i < num_layers; ++i)
55  pub_layers.push_back(i);
56  // pub_layers.push_back(1);
57  // pub_layers.push_back(2);
58  // pub_layers.push_back(3);
59  cv::Mat3b pub(cv::Size(width_, height_ * pub_layers.size() + vert_divider * (pub_layers.size() + 2)), cv::Vec3b(255, 255, 255));
60 
61  for(int i = 0; i < num_layers; ++i) {
62  // -- Determine the path to save the image for this layer.
63  char buffer[50];
64  float mindepth = reference_frustum.bin_depth_ * i;
65  float maxdepth = reference_frustum.bin_depth_ * (i + 1);
66  sprintf(buffer, "%05.2f-%05.2f", mindepth, maxdepth);
67  ostringstream oss;
68  oss << dir << "/multipliers_" << buffer << ".png";
69 
70  // -- Compute the multipliers visualization for this layer.
71  // Multiplier of 1 is black, >1 is red, <1 is blue. Think redshift.
72  cv::Mat3b mult(cv::Size(width_, height_), cv::Vec3b(0, 0, 0));
73  for(int y = 0; y < mult.rows; ++y) {
74  for(int x = 0; x < mult.cols; ++x) {
75  const DiscreteFrustum& frustum = *frustums_[y / bin_height_][x / bin_width_];
76  float val = frustum.multipliers_(i);
77  if(val > 1)
78  mult(y, x)[2] = min(255., 255 * (val - 1.0) / 0.25);
79  if(val < 1)
80  mult(y, x)[0] = min(255., 255 * (1.0 - val) / 0.25);
81  }
82  }
83  if(writeFiles)
84  {
85  cv::imwrite(oss.str(), mult);
86  UINFO("Written \"%s\"", oss.str().c_str());
87  }
88 
89  // -- Compute the counts visualization for this layer.
90  // 0 is black, 100 is white.
91  cv::Mat3b count(cv::Size(width_, height_), cv::Vec3b(0, 0, 0));
92  for(int y = 0; y < count.rows; ++y) {
93  for(int x = 0; x < count.cols; ++x) {
94  const DiscreteFrustum& frustum = *frustums_[y / bin_height_][x / bin_width_];
95  uchar val = min(255., (double)(255 * frustum.counts_(i) / 100));
96  count(y, x)[0] = val;
97  count(y, x)[1] = val;
98  count(y, x)[2] = val;
99  }
100  }
101  oss.str("");
102  oss << dir << "/counts_" << buffer << ".png";
103  if(writeFiles)
104  {
105  cv::imwrite(oss.str(), count);
106  UINFO("Written \"%s\"", oss.str().c_str());
107  }
108 
109  // -- Make images showing the two, side-by-side.
110  cv::Mat3b combined(cv::Size(width_ * 2 + horiz_divider, height_), cv::Vec3b(0, 0, 0));
111  for(int y = 0; y < combined.rows; ++y) {
112  for(int x = 0; x < combined.cols; ++x) {
113  if(x < count.cols)
114  combined(y, x) = count(y, x);
115  else if(x > count.cols + horiz_divider)
116  combined(y, x) = mult(y, x - count.cols - horiz_divider);
117  }
118  }
119  oss.str("");
120  oss << dir << "/combined_" << buffer << ".png";
121  if(writeFiles)
122  {
123  cv::imwrite(oss.str(), combined);
124  UINFO("Written \"%s\"", oss.str().c_str());
125  }
126 
127  // -- Append to the overview image.
128  for(int y = 0; y < combined.rows; ++y)
129  for(int x = 0; x < combined.cols; ++x)
130  overview(y + i * (combined.rows + vert_divider) + vert_divider, x) = combined(y, x);
131 
132  // -- Compute the publication multipliers visualization for this layer.
133  // Multiplier of 1 is white, >1 is red, <1 is blue. Think redshift.
134  cv::Mat3b pubmult(cv::Size(width_, height_), cv::Vec3b(255, 255, 255));
135  for(int y = 0; y < pubmult.rows; ++y) {
136  for(int x = 0; x < pubmult.cols; ++x) {
137  const DiscreteFrustum& frustum = *frustums_[y / bin_height_][x / bin_width_];
138  float val = frustum.multipliers_(i);
139  if(val > 1) {
140  pubmult(y, x)[0] = 255 - min(255., 255 * (val - 1.0) / 0.1);
141  pubmult(y, x)[1] = 255 - min(255., 255 * (val - 1.0) / 0.1);
142  }
143  if(val < 1) {
144  pubmult(y, x)[1] = 255 - min(255., 255 * (1.0 - val) / 0.1);
145  pubmult(y, x)[2] = 255 - min(255., 255 * (1.0 - val) / 0.1);
146  }
147  }
148  }
149 
150  // -- Append to publication image.
151  for(size_t j = 0; j < pub_layers.size(); ++j)
152  if(pub_layers[j] == i)
153  for(int y = 0; y < pubmult.rows; ++y)
154  for(int x = 0; x < pubmult.cols; ++x)
155  pub(y + j * (pubmult.rows + vert_divider) + vert_divider, x) = pubmult(y, x);
156  }
157 
158  // -- Add a white bar at the top and bottom for reference.
159  for(int y = 0; y < overview.rows; ++y)
160  if(y < vert_divider || y > overview.rows - vert_divider)
161  for(int x = 0; x < overview.cols; ++x)
162  overview(y, x) = cv::Vec3b(255, 255, 255);
163 
164  // -- Save overview image.
165  ostringstream oss;
166  oss << dir << "/overview.png";
167  if(writeFiles)
168  {
169  cv::imwrite(oss.str(), overview);
170  UINFO("Written \"%s\"", oss.str().c_str());
171  }
172 
173  // -- Save a small version for easy loading.
174  cv::Mat3b overview_scaled;
175  cv::resize(overview, overview_scaled, cv::Size(), 0.2, 0.2, cv::INTER_CUBIC);
176  oss.str("");
177  oss << dir << "/overview_scaled.png";
178  if(writeFiles)
179  {
180  cv::imwrite(oss.str(), overview_scaled);
181  UINFO("Written \"%s\"", oss.str().c_str());
182  }
183 
184  // -- Save publication image.
185  oss.str("");
186  oss << dir << "/pub";
187  // for(size_t i = 0; i < pub_layers.size(); ++i)
188  // oss << "-" << setw(2) << setfill('0') << pub_layers[i];
189  oss << ".png";
190  if(writeFiles)
191  {
192  cv::imwrite(oss.str(), pub);
193  UINFO("Written \"%s\"", oss.str().c_str());
194  }
195 
196  UASSERT(overview.rows == pub.rows);
197  cv::Mat3b targetImage(overview.rows, overview.cols/2 + pub.cols);
198 
199  cv::Mat roiA(targetImage, cv::Rect( 0, 0, overview.cols/2, overview.rows ));
200  cv::Mat(overview, cv::Rect( 0, 0, overview.cols/2, overview.rows )).copyTo(roiA);
201  cv::Mat roiB( targetImage, cv::Rect( overview.cols/2, 0, pub.cols, pub.rows ) );
202  pub.copyTo(roiB);
203 
204  return targetImage;
205  }
206 
207 } // namespace clams
GLM_FUNC_DECL genType min(genType const &x, genType const &y)
x
unsigned char uchar
Definition: matrix.h:41
#define UASSERT(condition)
ULogger class and convenient macros.
static bool exists(const std::string &dirPath)
Definition: UDirectory.cpp:249
GLM_FUNC_DECL detail::tmat4x4< T, defaultp > frustum(T const &left, T const &right, T const &bottom, T const &top, T const &near, T const &far)
#define UINFO(...)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:28