discrete_depth_distortion_model_helpers.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2013, Alex Teichman and Stephen Miller (Stanford University)
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007     * Redistributions of source code must retain the above copyright
00008       notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright
00010       notice, this list of conditions and the following disclaimer in the
00011       documentation and/or other materials provided with the distribution.
00012     * Neither the name of the <organization> nor the
00013       names of its contributors may be used to endorse or promote products
00014       derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 RTAB-Map integration: Mathieu Labbe
00028 */
00029 
00030 #include <rtabmap/core/clams/discrete_depth_distortion_model.h>
00031 #include <opencv2/imgproc/imgproc.hpp>
00032 #include <opencv2/highgui/highgui.hpp>
00033 #include <rtabmap/utilite/ULogger.h>
00034 #include <rtabmap/utilite/UDirectory.h>
00035 
00036 using namespace std;
00037 using namespace Eigen;
00038 
00039 namespace clams
00040 {
00041 
00042   cv::Mat DiscreteDepthDistortionModel::visualize(const std::string& dir) const
00043   {
00044           bool writeFiles = !dir.empty() && UDirectory::exists(dir);
00045 
00046     const DiscreteFrustum& reference_frustum = *frustums_[0][0];
00047     int num_layers = reference_frustum.num_bins_;
00048 
00049     // -- Set up for combined imagery.
00050     int horiz_divider = 10;
00051     int vert_divider = 20;
00052     cv::Mat3b overview(cv::Size(width_ * 2 + horiz_divider, height_ * num_layers + vert_divider * (num_layers + 2)), cv::Vec3b(0, 0, 0));
00053     vector<int> pub_layers;
00054     for(int i = 0; i < num_layers; ++i)
00055       pub_layers.push_back(i);
00056     // pub_layers.push_back(1);
00057     // pub_layers.push_back(2);
00058     // pub_layers.push_back(3);
00059     cv::Mat3b pub(cv::Size(width_, height_ * pub_layers.size() + vert_divider * (pub_layers.size() + 2)), cv::Vec3b(255, 255, 255));
00060   
00061     for(int i = 0; i < num_layers; ++i) {
00062       // -- Determine the path to save the image for this layer.
00063       char buffer[50];
00064       float mindepth = reference_frustum.bin_depth_ * i;
00065       float maxdepth = reference_frustum.bin_depth_ * (i + 1);
00066       sprintf(buffer, "%05.2f-%05.2f", mindepth, maxdepth);
00067       ostringstream oss;
00068       oss << dir << "/multipliers_" << buffer << ".png";
00069 
00070       // -- Compute the multipliers visualization for this layer.
00071       //    Multiplier of 1 is black, >1 is red, <1 is blue.  Think redshift.
00072       cv::Mat3b mult(cv::Size(width_, height_), cv::Vec3b(0, 0, 0));
00073       for(int y = 0; y < mult.rows; ++y) {
00074         for(int x = 0; x < mult.cols; ++x) {
00075           const DiscreteFrustum& frustum = *frustums_[y / bin_height_][x / bin_width_];
00076           float val = frustum.multipliers_(i);
00077           if(val > 1)
00078             mult(y, x)[2] = min(255., 255 * (val - 1.0) / 0.25);
00079           if(val < 1)
00080             mult(y, x)[0] = min(255., 255 * (1.0 - val) / 0.25);
00081         }
00082       }
00083       if(writeFiles)
00084           {
00085                   cv::imwrite(oss.str(), mult);
00086                   UINFO("Written \"%s\"", oss.str().c_str());
00087           }
00088 
00089       // -- Compute the counts visualization for this layer.
00090       //    0 is black, 100 is white.
00091       cv::Mat3b count(cv::Size(width_, height_), cv::Vec3b(0, 0, 0));
00092       for(int y = 0; y < count.rows; ++y) {
00093         for(int x = 0; x < count.cols; ++x) {
00094           const DiscreteFrustum& frustum = *frustums_[y / bin_height_][x / bin_width_];
00095           uchar val = min(255., (double)(255 * frustum.counts_(i) / 100));
00096           count(y, x)[0] = val;
00097           count(y, x)[1] = val;
00098           count(y, x)[2] = val;
00099         }
00100       }
00101       oss.str("");
00102       oss << dir << "/counts_" << buffer << ".png";
00103       if(writeFiles)
00104           {
00105                   cv::imwrite(oss.str(), count);
00106                   UINFO("Written \"%s\"", oss.str().c_str());
00107           }
00108 
00109       // -- Make images showing the two, side-by-side.
00110       cv::Mat3b combined(cv::Size(width_ * 2 + horiz_divider, height_), cv::Vec3b(0, 0, 0));
00111       for(int y = 0; y < combined.rows; ++y) {
00112         for(int x = 0; x < combined.cols; ++x) {
00113           if(x < count.cols)
00114             combined(y, x) = count(y, x);
00115           else if(x > count.cols + horiz_divider)
00116             combined(y, x) = mult(y, x - count.cols - horiz_divider);
00117         }
00118       }
00119       oss.str("");
00120       oss << dir << "/combined_" << buffer << ".png";
00121       if(writeFiles)
00122           {
00123                   cv::imwrite(oss.str(), combined);
00124                   UINFO("Written \"%s\"", oss.str().c_str());
00125           }
00126 
00127       // -- Append to the overview image.
00128       for(int y = 0; y < combined.rows; ++y)
00129         for(int x = 0; x < combined.cols; ++x)
00130           overview(y + i * (combined.rows + vert_divider) + vert_divider, x) = combined(y, x);
00131 
00132       // -- Compute the publication multipliers visualization for this layer.
00133       //    Multiplier of 1 is white, >1 is red, <1 is blue.  Think redshift.
00134       cv::Mat3b pubmult(cv::Size(width_, height_), cv::Vec3b(255, 255, 255));
00135       for(int y = 0; y < pubmult.rows; ++y) {
00136         for(int x = 0; x < pubmult.cols; ++x) {
00137           const DiscreteFrustum& frustum = *frustums_[y / bin_height_][x / bin_width_];
00138           float val = frustum.multipliers_(i);
00139           if(val > 1) {
00140             pubmult(y, x)[0] = 255 - min(255., 255 * (val - 1.0) / 0.1);
00141             pubmult(y, x)[1] = 255 - min(255., 255 * (val - 1.0) / 0.1);
00142           }
00143           if(val < 1) {
00144             pubmult(y, x)[1] = 255 - min(255., 255 * (1.0 - val) / 0.1);
00145             pubmult(y, x)[2] = 255 - min(255., 255 * (1.0 - val) / 0.1);
00146           }
00147         }
00148       }
00149   
00150       // -- Append to publication image.
00151       for(size_t j = 0; j < pub_layers.size(); ++j)
00152         if(pub_layers[j] == i)
00153           for(int y = 0; y < pubmult.rows; ++y)
00154             for(int x = 0; x < pubmult.cols; ++x)
00155               pub(y + j * (pubmult.rows + vert_divider) + vert_divider, x) = pubmult(y, x);
00156     }
00157   
00158     // -- Add a white bar at the top and bottom for reference.
00159     for(int y = 0; y < overview.rows; ++y)
00160       if(y < vert_divider || y > overview.rows - vert_divider)
00161         for(int x = 0; x < overview.cols; ++x)
00162           overview(y, x) = cv::Vec3b(255, 255, 255);
00163   
00164     // -- Save overview image.
00165     ostringstream oss;
00166     oss << dir << "/overview.png";
00167     if(writeFiles)
00168           {
00169                   cv::imwrite(oss.str(), overview);
00170                   UINFO("Written \"%s\"", oss.str().c_str());
00171           }
00172 
00173     // -- Save a small version for easy loading.
00174     cv::Mat3b overview_scaled;
00175     cv::resize(overview, overview_scaled, cv::Size(), 0.2, 0.2, cv::INTER_CUBIC);
00176     oss.str("");
00177     oss << dir << "/overview_scaled.png";
00178     if(writeFiles)
00179           {
00180                   cv::imwrite(oss.str(), overview_scaled);
00181                   UINFO("Written \"%s\"", oss.str().c_str());
00182           }
00183 
00184     // -- Save publication image.
00185     oss.str("");
00186     oss << dir << "/pub";
00187     // for(size_t i = 0; i < pub_layers.size(); ++i)
00188     //   oss << "-" << setw(2) << setfill('0') << pub_layers[i];
00189     oss << ".png";
00190     if(writeFiles)
00191           {
00192                   cv::imwrite(oss.str(), pub);
00193                   UINFO("Written \"%s\"", oss.str().c_str());
00194           }
00195 
00196     UASSERT(overview.rows == pub.rows);
00197     cv::Mat3b targetImage(overview.rows, overview.cols/2 + pub.cols);
00198 
00199         cv::Mat roiA(targetImage, cv::Rect( 0, 0, overview.cols/2, overview.rows ));
00200         cv::Mat(overview, cv::Rect( 0, 0, overview.cols/2, overview.rows )).copyTo(roiA);
00201         cv::Mat roiB( targetImage, cv::Rect( overview.cols/2, 0, pub.cols, pub.rows ) );
00202         pub.copyTo(roiB);
00203 
00204     return targetImage;
00205   }
00206 
00207 }  // namespace clams


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:19