layered_maps.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 by Horatiu George Todoran <todorangrg@gmail.com> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
34 
35 #include <opencv2/core/core.hpp>
36 #include <opencv2/core/core_c.h>
37 #include <opencv2/opencv.hpp>
38 #include <opencv/cv.hpp>
39 
40 using namespace tuw;
41 using namespace std;
42 using namespace cv;
43 
45 
46 }
47 
48 void LayeredMaps::initLayers( int width_pixel, int height_pixel,
49  double min_x, double max_x,
50  double min_y, double max_y, double rotation ) {
51  init(width_pixel, height_pixel, min_x, max_x, min_y, max_y, rotation);
52  initLayers();
53 }
54 
55 size_t LayeredMaps::sizeLayers() const {
56  return mapLayers_.size();
57 }
58 
59 
60 void LayeredMaps::resizeLayers( const size_t& _n ) {
61  mapLayers_.resize ( _n );
62 }
63 
65  for(size_t i = 0; i < mapLayers_.size(); i++){ mapLayers_[i].create ( width(), height(), CV_32FC1 ); clearLayer(i); }
66 }
67 
69  for(size_t i = 0; i < mapLayers_.size(); i++){ clearLayer(i); }
70 }
71 void LayeredMaps::clearLayer( const size_t& _layer ) {
72  mapLayers_[_layer].setTo( 1 );
73 }
74 
75 cv::Mat& LayeredMaps::mapLayer( const size_t& _layer ) {
76  return mapLayers_[_layer];
77 }
78 const cv::Mat& LayeredMaps::mapLayer( const size_t& _layer ) const {
79  return mapLayers_[_layer];
80 }
81 
82 double LayeredMaps::getVal( const size_t& _layer, const Point2D& _worldPos, Interpolation _interpolationType ) const {
83 
84  Point2D mapPos = w2m(_worldPos);
85  if ( !mapPos.inside(0, 0, mapLayer(_layer).rows-1, mapLayer(_layer).cols-1 ) ) { return -100; }
86  const double mapPos_row_d = mapPos.y() , mapPos_col_d = mapPos.x();
87  const int mapPos_row_i = int(mapPos_row_d), mapPos_col_i = int(mapPos_col_d);
88 
89  double retVal;
90  switch ( _interpolationType ) {
91  case SIMPLE :
92  retVal = mapLayer(_layer).at<float_t>( mapPos_row_i, mapPos_col_i );
93  break;
94  case BILINEAR:
95  //if((mapPos_y_i+1 >= mapLayer(layer).rows)||(mapPos_x_i+1 >= mapLayer(layer).cols)||(mapPos_y_i <0)||(mapPos_x_i <0)){throw 0;}
96 
97  const double f00 = mapLayer(_layer).at<float_t>(mapPos_row_i , mapPos_col_i );
98  const double f10 = mapLayer(_layer).at<float_t>(mapPos_row_i + 1, mapPos_col_i );
99  const double f01 = mapLayer(_layer).at<float_t>(mapPos_row_i , mapPos_col_i + 1);
100  const double f11 = mapLayer(_layer).at<float_t>(mapPos_row_i + 1, mapPos_col_i + 1);
101 
102  const double mapPosRed_row = mapPos_row_d - mapPos_row_i, mapPosRed_col = mapPos_col_d - mapPos_col_i;
103 
104  retVal = f00 * (1 - mapPosRed_row) * (1 - mapPosRed_col) +
105  f10 * mapPosRed_row * (1 - mapPosRed_col) +
106  f01 * (1 - mapPosRed_row) * mapPosRed_col +
107  f11 * mapPosRed_row * mapPosRed_col;
108  break;
109  }
110  return retVal;
111 }
112 
113 float distance2probabilitySigmoid ( float d, float threshold ) {
114  float p = 0;
115  if ( d < threshold ) {
116  p = tanh ( ( 2*M_PI*-d ) /threshold+M_PI ) * 0.5 + 0.5;
117  }
118  return p;
119 }
120 float distance2probabilityTriangle ( float d, float threshold ) {
121  float p = 0;
122  if ( d < threshold ) {
123  p = 1.-d/threshold;
124  }
125  return p;
126 }
127 
128 void LayeredMaps::computeDistanceField ( Mat& _mDst, vector< Point2D >& _pSrc, const double& _radius, bool _flipDistance, bool connectPoints ) const {
129  Mat srcMap; srcMap.create ( width(), height(), CV_32FC1 ); srcMap.setTo ( 1 );
130  Scalar colour ( 0 );
131  if(connectPoints){ for ( size_t i = 1; i < _pSrc.size(); i++ ) { line ( srcMap, _pSrc[i-1], _pSrc[i], colour, 8); } }
132  else { for ( size_t i = 0; i < _pSrc.size(); i++ ) { circle ( srcMap, _pSrc[i], 1, colour, 1, 8); } }
133  computeDistanceField ( _mDst, srcMap, _radius, _flipDistance );
134 }
135 
136 
137 void LayeredMaps::computeDistanceField ( Mat& _mDst, Mat& _mSrc, const double& _radius, bool _flipDistance ) const {
138  Mat srcMap;
139  _mSrc.convertTo(srcMap, CV_8U);
141  int maskSize0 = CV_DIST_MASK_PRECISE;
142  int voronoiType = -1;
143  int distType0 = CV_DIST_L2;//CV_DIST_L1;
144  int maskSize = voronoiType >= 0 ? CV_DIST_MASK_5 : maskSize0;
145  int distType = voronoiType >= 0 ? CV_DIST_L2 : distType0;
146  Mat destMap_f, labels;
147 // destMap_f.create ( width(), height(), CV_32F );
148  if ( voronoiType < 0 ){ cv::distanceTransform ( srcMap, destMap_f, distType, maskSize ); }
149  else { cv::distanceTransform ( srcMap, destMap_f, labels, distType, maskSize, voronoiType ); }
150 // destMap_f.convertTo(destMap_ui, CV_8U );
151 
152  CV_Assert(destMap_f.depth() == CV_32F);
153  int channels = destMap_f.channels();
154  int nRows = destMap_f.rows;
155  int nCols = destMap_f.cols * channels;
156  if (destMap_f.isContinuous()) { nCols *= nRows; nRows = 1; }
157 
158  float threshold = _radius * scale_x ();
159  float_t* p_d; float* p_s;
160  for(int i = 0; i < nRows; ++i) {
161  p_d = destMap_f.ptr<float_t>(i);
162  for (int j = 0; j < nCols; ++j) { if (_flipDistance) { p_d[j] = (float_t)( distance2probabilityTriangle ( p_d[j], threshold ) ); }
163  else { p_d[j] = (float_t)( (1. - distance2probabilityTriangle ( p_d[j], threshold ) ) ); } }
164  }
165  if ( ( _mDst.channels() != 1 ) && ( _mDst.channels() != 5 ) ) { cvtColor(destMap_f, _mDst, CV_GRAY2BGR ); }
166  else { _mDst = destMap_f; }
167 
168 }
const double & y() const
Definition: point2d.cpp:49
bool inside(double x0, double y0, double x1, double y1) const
Definition: point2d.cpp:171
void resizeLayers(const size_t &_n)
virtual void initLayers()
float distance2probabilitySigmoid(float d, float threshold)
void init(const M_string &remappings)
void computeDistanceField(cv::Mat &_mDst, std::vector< Point2D > &_pSrc, const double &_radius, bool _flipDistance=false, bool connectPoints=false) const
Definition: point2d.h:208
void clearLayer(const size_t &_layer)
float distance2probabilityTriangle(float d, float threshold)
cv::Mat & mapLayer(const size_t &_layer)
size_t sizeLayers() const
Definition: command.h:8
CvImagePtr cvtColor(const CvImageConstPtr &source, const std::string &encoding)
double getVal(const size_t &_layer, const tuw::Point2D &_worldPos, Interpolation interpolationType=BILINEAR) const
const double & x() const
Definition: point2d.cpp:35


tuw_geometry
Author(s): Markus Bader
autogenerated on Mon Jun 10 2019 15:33:08