Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasGenerateCheckerboardModel.cpp
1 #include <Base/Common/BIASpragma.hh>
2 #include <bias_config.h>
3 
4 #include <stdio.h>
5 #include <map>
6 #include <string>
7 
8 #include <Base/Image/ImageBase.hh>
9 #include <Base/Image/ImageIO.hh>
10 #include <Geometry/Projection.hh>
11 #include <Utils/TriangleMesh.hh>
12 #include <Utils/ThreeDOut.hh>
13 #include <Base/Image/ImageConvert.hh>
14 #include <Base/Math/Vector3.hh>
15 #include <Filter/Gauss.hh>
16 
17 #ifdef BIAS_HAVE_OPENSCENEGRAPH
18  #include <GLviewer/ThreeDOutOpenSceneGraph.hh>
19 #endif
20 
21 using namespace BIAS;
22 using namespace std;
23 
24 //forward declarations
25 void usage(int argc, char* argv[]);
26 void GenerateCheckerboard(int size,int xCorners, int yCorners);
27 void GenerateIntensityTestCheckerboard(int size,int xCorners, int yCorners,float maxDev);
28 
29 
30 /**
31  @file biasGenerateCheckerboardModel
32  @ingroup g_tools
33  @brief generates a checkerboard vrml file
34  @author ischiller
35 */
36 int main (int argc, char *argv[])
37 {
38  if(argc<4){
39  usage(argc, argv);
40  return -1;
41  }
42 
43  bool intensity = false;
44  float maxDev = 0.0;
45 
46  int cols = atoi(argv[1]);
47  int rows = atoi(argv[2]);
48  int size = atoi(argv[3]);
49 
50  if(argc > 4) intensity = bool(atoi(argv[4]) != 0);
51  if(argc > 5) maxDev = atof(argv[5]);
52 
53 
54  if(intensity){
55  cout<<"Intensity with MaxDev:"<<maxDev<<endl;
56  GenerateIntensityTestCheckerboard(size,cols, rows, maxDev);
57  }
58  else{
59  cout<<"Standard:"<<endl;
60  GenerateCheckerboard(size,cols, rows);
61  }
62 }
63 ///////////////////////////////////////////////////////////////////////
64 void usage(int argc, char* argv[])
65 {
66  cout<<"This program will create a checkerboard model and write it to a file checkerboard.wrl. "<<
67  "If OpenSceneGraph support is activated in BIAS it will also write corresponding files with "<<
68  "suffixes .ive, .3ds .osg\n";
69  cout<<"ATTENTION: The ThreeDOut.AddImage() method has a bug therefore the textures in models .ive, .3ds and "<<
70  ".osg models are wrong. (ischiller 29/06/2011).\n\n";
71  cout<<"Usage:"<<argv[0]<<" cols rows size [intensityVariable] [maxDeviation]\n"<<endl;
72 }
73 ///////////////////////////////////////////////////////////////////////
74 void GenerateCheckerboard(int size,int xCorners, int yCorners){
75 
76  BIAS::Vector3<unsigned int> UL,UR,LL,LR;
77  BIAS::Image<unsigned char> black,white;
79  unsigned int factor = 20;
80  BIAS::ThreeDOut threedout;
81  Vector3<double> ul(-size,-size,0);
82  Vector3<double> ur((xCorners)*size,-size,0);
83  Vector3<double> ll(-size,(yCorners)*size,0);
84  Vector3<double> lr((xCorners)*size,(yCorners)*size,0);
85 
86 
87  black.Init(factor,factor,1);
88  black.FillImageWithConstValue((unsigned char)0);
89  white.Init(factor,factor,1);
90  white.FillImageWithConstValue((unsigned char)255);
91 
92  // The checkerboard image
93  if(!image.IsEmpty()) image.Release();
94  unsigned newWidth=(unsigned)((xCorners+1)*factor);
95  unsigned newHeight=(unsigned)((yCorners+1)*factor);
96 
97  image.Init(newWidth,newHeight,1);
98 
99  bool topLeftBlack = true;
100  bool isBlack = topLeftBlack;
101 
102  for(int y=0;y < yCorners+1; y++){ //rows
103  for(int x= 0;x< xCorners+1 ; x++){ //cols
104 
105  if((y % 2) == 0 && x == 0)
106  isBlack = topLeftBlack;
107  else if(x == 0)
108  isBlack = !topLeftBlack;
109 
110  UL[0]=x*factor;
111  UL[1]=y*factor;
112  UL[2]=0;
113  LR[0]=(x+1)*factor;
114  LR[1]=(y+1)*factor;
115  LR[2]=0;
116 
117  image.SetROICorners(UL[0],UL[1], LR[0],LR[1]);
118 
119  if(isBlack)
120  image.Paste2ROI(black);
121  else
122  image.Paste2ROI(white);
123 
124  isBlack = !isBlack;
125  }
126  }
127 
128 
129  BIAS::ImageIO::Save("checkerboard.jpg",image);
130 
131  unsigned char *pData = image.GetImageData();
132  unsigned xSize = (unsigned)rint(image.GetWidth());
133  unsigned ySize = (unsigned)rint(image.GetHeight());
134  threedout.AddImage(ul, ur,ll,lr,xSize ,ySize,pData,1,"cb",false);
135 
136  threedout.VRMLOut("checkerboard.wrl");
137 #ifdef BIAS_HAVE_OPENSCENEGRAPH
139  t3dOSG.AddImage(ul, ur,ll,lr,xSize ,ySize,pData,1,"cb",false);
140  t3dOSG.OpenSceneGraphOut("checkerboard.osg");
141  t3dOSG.OpenSceneGraphOut("checkerboard.ive");
142  t3dOSG.OpenSceneGraphOut("checkerboard.3ds");
143 #endif
144 }
145 
146 ///////////////////////////////////////////////////////////////////////
147 void GenerateIntensityTestCheckerboard(int size,int xCorners, int yCorners,float maxDev)
148 {
149  BIAS::Vector3<unsigned int> UL,UR,LL,LR;
150  BIAS::Image<unsigned char> black,grey80,grey60,grey40,grey20,white;
152  BIAS::Image<float> imageD,blackD,grey80D,grey60D,grey40D,grey20D,whiteD;
153 
154  unsigned int factor = 20;
155  BIAS::ThreeDOut threedout;
156 
157  Vector3<double> ul(-size,-size,0);
158  Vector3<double> ur((xCorners)*size,-size,0);
159  Vector3<double> ll(-size,(yCorners)*size,0);
160  Vector3<double> lr((xCorners)*size,(yCorners)*size,0);
161 
162  black.Init(factor,factor,1);
163  black.FillImageWithConstValue((unsigned char)0);
164  grey80.Init(factor,factor,1);
165  grey80.FillImageWithConstValue((unsigned char)(255.0*0.20));
166  grey60.Init(factor,factor,1);
167  grey60.FillImageWithConstValue((unsigned char)(255.0*0.40));
168  grey40.Init(factor,factor,1);
169  grey40.FillImageWithConstValue((unsigned char)(255.0*0.60));
170  grey20.Init(factor,factor,1);
171  grey20.FillImageWithConstValue((unsigned char)(255.0*0.80));
172  white.Init(factor,factor,1);
173  white.FillImageWithConstValue((unsigned char)255);
174 
175  //fill depth images, black comes to front, white is at back plane
176  if(maxDev<=0) maxDev =0.0;
177  blackD.Init(factor,factor,1);
178  blackD.FillImageWithConstValue(maxDev);
179  grey80D.Init(factor,factor,1);
180  grey80D.FillImageWithConstValue(maxDev*0.80);
181  grey60D.Init(factor,factor,1);
182  grey60D.FillImageWithConstValue(maxDev*0.60);
183  grey40D.Init(factor,factor,1);
184  grey40D.FillImageWithConstValue(maxDev*0.40);
185  grey20D.Init(factor,factor,1);
186  grey20D.FillImageWithConstValue(maxDev*0.20);
187  whiteD.Init(factor,factor,1);
188  whiteD.FillImageWithConstValue(1.0);
189 
190  // The checkerboard image
191  unsigned newWidth=(unsigned)((xCorners+1)*factor);
192  unsigned newHeight=(unsigned)((yCorners+1)*factor);
193 
194  image.Init(newWidth,newHeight,1);
195  imageD.Init(newWidth,newHeight,1);
196 
197  bool topLeftBlack = true;
198  bool isBlack = topLeftBlack;
199  unsigned blackCounter=0;
200  for(int y=0;y < yCorners+1; y++){ //rows
201  for(int x= 0;x< xCorners+1 ; x++){ //cols
202 
203  if((y % 2) == 0 && x == 0){
204  isBlack = topLeftBlack;
205  }
206  else if(x == 0)
207  isBlack = !topLeftBlack;
208 
209  UL[0]=x*factor;
210  UL[1]=y*factor;
211  UL[2]=0;
212  LR[0]=(x+1)*factor;
213  LR[1]=(y+1)*factor;
214  LR[2]=0;
215 
216  image.SetROICorners(UL[0],UL[1], LR[0],LR[1]);
217  imageD.SetROICorners(UL[0],UL[1], LR[0],LR[1]);
218 
219  if(isBlack){
220  if( y!=0 && y!=yCorners && x != 0 && x!=xCorners){
221  switch(blackCounter){
222  case 0:
223  image.Paste2ROI(black);
224  imageD.Paste2ROI(blackD);
225  break;
226  case 1:
227  image.Paste2ROI(grey80);
228  imageD.Paste2ROI(grey80D);
229  break;
230  case 2:
231  image.Paste2ROI(grey60);
232  imageD.Paste2ROI(grey60D);
233  break;
234  case 3:
235  image.Paste2ROI(grey40);
236  imageD.Paste2ROI(grey40D);
237  break;
238  case 4:
239  image.Paste2ROI(grey20);
240  imageD.Paste2ROI(grey20D);
241  break;
242  default:
243  BIASERR("blackCounter out of bounds");
244  }
245  blackCounter++;
246  if(blackCounter == 5)
247  blackCounter =0;
248  }
249  else{
250  image.Paste2ROI(black);
251  imageD.Paste2ROI(blackD);
252  }
253  }
254  else{
255  image.Paste2ROI(white);
256  imageD.Paste2ROI(whiteD);
257  }
258  isBlack = !isBlack;
259  }
260  }
261  BIAS::ImageIO::Save("checkerboard_distorted.mip",image);
262 
263  /*
264  BIAS::Gauss<float,float> gauss;
265  gauss.Filter7x7Grey(imageD,imageD);
266  BIAS::ImageIO::Save("checkerboardGaussFiltered.mip",imageD);
267 
268  TriangleMesh mesh(0.0,360.0,1);
269  unsigned w = 800;
270  unsigned h = 600;
271  float f = 470.0;
272  KMatrix K;K.Set(f,f,w/2.0,h/2.0);
273  Projection P;
274  P.CreatePerspective(Pose(),K,w,h);*/
275 
276  unsigned char *pData = image.GetImageData();
277 
278  unsigned xSize = (unsigned)rint(image.GetWidth());
279  unsigned ySize = (unsigned)rint(image.GetHeight());
280 
281  threedout.AddImage(ul, ur,ll,lr,xSize ,ySize,pData,1,"cb",false);
282  threedout.VRMLOut("checkerboard_distorted.wrl");
283 
284 #ifdef BIAS_HAVE_OPENSCENEGRAPH
286  t3dOSG.AddImage(ul, ur,ll,lr,xSize ,ySize,pData,1,"cb",false);
287  t3dOSG.OpenSceneGraphOut("checkerboard_distorted.3ds");
288  t3dOSG.OpenSceneGraphOut("checkerboard_distorted.ive");
289  t3dOSG.OpenSceneGraphOut("checkerboard_distorted.osg");
290 #endif
291 }
292 
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
int VRMLOut(const std::string &sFilename)
flush all 3d objects to a vrml file with name sFilename, this is the function most users would call ...
Definition: ThreeDOut.cpp:3670
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
int OpenSceneGraphOut(const std::string &filename) const
save ThreeDOut object as OpenSceneGraph saves an Open Scene Graph and the needed textures ...
Unified output of 3D entities via OpenGL or VRML.
Definition: ThreeDOut.hh:349
unsigned int AddImage(const Vector3< double > &UL, const Vector3< double > &UR, const Vector3< double > &LL, unsigned int Width, unsigned int Height, unsigned char *pData, unsigned int channels=1, const std::string &name="", bool billboard=false)
add a small rectangular image patch spanned by three 3d points small means really small (only a few p...
Definition: ThreeDOut.hh:512
unsigned int GetWidth() const
Definition: ImageBase.hh:312
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
a class for exporting ThreeDOut objects to OSG scene graphs
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
int Paste2ROI(const ImageBase &Image)
paste Image to current ROI
Definition: ImageBase.cpp:603
int SetROICorners(unsigned int UpperLeftX, unsigned int UpperLeftY, unsigned int LowerRightX, unsigned int LowerRightY)
Definition: ImageBase.cpp:1048