Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/gui.h>
11 #include <mrpt/math/CMatrix.h>
12 #include <mrpt/img/CImage.h>
13 #include <mrpt/system/CTicTac.h>
14 #include <mrpt/math/fourier.h>
15 #include <iostream>
16 
17 using namespace mrpt;
18 using namespace mrpt::math;
19 using namespace mrpt::gui;
20 using namespace mrpt::system;
21 using namespace mrpt::img;
22 using namespace std;
23 
24 #include <mrpt/examples_config.h>
25 string myDataDir(
26  MRPT_EXAMPLES_BASE_DIRECTORY +
27  string("img_correlation_example/")); // Reuse it's images
28 
29 // ------------------------------------------------------
30 // TestFFT_2D_real
31 // ------------------------------------------------------
32 void TestFFT_2D_real()
33 {
34  CMatrix A, RES_R, RES_I, B, D;
35  CTicTac tictac;
36 
37  printf("Loading matrix from file...");
38  A.loadFromTextFile("dft2_test.txt");
39  printf("ok\n");
40 
41  printf(
42  "Computing 2D FFT of %ux%u...", (unsigned int)A.rows(),
43  (unsigned int)A.cols());
44  tictac.Tic();
45  math::dft2_real(A, RES_R, RES_I);
46  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
47 
48  RES_R.saveToTextFile("_out_dft2_real.txt");
49  RES_I.saveToTextFile("_out_dft2_imag.txt");
50 
51  printf(
52  "Computing 2D IFFT of %ux%u...", (unsigned int)A.rows(),
53  (unsigned int)A.cols());
54  tictac.Tic();
55  math::idft2_real(RES_R, RES_I, B);
56  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
57 
58  // B.saveToTextFile("_out_ifft2.txt");
59  D = B - A;
60  // D.saveToTextFile("_out_dft2_error_diffs.txt");
61 
62  float maxError;
63  size_t u, v;
64  D.find_index_max_value(u, v, maxError);
65 
66  printf("Maximum error between 'A' and 'IFFT(FFT(A))'=%e\n", maxError);
67 }
68 
69 // ------------------------------------------------------
70 // TestFFT_2D_complex
71 // ------------------------------------------------------
72 void TestFFT_2D_complex()
73 {
74  CMatrix DATA_R, DATA_I, RES_R, RES_I, B_R, B_I, D_R, D_I;
75  CTicTac tictac;
76 
77  printf("Loading matrix from file...");
78  DATA_R.loadFromTextFile("complex_dft2_test_real.txt");
79  DATA_I.loadFromTextFile("complex_dft2_test_imag.txt");
80  printf("ok\n");
81 
82  printf(
83  "Computing 2D complex FFT of %ux%u...", (unsigned int)DATA_R.rows(),
84  (unsigned int)DATA_R.cols());
85  tictac.Tic();
86  math::dft2_complex(DATA_R, DATA_I, RES_R, RES_I);
87  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
88 
89  RES_R.saveToTextFile("_out_complex_dft2_real.txt");
90  RES_I.saveToTextFile("_out_complex_dft2_imag.txt");
91 
92  printf(
93  "Computing 2D complex IFFT of %ux%u...", (unsigned int)DATA_R.rows(),
94  (unsigned int)DATA_R.cols());
95  tictac.Tic();
96  math::idft2_complex(RES_R, RES_I, B_R, B_I);
97  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
98 
99  // B.saveToTextFile("_out_ifft2.txt");
100  D_R = B_R - DATA_R;
101  D_I = B_I - DATA_I;
102  // D.saveToTextFile("_out_dft2_error_diffs.txt");
103 
104  float maxError_R, maxError_I;
105  size_t u, v;
106  D_R.find_index_max_value(u, v, maxError_R);
107  D_I.find_index_max_value(u, v, maxError_I);
108 
109  printf("Maximum error between 'A' and 'IFFT(FFT(A))'=%e\n", maxError_R);
110  printf("Maximum error between 'A' and 'IFFT(FFT(A))'=%e\n", maxError_I);
111 }
112 
113 // ------------------------------------------------------
114 // TestImageFFT
115 // ------------------------------------------------------
116 void TestImageFFT()
117 {
118  CTicTac tictac;
119  CImage IM1, IM2;
120  CMatrix imgCorr;
121 
122  IM1.loadFromFile(
123  myDataDir + string("fft2_test_image_patch.jpg"), 0); // "Patch"
124  IM2.loadFromFile(
125  myDataDir + string("fft2_test_image.jpg"), 0); // Ref. image
126 
127  printf("Computing images correlation...");
128  tictac.Tic();
129  IM2.cross_correlation_FFT(IM1, imgCorr);
130  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
131 
132  imgCorr.saveToTextFile("_out_dft2_image_test.txt");
133 }
134 
135 // ------------------------------------------------------
136 // TestImageCap
137 // ------------------------------------------------------
138 void TestImage3D()
139 {
140  /* // Pixels -> 3D
141  CMatrix A = VisionUtils::defaultIntrinsicParamsMatrix();
142  CPoint3D p;
143 
144  FILE *f=fopen("test.txt","wt");
145  for (int x=0;x<320;x+=10)
146  for (int y=0;y<240;y+=10)
147  for (int d=1;d<20;d+=1)
148  {
149  p = VisionUtils::pixelTo3D(x,y, A);
150  fprintf(f,"%f %f %f\n",p.x,p.y,p.z);
151  }
152 
153  fclose(f);
154 
155  return;
156  */
157 }
158 
159 // ------------------------------------------------------
160 // TestImageCap
161 // ------------------------------------------------------
162 void TestImageConversion()
163 {
164  // BMP -> JPEG conversion tester:
165  // --------------------------------
166  CImage img, img2;
167  CTicTac tictac;
168 
169  {
170  tictac.Tic();
171  if (!img.loadFromFile("../img_basic_example/frame_color.bmp"))
172  {
173  cerr << "Error loading ../img_basic_example/frame_color.bmp"
174  << endl;
175  return;
176  }
177  printf("bmp file loaded in %.03fms\n", 1000.0f * tictac.Tac());
178 
179  CDisplayWindow win1("BMP file, color");
180  win1.showImage(img);
181  win1.waitForKey();
182 
183  tictac.Tic();
184  img.loadFromFile("frame_gray.bmp");
185  printf("bmp file loaded in %.03fms\n", 1000.0f * tictac.Tac());
186 
187  CDisplayWindow win2("BMP file, gray");
188 
189  win2.showImage(img);
190  win2.waitForKey();
191  }
192 
193  tictac.Tic();
194  img.loadFromFile("frame.jpg");
195  printf("jpeg file loaded in %.03fms\n", 1000.0f * tictac.Tac());
196 
197  CDisplayWindow win1("win1"), win2("win2"), win3("win3");
198 
199  CImage imgSmall(img.scaleHalf());
200  CImage imgSmall2(imgSmall.scaleHalf());
201  CImage imgGray(imgSmall2.grayscale());
202 
203  // Test some draw capabilities:
204  // ---------------------------------
205  imgSmall.rectangle(85, 35, 170, 170, TColor(255, 0, 0), 10);
206 
207  imgSmall.line(550, 75, 650, 25, TColor(0, 0, 255));
208  imgSmall.line(-10, -20, 20, 30, TColor(0, 0, 255));
209 
210  CMatrix COV(2, 2);
211  COV(0, 0) = 100;
212  COV(1, 1) = 50;
213  COV(0, 1) = COV(1, 0) = -30;
214  imgSmall.ellipseGaussian(&COV, 600.0f, 50.0f, 2, TColor(255, 255, 0), 4);
215  imgGray.ellipseGaussian(&COV, 100.0f, 100.0f, 2, TColor(0, 0, 255), 4);
216 
217  imgSmall.drawImage(400, 500, imgGray);
218 
219  // Show the windows now:
220  // ------------------------------------------------------
221  win1.showImage(imgSmall);
222  win1.setPos(0, 0);
223  win2.showImage(imgSmall2);
224  win2.setPos(810, 0);
225  win3.showImage(imgGray);
226  win3.setPos(810, 400);
227 
228  os::getch();
229 
230  tictac.Tic();
231  img2.saveToFile("frame_out.jpg");
232  printf("jpeg file loaded in %.03fms\n", 1000.0f * tictac.Tac());
233 
234  return;
235 }
236 
237 // ------------------------------------------------------
238 // TestImageCap
239 // ------------------------------------------------------
240 /*void TestImageCap()
241 {
242  CTicTac tictac;
243  bool ok = true;
244 
245  CImageGrabber_OpenCV cap( 0 );
246  CObservationImage obs;
247  CDisplayWindow win("Capture");
248 
249 
250  while (ok && !_kbhit())
251  {
252  std::this_thread::sleep_for(10ms);
253  tictac.Tic();
254  ok = cap.getObservation( obs );
255  printf("Frame grabbed in %.03fms\n", 1000.0f*tictac.Tac() );
256 
257 
258  if (ok)
259  {
260 // CImageFloat img2; img2 = obs.image;
261 // win.showImage( obs.image.grayscale() );
262 
263  win.showImage( obs.image );
264  }
265  };
266 
267 
268  if (_kbhit()) _getch();
269 }
270 */
271 // ------------------------------------------------------
272 // MAIN
273 // ------------------------------------------------------
274 int main()
275 {
276  try
277  {
278  TestImageFFT();
279 
280  // TestFFT_2D_real();
281  // TestFFT_2D_complex();
282  // TestImageFFT();
283  // TestImageCap();
284  // TestImageConversion();
285 
286  return 0;
287  }
288  catch (std::exception& e)
289  {
290  std::cout << "MRPT exception caught: " << e.what() << std::endl;
291  return -1;
292  }
293  catch (...)
294  {
295  printf("Untyped exception!!");
296  return -1;
297  }
298 }
mrpt::math::dft2_real
void dft2_real(const CMatrixFloat &in_data, CMatrixFloat &out_real, CMatrixFloat &out_imag)
Compute the 2D Discrete Fourier Transform (DFT) of a real matrix, returning the real and imaginary pa...
Definition: fourier.cpp:970
fourier.h
mrpt::math::idft2_complex
void idft2_complex(const CMatrixFloat &in_real, const CMatrixFloat &in_imag, CMatrixFloat &out_real, CMatrixFloat &out_imag)
Compute the 2D inverse Discrete Fourier Transform (DFT).
Definition: fourier.cpp:1337
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
CMatrix.h
myDataDir
std::string myDataDir
Definition: vision_stereo_rectify/test.cpp:23
TestFFT_2D_real
void TestFFT_2D_real()
Definition: vision_stereo_rectify/test.cpp:157
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
TestImageFFT
void TestImageFFT()
Definition: vision_stereo_rectify/test.cpp:49
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
v
const GLdouble * v
Definition: glext.h:3678
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::img
Definition: CCanvas.h:17
mrpt::math::CMatrix
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:24
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:22
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::gui::CDisplayWindow
This class creates a window as a graphical user interface (GUI) for displaying images to the user.
Definition: CDisplayWindow.h:30
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
mrpt::img::CImage::cross_correlation_FFT
void cross_correlation_FFT(const CImage &in_img, math::CMatrixFloat &out_corr, int u_search_ini=-1, int v_search_ini=-1, int u_search_size=-1, int v_search_size=-1, float biasThisImg=0, float biasInImg=0) const
Computes the correlation matrix between this image and another one.
Definition: CImage.cpp:1738
mrpt::img::CImage::saveToFile
bool saveToFile(const std::string &fileName, int jpeg_quality=95) const
Save the image to a file, whose format is determined from the extension (internally uses OpenCV).
Definition: CImage.cpp:296
TestFFT_2D_complex
void TestFFT_2D_complex()
Definition: vision_stereo_rectify/test.cpp:197
mrpt::math::idft2_real
void idft2_real(const CMatrixFloat &in_real, const CMatrixFloat &in_imag, CMatrixFloat &out_data)
Compute the 2D inverse Discrete Fourier Transform (DFT)
Definition: fourier.cpp:1070
mrpt::math::dft2_complex
void dft2_complex(const CMatrixFloat &in_real, const CMatrixFloat &in_imag, CMatrixFloat &out_real, CMatrixFloat &out_imag)
Compute the 2D Discrete Fourier Transform (DFT) of a complex matrix, returning the real and imaginary...
Definition: fourier.cpp:1232
img
GLint GLvoid * img
Definition: glext.h:3763
CTicTac.h
mrpt::system::os::getch
int getch() noexcept
An OS-independent version of getch, which waits until a key is pushed.
Definition: os.cpp:370
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
gui.h
CImage.h
mrpt::img::CImage::loadFromFile
bool loadFromFile(const std::string &fileName, int isColor=-1)
Load image from a file, whose format is determined from the extension (internally uses OpenCV).
Definition: CImage.cpp:271
TestImageConversion
void TestImageConversion()
Definition: vision_stereo_rectify/test.cpp:29
TestImage3D
void TestImage3D()
Definition: vision_stereo_rectify/test.cpp:138
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST