Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SphericalDepthPanorama.cpp
1 #include "Image/SphericalDepthPanorama.hh"
2 #include <Base/Image/ImageConvert.hh>
3 #include <Base/Image/ImageIO.hh>
4 #include <Filter/Rescale.hh>
5 #include "Utils/ThreeDOut.hh"
6 #include <bias_config.h>
7 
8 using namespace BIAS;
9 using namespace std;
10 
11 /** Constructor */
13 
14 
15  bFirstPoseSet_ = false;
16  pSphericalProj_ = NULL;
17  bTextureFilled_ = false;
18  pInitialProj_ = initialProjection_;
19  if (pInitialProj_ != NULL) {
20  pSphericalProj_ = new BIAS::Projection(*pInitialProj_);
21  pSphericalProj_->GetParameters()->GetImageSize(panoramaWidth_,panoramaHeight_);
22  panorama_.Init(panoramaWidth_,panoramaHeight_,1);
23  panoramaHitCount_.Init(panoramaWidth_,panoramaHeight_,1);
24  panoramaHitCount_.FillImageWithConstValue(0.0);
25  panoramaTexture_.Init(panoramaWidth_,panoramaHeight_,1);
26 
27  //contruct spherical projection
28  pSphericalProj_->GetParameters()->SetQC(pInitialProj_->GetQ(),
29  pInitialProj_->GetC());
30  bFirstPoseSet_ = true;
31  }
32 
33 
34 }
35 
36 /** Destructor */
38 
39  if(pSphericalProj_){
40  delete pSphericalProj_;
41  pSphericalProj_ = NULL;
42  }
43 }
44 
45 
48  BIAS::Projection & p,
50  if(!bFirstPoseSet_){
51  pSphericalProj_ = new BIAS::Projection(p);
52  pSphericalProj_->GetParameters()->GetImageSize(panoramaWidth_,panoramaHeight_);
53  panorama_.Init(panoramaWidth_,panoramaHeight_,1);
54  panoramaHitCount_.Init(panoramaWidth_,panoramaHeight_,1);
55  panoramaHitCount_.FillImageWithConstValue(0.0);
56  panoramaTexture_.Init(panoramaWidth_,panoramaHeight_,1);
57 
58  //contruct spherical projection
59  pSphericalProj_->GetParameters()->SetQC(p.GetQ(),
60  p.GetC());
61  bFirstPoseSet_ = true;
62  }
63  // cout<<"Initial projection: "<<*pSphericalProj_<<endl;
64  // cout<<"Current projection: "<<p<<endl;
65  int ret =0;
66  float **ida = depthMap.GetImageDataArray();
67  float **idaP = panorama_.GetImageDataArray();
68  float **idaPHC = panoramaHitCount_.GetImageDataArray();
69  unsigned char ** idaTex = panoramaTexture_.GetImageDataArray();
70 
71  int width = depthMap.GetWidth();
72  int height = depthMap.GetHeight();
73 
74  float depthValue=0.0;
75  BIAS::HomgPoint3D point3D;
76  BIAS::HomgPoint2D point2D,sphericalPoint;
77 
78  for(int x=0;x<width;x++){
79  for(int y=0;y<height;y++){
80  depthValue = ida[y][x];
81  if (depthValue > 0.0) {
82  //fill texture panorama with values
83  if(texture != NULL){
84  idaTex[y][x] = texture->GetImageDataArray()[y][x];
85  bTextureFilled_ = true;
86  }
87  if(idaPHC[y][x] == 0.0){
88  idaP[y][x]=depthValue;
89  idaPHC[y][x]=1.0;
90  }
91  else{
92  idaP[y][x]=
93  (idaP[y][x] * idaPHC[y][x]+depthValue)/(idaPHC[y][x]+1);
94  idaPHC[y][x]+=1.0;
95  }
96  }
97  }
98  }
99 
100 /*
101  unsigned int x1,y1;
102  for(int x=0;x<width;x++){
103  for(int y=0;y<height;y++){
104  point2D[0]=x;
105  point2D[1]=y;
106  point2D[2]=1;
107  depthValue = ida[y][x];
108  //if (depthValue != 0.0) {
109  if (depthValue > 0.0) {
110  // Unproject to a 3Dpoint
111  point3D = p.UnProjectToPoint(point2D,depthValue);
112  //cout<<"Point :"<<x<<","<<y<<" Maps to 3D Point"<<point3D<<endl;
113  if(pSphericalProj_->DoesPointProjectIntoImage(point3D,sphericalPoint)){
114  sphericalPoint.Homogenize();
115  x1 = (unsigned int)sphericalPoint[0];
116  y1 = (unsigned int)sphericalPoint[1];
117  //cout<<"3DPoint maps to 2D spherical point:"<<x1<<","<<y1<<endl;
118  if(x1>=0.0 && x1<panoramaWidth_ &&
119  y1>=0.0 && y1<panoramaHeight_ ) {
120 
121  //fill texture panorama with values
122  if(texture != NULL){
123  idaTex[y1][x1] = texture->GetImageDataArray()[y][x];
124  bTextureFilled_ = true;
125  }
126  if(idaPHC[y1][x1] == 0.0){
127  idaP[y1][x1]=depthValue;
128  idaPHC[y1][x1]=1.0;
129  }
130  else{
131  idaP[y1][x1]=
132  (idaP[y1][x1] * idaPHC[y1][x1]+depthValue)/(idaPHC[y1][x1]+1);
133  idaPHC[y1][x1]+=1.0;
134  }
135  }
136  }
137  //else
138  // cout<<"point3D:"<<point3D<<" has no 2D point!"<<endl;
139  }
140  }
141  }
142 
143 
144  */
145  return ret;
146 }
147 
148 
151  BIAS::Image<unsigned char> &texture){
152  int ret =0;
153  image = panorama_;
154  image.ScaleShiftBetween(0,255);
155  //BIAS::ImageIO::Save("pan.mip",image);
156  if(bTextureFilled_){
157  texture = panoramaTexture_;
158  // BIAS::ImageIO::Save("tex.mip",panoramaTexture_);
159  }
160  else{
161  BIAS::ImageConvert::ConvertST(panorama_,texture,
163  texture.ScaleShiftBetween(0,255);
164  }
165  return ret;
166 }
167 
168 
171  int ret =-1;
173  if(bTextureFilled_){
174  if(pSphericalProj_ != NULL){
175  int factor = 4;
176  BIAS::Projection proj(*pSphericalProj_);
177  BIAS::Image<float> depthDownSampled_(panoramaWidth_ / factor, panoramaHeight_ / factor);
178  Rescale<float, float> pRescaleFilter_;
179  pRescaleFilter_.SetFactor((float)factor);
180  pRescaleFilter_.SetLowPassType(LPT_Gauss);
181  pRescaleFilter_.Filter(panorama_, depthDownSampled_);
182  int width = depthDownSampled_.GetWidth();
183  int height = depthDownSampled_.GetHeight();
184  // remove low depth values resulting from downsampling on 0.0 edges
185  BIAS::Image<float> filteredDepthMap_(width, height);
186  filteredDepthMap_.FillImageWithConstValue(0.0);
187 
188  // anti bubblegum: delete points of high variance
189  float **idaF = filteredDepthMap_.GetImageDataArray();
190  float **idaD = depthDownSampled_.GetImageDataArray();
191  for (int x=0;x<width;x++) {
192  for (int y=0;y<height;y++) {
193  if (idaD[y][x] < 10.0)
194  continue;
195  float minDepth_ = idaD[y][x];
196  float maxDepth_ = idaD[y][x];
197  for (int xw=x-1;xw<=x+1;xw+=1) {
198  for (int yw=y-1;yw<=y+1;yw+=1) {
199  if (xw >= 0 && yw >= 0 && xw < width && yw < height) {
200  if (idaD[yw][xw] > maxDepth_)
201  maxDepth_ = idaD[yw][xw];
202  if (idaD[yw][xw] < minDepth_)
203  minDepth_ = idaD[yw][xw];
204  }
205  }
206  }
207  if (maxDepth_ - minDepth_ < 1000.0)
208  idaF[y][x] = idaD[y][x];
209  }
210  }
211  depthDownSampled_ = filteredDepthMap_;
212 
213  BIAS::ImageIO::Save("Panorama_DepthMap.mip", panorama_);
214  BIAS::ImageIO::Save("Panorama_DepthMap_Downsampled.mip", depthDownSampled_);
215  BIAS::ImageIO::Save("Panorama_Texture.mip", panoramaTexture_);
216 #ifdef BIAS_HAVE_XML2
217  proj.XMLWrite("Panorama_Projection.xml");
218 #else
219  BIASERR("Could not save because of missing XML support in BIAS.");
220 #endif
221  proj.Rescale((float)factor);
222  mesh.GenerateDenseMesh(depthDownSampled_,proj,panoramaTexture_,0,0, depthDownSampled_.GetWidth() ,depthDownSampled_.GetHeight());
223  proj.Rescale(1.0f/float(factor));
224  // mesh.GenerateDenseMesh(panorama_,proj,panoramaTexture_,0,0,
225  // panoramaWidth_,panoramaHeight_);
226  // mesh.GenerateSimplifiedMesh(panorama_,
227  // proj,panoramaTexture_,0,0,
228  // panoramaWidth_,panoramaHeight_);
229 
230  return 0;
231  }
232  else
233  return ret;
234 
235  }
236  else{
237  BIAS::ImageConvert::ConvertST(panorama_,tmpUC,
239  tmpUC.ScaleShiftBetween(0,255);
240  if(pSphericalProj_ != NULL){
241  BIAS::Projection proj(*pSphericalProj_);
242  mesh.GenerateDenseMesh(panorama_,proj,tmpUC,0,0,
243  panoramaWidth_,panoramaHeight_);
244 // mesh.GenerateSimplifiedMesh(panorama_,
245 // proj,panoramaTexture_,0,0,
246 // panoramaWidth_,panoramaHeight_);
247 
248  return 0;
249  }
250  else
251  return ret;
252 
253  }
254 }
int GetPanorama(BIAS::Image< float > &image, BIAS::Image< unsigned char > &texture)
int GenerateDenseMesh(const BIAS::Image< float > &DenseDepthMap, const BIAS::Image< unsigned char > &Texture)
Calculate a triangle mesh from dense depth map without PMatrix.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
int ScaleShiftBetween(double Min, double Max)
scales and shifts image so afterwards every pixel has a value between Min and Max ...
Definition: Image.cpp:1118
void SetLowPassType(int lpt)
Sets the lowpass filter by a lowpass filter type.
Definition: Rescale.cpp:2060
void Rescale(float ratio, unsigned int cam=0)
adapt internal params to resampled image
Definition: Projection.hh:471
Down-, Upsampling routines and Resize.
Definition: Rescale.hh:71
unsigned int GetWidth() const
Definition: ImageBase.hh:312
BIAS::Quaternion< double > GetQ(unsigned int cam=0) const
return rotation quaternion of camera with index cam if cam&gt;0 this is a relative pose in the coordinat...
Definition: Projection.cpp:524
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
int ProcessSphericalProjection(BIAS::Image< float > &depthMap, BIAS::Projection &p, BIAS::Image< unsigned char > *texture=NULL)
Call Process to update the measurement.
static int ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest, ImageBase::EStorageType targetST)
Function to convert the storage type of images e.g.
int XMLWrite(const std::string &Filename, int CompressionLevel=0, bool AutoAddCompressionSuffix=true, std::string encoding="UTF-8") const
call this to add the class to a new xml tree and write it to the file Filename.
Definition: XMLBase.cpp:40
Create and represent a 3D triangle mesh.
Definition: TriangleMesh.hh:84
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
scales the src to dst using the downsampling factor from SetFactor()
Definition: Rescale.cpp:89
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void FillImageWithConstValue(StorageType Value)
fill grey images
Definition: Image.cpp:456
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
int GetTriangleMesh(BIAS::TriangleMesh &mesh)
Vector3< double > GetC(unsigned int cam=0) const
return Center of camera with index cam.
Definition: Projection.hh:236
SphericalDepthPanorama(Projection *initialProjection_=NULL)
Constructor with default params.
void SetFactor(double factor)
the downsampling factor
Definition: Rescale.hh:361
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153