Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleMultipleDepthWarp.cpp
1 /*
2  * ExampleMultipleDepthWarp.cpp
3  *
4  * Created on: Jan 28, 2010
5  * Author: africk
6  */
7 
8 /*
9  This file is part of the BIAS library (Basic ImageAlgorithmS).
10 
11  Copyright (C) 2003-2009 (see file CONTACT for details)
12  Multimediale Systeme der Informationsverarbeitung
13  Institut fuer Informatik
14  Christian-Albrechts-Universitaet Kiel
15 
16 
17  BIAS is free software; you can redistribute it and/or modify
18  it under the terms of the GNU Lesser General Public License as published by
19  the Free Software Foundation; either version 2.1 of the License, or
20  (at your option) any later version.
21 
22  BIAS is distributed in the hope that it will be useful,
23  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  GNU Lesser General Public License for more details.
26 
27  You should have received a copy of the GNU Lesser General Public License
28  along with BIAS; if not, write to the Free Software
29  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 
33 /**
34  @example ExampleMultipleDepthWarp.cpp
35  @relates MultipleDepthWarp
36  @brief Example for using the multidepthwarp
37  @ingroup g_examples
38  @ingroup g_openglframework
39  @author Anatol Frick
40 */
41 
42 
43 #include <Base/Image/ImageConvert.hh>
44 
45 #include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
46 #include <OpenGLFramework/SpecializedBatches/MultipleDepthWarp.hh>
47 #include <OpenGLFramework/Base/glfException.hh>
48 
49 #include <Utils/Param.hh>
50 #include <Utils/IOUtils.hh>
51 #include <Base/Image/ImageIO.hh>
52 #include <Base/Image/Image.hh>
53 #include <Image/Camera.hh>
54 #include <Filter/Gauss.hh>
55 #include <Base/Debug/TimeMeasure.hh>
56 #include <OpenGLFramework/SpecializedBatches/DisparityInterpolator.hh>
57 #include <OpenGLFramework/SpecializedBatches/SeparabelBoxFilter.hh>
58 #include <Filter/Rescale.hh>
59 #include <Filter/Median.hh>
60 
61 #include <Gui/biasgl.h>
62 
63 using namespace std;
64 using namespace BIAS;
65 
66 
67 void ShiftAndAdjustFrustum(ProjectionParametersPerspective* ppp, double distFromCam, double shift) {
68  unsigned int width, height;
69  ppp->GetImageSize(width,height);
70 
71  cout << ppp->GetK() << endl;
72 
73  HomgPoint2D leftTop(0.5,0.5,1);
74  HomgPoint2D rightBottom((double)width-0.5, (double) height - 0.5,1);
76 
77  Vector3<double> d1;
78  ppp->UnProjectLocal(leftTop, p, d1, true);
79 
80  d1 = (d1 / d1[2]) * distFromCam;
81 
82  d1[0] -= shift;
83 
84  Vector3<double> d2;
85  ppp->UnProjectLocal(rightBottom, p , d2, true);
86 
87  d2 = (d2 / d2[2]) * distFromCam;
88 
89  d2[0] -= shift;
90 
91  ppp->SetIntrinsics(d1,d2,width,height);
92 
93  ppp->SetC(ppp->GetC() + Vector3<double>(shift,0,0));
94 
95  cout << ppp->GetK() << endl;
96 
97 }
98 
99 
100 int main(int argc, char* argv[]) {
101  Param params(true);
102 
103  string* firstDepthImageName = params.AddParamString("firstDepthImage",
104  "image of first source camera", "", 'f');
105  string* secondDepthImageName = params.AddParamString("secondDepthImage",
106  "image of second source camera", "", 's');
107 
108 // unused string* dispImageName = params.AddParamString("resultDispImage",
109 // "result disparity image name", "depthResult.mip", 'd');
110 // unused string* colorImageName = params.AddParamString("resultColorImage",
111 // "result color image name", "colorResult.mip", 'c');
112 
113  string* textureForTheFirstSourceProj = params.AddParamString("projectiveTexture",
114  "texture for the first source proj", "", 'p');
115 
116  string* targetProjName = params.AddParamString("targetProj",
117  "projection with params of target perspective cam", "", 't');
118  string* firstSourceProjName = params.AddParamString("firstSourceProj",
119  "projection with params of first source cam", "", 'a');
120  string* secondSourceProjName = params.AddParamString("secondSourceProj",
121  "projection with params of second source cam", "", 'b');
122 
123  double* zNear = params.AddParamDouble("zNear", "", 100);
124 
125 // unused double* distFromCam = params.AddParamDouble("distFromCam", "distance from camera centrum to the display in millimeter", 4000);
126 // unused double* eyeDist = params.AddParamDouble("eyeDist", "distance between the eyes in millimeter", 65);
127  double* zFar = params.AddParamDouble("zFar", "", 8000);
128 // unused int* step = params.AddParamInt("step","", 0);
129 
130  if (!IOUtils::ParseCommandLineEvalHelp(params, argc, argv))
131  return 0;
132 
133  vector<ProjectionParametersPerspectiveDepth> pmdParams;
134 
135  vector<Projection> sourceProjs;
136  Projection targetProj;
137 
138  if ((*firstSourceProjName).compare("")!=0) {
139  sourceProjs.push_back(Projection());
140  if (sourceProjs.back().Load(*firstSourceProjName) != 0) {
141  BIASERR("Could not load source tof projection: "<<*firstSourceProjName);
142  return -1;
143  }
144  }
145 
146  if ((*secondSourceProjName).compare("")!=0) {
147  sourceProjs.push_back(Projection());
148  if (sourceProjs.back().Load(*secondSourceProjName) != 0) {
149  BIASERR("Could not load source tof projection: "<<*secondSourceProjName);
150  return -1;
151  }
152  }
153 
154  if (targetProj.Load(*targetProjName) != 0) {
155  BIASERR("Could not load target tof projection:"<<*targetProjName);
156  return -1;
157  }
158 
159  Image<float> firstDepthMap, secondDepthMap;
160  if ((*firstDepthImageName).compare("")!=0) {
161  if (ImageIO::Load(*firstDepthImageName, firstDepthMap)) {
162  BIASERR("Could not load first depth map :"<< *firstDepthImageName);
163  return -1;
164  }
165  }
166  if ((*secondDepthImageName).compare("")!=0) {
167  if (ImageIO::Load(*secondDepthImageName, secondDepthMap)) {
168  BIASERR("Could not load second depth map :"<< *secondDepthImageName);
169  //return -1;
170  }
171  }
172 
173  Image<unsigned char> projTexture;
174 
175  if ((*textureForTheFirstSourceProj).compare("")!=0) {
176  if (ImageIO::Load(*textureForTheFirstSourceProj, projTexture)) {
177  BIASERR("Could not load second depth map :"<< *textureForTheFirstSourceProj);
178  return -1;
179  }
180  }
181 
182 
183  MultipleDepthWarp mpmdWarp;
184 
185 
186  ProjectionParametersBase* ppb = targetProj.GetParameters();
188  dynamic_cast<ProjectionParametersPerspective*> (ppb);
189 
190  ppp->Rescale((unsigned int)960, (unsigned int) 540);
191 
192  //ShiftAndAdjustFrustum(ppp,(*distFromCam),((*distFromCam)/4000.0)*(*eyeDist)*(*step));
193 
194 
195  ProjectionParametersBase* source0_ppb = sourceProjs[0].GetParameters();
196  ProjectionParametersPerspective* source0_ppp =
197  dynamic_cast<ProjectionParametersPerspective*> (source0_ppb);
198 
199  source0_ppp->Rescale((unsigned int) 960, (unsigned int) 540);
200 
201  if (ppp == NULL) {
202  BIASERR("target parameters are not from type perspective");
203  return -1;
204  }
205 
206 
207 
208 
209  Image<float> tmpDepthMap;
210  Gauss<float,float> gaussFilter;
211  gaussFilter.SetHalfWinSize(30);
212  gaussFilter.Filter(firstDepthMap,tmpDepthMap);
213  firstDepthMap = tmpDepthMap;
214 
215  TimeMeasure tm;
216  Image<float> warpedDepth;
217  Image<unsigned char> warpedColor;
218  Image<unsigned char> redGreen;
219  glfPBuffer pbuffer;
220  try {
221 
223  cfg.width = 5;
224  cfg.height = 5;
225  cfg.doubleBuffer = 0;
226  cfg.redSize = 8;
227  cfg.greenSize = 8;
228  cfg.blueSize = 8;
229  cfg.alphaSize = 0;
230  cfg.depthSize = 24;
231  cfg.stencilSize = 0;
232 
233  pbuffer.Init(cfg);
234 
235  mpmdWarp.SetDepthScaleBeforeWarping(1.0f);
236 
237  // ppp->Rescale((unsigned int) 960, (unsigned int) 540);
238  mpmdWarp.Init(*ppp, sourceProjs);
239  mpmdWarp.SetupNViewsWarp(2000,1.0,20, 8, 4, false);
240  tm.Start();
241  // for (unsigned int i = 0; i < 1000; i++) {
242  mpmdWarp.UploadDepthMap(firstDepthMap, 0);
243  //mpmdWarp.UploadDepthMap(secondDepthMap, 1);
244  mpmdWarp.SetProjectiveTextureCam(*source0_ppp);
245  mpmdWarp.UploadProjectiveTexture(projTexture);
246  mpmdWarp.UseProjectiveTexturing(true);
247 
248  mpmdWarp.SetWarpType(MultipleDepthWarp::DEPTH);
249  mpmdWarp.SetDiscardAngle(80);
250  mpmdWarp.SetDispForDistance(3000, 128);
251  mpmdWarp.SetZNearAndZFar((float) *zNear, (float) *zFar);
252  mpmdWarp.WarpNViews();
253 
254  // }
255  tm.Stop();
256 
257  cout << "frame rate = " << (1000.0 / (tm.GetRealTime() / 1000000.0)) << endl;
258 
259  tm.Print(cout);
260 
261  // mpmdWarp.GetProjectedTexture(warpedColor);
262  // mpmdWarp.GetWarpedDisparity(warpedDepth);
263  //boxFilter.GetFilteredTexture(result);
264 
265  vector<glfTexture2D*>* textures = mpmdWarp.GetResultTexturesFromNViewWarp();
266 
267  //1
268  ((*textures)[0])->CopyToImage(warpedColor);
269  warpedColor.Flip();
270  ImageIO::Save("colorImage1.png",warpedColor);
271 
272 // //2
273 // ((*textures)[1])->CopyToImage(warpedColor);
274 // warpedColor.Flip();
275 // ImageIO::Save("colorImage2.png",warpedColor);
276 //
277 // //3
278 // ((*textures)[2])->CopyToImage(warpedColor);
279 // warpedColor.Flip();
280 // ImageIO::Save("colorImage3.png",warpedColor);
281 //
282 // //4
283 // ((*textures)[3])->CopyToImage(warpedColor);
284 // warpedColor.Flip();
285 // ImageIO::Save("colorImage4.png",warpedColor);
286 //
287 // //5
288 // ((*textures)[4])->CopyToImage(warpedColor);
289 // warpedColor.Flip();
290 // ImageIO::Save("colorImage5.png",warpedColor);
291 //
292 // //6
293 // ((*textures)[5])->CopyToImage(warpedColor);
294 // warpedColor.Flip();
295 // ImageIO::Save("colorImage6.png",warpedColor);
296 //
297 // //7
298 // ((*textures)[6])->CopyToImage(warpedColor);
299 // warpedColor.Flip();
300 // ImageIO::Save("colorImage7.png",warpedColor);
301 //
302 // //8
303 // ((*textures)[7])->CopyToImage(warpedColor);
304 // warpedColor.Flip();
305 // ImageIO::Save("colorImage8.png",warpedColor);
306 
307 
308  // warpedColor.Flip();
309  // warpedDepth.Flip();
310 
311 
312 // if (ImageIO::Save(*dispImageName, warpedDepth) == 0) {
313 // cout << "written disparity map " << *dispImageName << endl;
314 // } else {
315 // BIASERR("could not write " << *dispImageName);
316 // pbuffer.Destroy();
317 // return -1;
318 // }
319 //
320 //
321 // if (ImageIO::Save(*colorImageName, warpedColor) == 0) {
322 // cout << "written disparity map " << *colorImageName << endl;
323 // } else {
324 // BIASERR("could not write " << *colorImageName);
325 // pbuffer.Destroy();
326 // return -1;
327 // }
328 //
329 //
330 // redGreen = projTexture;
331 // Image<unsigned char> grey1;
332 // ImageConvert::Convert(projTexture,grey1,ImageBase::CM_Grey);
333 // Image<unsigned char> grey2;
334 // ImageConvert::Convert(warpedColor, grey2,ImageBase::CM_Grey);
335 //
336 // redGreen.Clear(0);
337 // for (unsigned int x = 0; x < warpedColor.GetWidth(); x++) {
338 // for (unsigned int y = 0; y < warpedColor.GetHeight(); y++) {
339 // redGreen.SetPixel(grey1.PixelValue(x,y),x,y,0);
340 // redGreen.SetPixel(grey2.PixelValue(x,y),x,y,1);
341 // // redGreen.SetPixel(grey1.PixelValue(x,y),x,y,2);
342 // }
343 // }
344 //
345 // ImageIO::Save("redGreenTexture.png",redGreen);
346 
347  } catch (glfException& e) {
348  std::cout << "Error: " << e.GetMessageString() << std::endl;
349  pbuffer.Destroy();
350  return -1;
351  }
352 
353  pbuffer.Destroy();
354 
355  return 0;
356 }
357 
virtual BIAS::Vector3< double > GetC() const
Get projection center.
int SetProjectiveTextureCam(const ProjectionParametersPerspective &textureCam)
sets the parameters for the camera for projective texturing this camera will be used to project the t...
void Print(std::ostream &os=std::cout) const
int SetIntrinsics(double minPhi, double maxPhi, double minTheta, double maxTheta, double angleStep, double aspectratio=1.0)
Method calculates K-Matrix and image size from specified angles.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
Definition: Projection.cpp:62
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
void SetHalfWinSize(const int hws, bool AdjustSigma=true)
define the half win size of the kernel, if AdjustSigma is true sigma is computed according to the cut...
Definition: Gauss.hh:173
double * AddParamDouble(const std::string &name, const std::string &help, double deflt=0.0, double min=-DBL_MAX, double max=DBL_MAX, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:351
void SetDispForDistance(const float &dist_from_cam, const float &disp)
if disparity output is required, then points with the distance (dist_from_cam) will get disparity (di...
int SetupNViewsWarp(const double &distToScreen, const double &zoom, const double &shift, unsigned int numberOfViews, unsigned int centralViewIndex, bool separate=true)
Exception class used for run-time errors in the OpenGLFramework.
Definition: glfException.hh:79
virtual void Destroy()
Uninitializes the rendering context.
int UploadDepthMap(Image< float > &depthMap, unsigned int index)
uploads the given depthMap to the gpu as texture depthMap is a depth map from source camera i ...
const std::string & GetMessageString() const
Returns the description of the error including the file name and line number where the error occured...
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
Definition: Projection.hh:194
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
void SetZNearAndZFar(const float &zNear, const float &zFar)
this function sets near and far clipping planes for the target camera default parameters defined in t...
GLX pbuffer rendering context.
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
sets gauss kernel if params changed and calls convolution or fast grey implementation if possible ...
Definition: Gauss.cpp:89
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
double GetRealTime() const
return real time (=wall time clock) in usec JW For Win32: real-time is measured differently from user...
void SetWarpType(WARPING_TYPE type)
virtual void Init(const glfRenderingContextConfig &config)
Initializes the rendering context with the given configuration.
This class Param provides generic support for parameters.
Definition: Param.hh:231
int Init(const ProjectionParametersPerspective &targetCam, std::vector< Projection > &sourceCameras, bool createDepthMaps=true)
void SetDepthScaleBeforeWarping(float scale)
void SetDiscardAngle(float degree)
void UseProjectiveTexturing(bool enable)
enable or disables use of projective texturing
int Flip()
flips the image vertically (row order is inverted) In place function return 0 in case of success...
Definition: ImageBase.cpp:834
std::vector< glfTexture2D * > * GetResultTexturesFromNViewWarp()
void UploadProjectiveTexture(const Image< unsigned char > &projectiveTexture)
uploads the image which will be used for projective texturing of the scene to the corresponding textu...
virtual void UnProjectLocal(const HomgPoint2D &pos, Vector3< double > &pointOnRay, Vector3< double > &direction, bool IgnoreDistortion=false) const
calculates the viewing ray from the camera center (in the camera coordinate system) which belongs to ...
virtual void Rescale(double ratio, const double offset=0.0)
Adapt internal parameters to resampled image.
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
std::string * AddParamString(const std::string &name, const std::string &help, std::string deflt="", char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:327
Configuration for a rendering context.
virtual void SetC(const BIAS::Vector3< double > &C)
Set projection center.
class TimeMeasure contains functions for timing real time and cpu time.
Definition: TimeMeasure.hh:111