Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImageWarper.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "ImageWarper.hh"
26 #include <OpenGLFramework/Base/glfBatch.hh>
27 #include <Base/Image/ImageIO.hh>
28 #include <Base/Debug/TimeMeasure.hh>
29 
30 using namespace BIAS;
31 using namespace std;
32 
33 
34 template <class StorageType>
36 ImageWarper(bool ignoreRadialDistortion){
37  ignoreRadialDist_ = ignoreRadialDistortion;
38  inited_=false;
39 }
40 
41 template <class StorageType>
44  pBuffer_.Destroy();
45 }
46 
47 
48 template <class StorageType> int
52  float minZ, float maxZ){
53  //init context
55  //TODO: Did this do something in the past ???
56  /*int depthbits;
57  GLenum depthTextureFormat = 0;
58  glGetIntegerv(GL_DEPTH_BITS, &depthbits);
59  switch (depthbits) {
60  case 16:
61  depthTextureFormat = GL_DEPTH_COMPONENT16;
62  break;
63  case 24:
64  depthTextureFormat = GL_DEPTH_COMPONENT24;
65  break;
66  case 32:
67  depthTextureFormat = GL_DEPTH_COMPONENT32;
68  break;
69  }*/
70  cfg.width = 5;
71  cfg.height = 5;
72  cfg.doubleBuffer = 0;
73  cfg.redSize = 8;
74  cfg.greenSize = 8;
75  cfg.blueSize = 8;
76  cfg.alphaSize = 8;
77  cfg.depthSize = 24;
78  cfg.stencilSize = 0;
79  //cout<<"Before pbuffer Init:"<<endl;
80  // initialize context
81  try {
82  pBuffer_.Init(cfg);
83  }catch (glfException& e) {
84  std::cout << "ImageWarper Init Error: " << e.GetMessageString() << std::endl;
85  return -1;
86  }
87 
88  target.GetImageSize(width_, height_);
89  //cout<<"ImageWarper width, height:"<<width_<<", "<<height_<<endl;
90 
91  try {
92  resultTexture.Create();
93  resultTexture.SetMagFilter(GL_NEAREST);
94  resultTexture.SetMinFilter(GL_NEAREST);
95  resultTexture.SetWrapS(GL_CLAMP);
96  resultTexture.SetWrapT(GL_CLAMP);
97  resultTexture.Allocate(width_, height_, GL_RGB);
98  }catch (glfException& e) {
99  std::cout << "ImageWarper resultTexture error: " << e.GetMessageString() << std::endl;
100  return -2;
101  }
102  try {
103  depthTexture.Create();
104  depthTexture.SetMagFilter(GL_NEAREST);
105  depthTexture.SetMinFilter(GL_NEAREST);
106  depthTexture.SetWrapS(GL_CLAMP);
107  depthTexture.SetWrapT(GL_CLAMP);
108  depthTexture.Allocate(width_, height_, GL_DEPTH_COMPONENT);
109  }catch (glfException& e) {
110  std::cout << "ImageWarper depthTexture error: " << e.GetMessageString() << std::endl;
111  return -3;
112  }
113  try {
114  depthWarpFBO_.Create();
115  depthWarpFBO_.AttachTexture(resultTexture,GL_COLOR_ATTACHMENT0_EXT);
116  depthWarpFBO_.AttachTexture(depthTexture,GL_DEPTH_ATTACHMENT_EXT);
117  depthWarpFBO_.CheckComplete();
118  }catch (glfException& e) {
119  std::cout << "ImageWarper depthWarpFBO error: " << e.GetMessageString() << std::endl;
120  return -4;
121  }
122  //init projections
123  source_ = source;
124  target_ = target;
125 
126  try {
127  if(ignoreRadialDist_)
128  depthMeshing_.Init(source_,false);
129  else{
130  bool hasDist = true; //determine here
131  depthMeshing_.Init(source_,hasDist);
132  }
133  }catch (glfException& e) {
134  std::cout << "ImageWarper depthMeshing_.Init() error: " << e.GetMessageString() << std::endl;
135  return -5;
136  }
137  try {
138  depthMeshing_.SetRenderTarget(&depthWarpFBO_);
139  depthMeshing_.SetTargetCameraParameters(target_, minZ, maxZ);
140  // depthMeshing_.SetTargetCameraParametersToGlobal(true);
141  }catch (glfException& e) {
142  std::cout << "ImageWarper depthMeshing_.SetRenderTarget error: " << e.GetMessageString() << std::endl;
143  return -6;
144  }
145  inited_ = true;
146  // cout<<"ImageWarper Init done."<<endl;
147  return 0;
148 }
149 
150 
151 template <class StorageType> int
154  float minZ, float maxZ){
155  target_ = target;
156  target_.GetImageSize(width_, height_);
157  depthMeshing_.SetTargetCameraParameters(target_, minZ, maxZ);
158  //depthMeshing_.SetTargetCameraParametersToGlobal(true);
159  return 0;
160 }
161 
162 
163 template <class StorageType> int
166  BIAS::Image<unsigned char>& colorImage,
167  BIAS::Image<StorageType>& warpedDepthImage,
168  BIAS::Image<unsigned char>& warpedColorImage,
169  float minZ, float maxZ, bool getdepth){
170 
171  int ret =0;
172  //do not change line order here!!
174  pBuffer_.MakeCurrent();
175  depthMeshing_.UploadProjectiveTexture(colorImage);
176  ret = Process(depthImage,warpedDepthImage,minZ,maxZ,getdepth);
177  resultTexture.CopyToImage(warpedColorImage,0);
178  return ret;
179 }
180 
181 template <class StorageType> int
184  BIAS::Image<StorageType>& warpedDepthImage,
185  float minZ, float maxZ, bool getdepth){
186  // TimeMeasure timer;
187  // timer.Start();
188  if(!inited_) return -1;
189  //do not change line order here!!
191  pBuffer_.MakeCurrent();
192  glViewport(0,0, width_, height_);
193  depthWarpFBO_.ClearColorBuffer();
194  depthWarpFBO_.ClearDepthBuffer();
195  depthMeshing_.UploadDepthMap(depthImage);
196 
197  // timer.Stop();
198  // cout<<"ImageWarper::UploadDepthMap:"; timer.PrintRealTime();
199  // timer.Reset();
200  // timer.Start();
201  //
202  depthMeshing_.Draw();
203 
204  // timer.Stop();
205  // cout<<"ImageWarper::Draw:"; timer.PrintRealTime();
206  // timer.Reset();
207  // timer.Start();
208  //
209  depthTexture.CopyToImage(warpedDepthImage,0);
210 
211  // timer.Stop();
212  // cout<<"ImageWarper::CopyToImage:"; timer.PrintRealTime();
213  // timer.Reset();
214  // timer.Start();
215 
216 
217  //translate to depth
218  if(getdepth){
219  float** z = warpedDepthImage.GetImageDataArray();
220  Vector3<double> ray, pos;
221  for (unsigned int y=0; y<warpedDepthImage.GetHeight(); y++) {
222  for (unsigned int x=0; x<warpedDepthImage.GetWidth(); x++) {
223  if (z[y][x] >= 1.0) z[y][x] = 0.0;
224  else if(z[y][x] != 0.0){
225  z[y][x] = minZ*maxZ / (maxZ - (z[y][x])*(maxZ-minZ));
226  target_.UnProjectToRay(HomgPoint2D(x,y,1.0), pos, ray);
227  ray = (z[y][x] / ray[2])*ray;
228  z[y][x] = ray.NormL2();
229  }
230  }
231  }
232  // timer.Stop();
233  // cout<<"ImageWarper::Translate:"; timer.PrintRealTime();
234  // timer.Reset();
235  // timer.Start();
236  }
237 
238 
239  return 0;
240 }
241 
242 
243 ///////////////////////////////////////////////////
244 
245 template <class StorageType> void
248  //tex.SetMinFilter(GL_LINEAR_MIPMAP_LINEAR);//GL_NEAREST
249  tex.SetMinFilter(GL_LINEAR);
250  tex.SetMagFilter(GL_LINEAR);//GL_NEAREST
251  tex.SetWrapS(GL_CLAMP);
252  tex.SetWrapT(GL_CLAMP);
253 }
254 
255 template <class StorageType> void
258  unsigned width, unsigned height,
259  GLenum intFormat) {
260  tex.Create();
261  SetDefaultTextureParameters_(tex);
262  tex.Allocate(width, height, intFormat);
263 }
264 ///////////////////////////////////////////////////
265 template BIASOpenGLFramework_EXPORT class BIAS::ImageWarper<float>;
266 
void Allocate(int width, int height, GLenum internalFormat, int mipmap=0)
Creates a texture with undefined content.
A 2D texture.
Definition: glfTexture2D.hh:40
void Create()
Creates the texture but does not upload any data yet.
Definition: glfTexture.cpp:80
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
void SetWrapS(GLenum wrapS)
Sets the wrapping mode for the 1st texture coordinate.
Definition: glfTexture.cpp:105
this class warpes depth images from one persective to the perspective of another camera.
Definition: ImageWarper.hh:46
int Init(BIAS::ProjectionParametersPerspective &source, BIAS::ProjectionParametersPerspective &target, float minZ=500, float maxZ=7500.0)
Init the warping with projection parameters.
Definition: ImageWarper.cpp:50
Exception class used for run-time errors in the OpenGLFramework.
Definition: glfException.hh:79
unsigned int GetWidth() const
Definition: ImageBase.hh:312
const std::string & GetMessageString() const
Returns the description of the error including the file name and line number where the error occured...
void SetMinFilter(GLenum minFilter)
Sets the minifying function.
Definition: glfTexture.cpp:89
ImageWarper(bool ignoreRadialDistortion=false)
Definition: ImageWarper.cpp:36
void SetWrapT(GLenum wrapT)
Sets the wrapping mode for the 2nd texture coordinate.
Definition: glfTexture.cpp:113
unsigned int GetHeight() const
Definition: ImageBase.hh:319
int ProcessWithColor(BIAS::Image< StorageType > &depthImage, BIAS::Image< unsigned char > &colorImage, BIAS::Image< StorageType > &warpedDepthImage, BIAS::Image< unsigned char > &warpedColorImage, float minZ, float maxZ, bool getdepth=false)
Process one warping from source to target parameters including color texture information which has to...
The image template class for specific storage types.
Definition: Image.hh:78
int Process(BIAS::Image< StorageType > &depthImage, BIAS::Image< StorageType > &warpedDepthImage, float minZ=500, float maxZ=7500.0, bool getdepth=false)
Process one warping from source to target parameters.
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
int SetTargetCamera(BIAS::ProjectionParametersPerspective &target, float minZ=500, float maxZ=7500.0)
Configuration for a rendering context.
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633
static void SetDefaultRenderStates()
Sets all render states possibly modified by Batch to the OpenGL defaults.
Definition: glfBatch.cpp:337
class BIASGeometryBase_EXPORT HomgPoint2D
void SetMagFilter(GLenum magFilter)
Sets the magnification function.
Definition: glfTexture.cpp:97
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153