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 
11 #include <mrpt/img/CImage.h>
12 #include <mrpt/math/CMatrix.h>
13 #include <mrpt/system/CTicTac.h>
14 #include <mrpt/math/ops_matrices.h>
15 #include <mrpt/math/fourier.h>
16 #include <iostream>
17 
18 using namespace mrpt;
19 using namespace mrpt::gui;
20 using namespace mrpt::img;
21 using namespace mrpt::math;
22 using namespace mrpt::system;
23 using namespace std;
24 
25 #include <mrpt/examples_config.h>
26 string myDataDir(
27  MRPT_EXAMPLES_BASE_DIRECTORY + string("img_correlation_example/"));
28 
29 void TestWindow()
30 {
31  CImage IM;
32  IM.loadFromFile(myDataDir + string("fft2_test_image_patch.jpg"));
33 
34  gui::CDisplayWindow win("Hola!");
35 
36  win.showImage(IM);
37  win.waitForKey();
38 
39  win.setPos(400, 100);
40  win.waitForKey();
41 
42  win.resize(400, 100);
43  win.waitForKey();
44 }
45 
46 // ------------------------------------------------------
47 // TestImageFFT
48 // ------------------------------------------------------
49 void TestImageFFT()
50 {
51  CTicTac tictac;
52  CImage IM1, IM2;
53  CMatrix imgCorr;
54  size_t u, v;
55  double valMax;
56  float valMaxF;
57  uint32_t nTimes;
58 
59  // ==================== 1 ===================
60  IM1.loadFromFile(
61  myDataDir + string("fft2_test_image_patch.jpg"), 0); // "Patch"
62  IM2.loadFromFile(
63  myDataDir + string("fft2_test_image.jpg"), 0); // Ref. image
64 
65  // Method I:
66  printf(
67  "Computing images correlation %ux%u (FFT)...", (unsigned)IM1.getWidth(),
68  (unsigned)IM1.getHeight());
69 
70  int searchWindow_x = 20;
71  int searchWindow_y = 20;
72  int searchWindow_lx = 64;
73  int searchWindow_ly = 64;
74  uint32_t N_TIMES = 100;
75 
76  tictac.Tic();
77  for (nTimes = 0; nTimes < N_TIMES; nTimes++)
78  {
80  IM1, imgCorr, searchWindow_x, searchWindow_y, searchWindow_lx,
81  searchWindow_ly);
82  }
83  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f / N_TIMES);
84 
85  SAVE_MATRIX(imgCorr);
86 
87  imgCorr.find_index_max_value(u, v, valMaxF);
88  u += searchWindow_x;
89  v += searchWindow_y;
90 
91  printf("Peak found at (%u,%u)=%f\n", (unsigned)u, (unsigned)v, valMaxF);
92 
93  imgCorr *= 1.0f / valMaxF;
94  CImage imFl(imgCorr, true);
95  imFl.saveToFile("_OUT_CORRELATION_FFT.png");
96 
97  // Repeat using OpenCV correlation:
98  // -----------------------------------------
99  printf(
100  "Computing images correlation %ux%u (OpenCV)...",
101  (unsigned)IM1.getWidth(), (unsigned)IM1.getHeight());
102  tictac.Tic();
103  for (nTimes = 0; nTimes < N_TIMES; nTimes++)
104  {
105  IM2.cross_correlation(
106  IM1, u, v, valMax, searchWindow_x, searchWindow_y, searchWindow_lx,
107  searchWindow_ly);
108  }
109  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f / N_TIMES);
110  printf(
111  "Peak found at (%u,%u)=%f || -image_size/2 = (%u,%u)\n", (unsigned)u,
112  (unsigned)v, valMax, (unsigned)(u - IM1.getWidth() / 2),
113  (unsigned)(v - IM1.getHeight() / 2));
114 
115  // ==================== 2 ===================
116  printf("Computing correlation (non-FFT method)...");
117  tictac.Tic();
118 
119  CImage imgCorr2;
120  size_t x_max, y_max;
121  double corr_max;
122 
123  IM2.cross_correlation(
124  IM1, x_max, y_max, corr_max, searchWindow_x, searchWindow_y,
125  searchWindow_lx, searchWindow_ly, &imgCorr2);
126 
127  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
128 
129  {
130  CDisplayWindow win("Localization of patch"), win2("patch"),
131  win3("correlation");
132 
133  CImage aux(
134  max(IM1.getWidth(), IM2.getWidth()),
135  max(IM1.getHeight(), IM2.getHeight()));
136 
137  aux.drawImage(0, 0, IM2);
138  aux.rectangle(
139  u, v, u - IM1.getWidth() / 2, v - IM1.getHeight() / 2,
140  TColor::white(), 2);
141 
142  win.showImage(aux);
143  win.setPos(30, 30);
144  win2.showImage(IM1);
145  win2.setPos(30, 400);
146  win3.showImage(imFl);
147  win3.setPos(550, 30);
148 
149  win3.waitForKey();
150  return;
151  }
152 }
153 
154 // ------------------------------------------------------
155 // TestFFT_2D_real
156 // ------------------------------------------------------
157 void TestFFT_2D_real()
158 {
159  CMatrix A, RES_R, RES_I, B, D;
160  CTicTac tictac;
161 
162  printf("Loading matrix from file...");
163  A.loadFromTextFile(myDataDir + string("fft2_test.txt"));
164  printf("ok\n");
165 
166  printf(
167  "Computing 2D FFT of %ux%u...", (unsigned int)A.rows(),
168  (unsigned int)A.cols());
169  tictac.Tic();
170  math::dft2_real(A, RES_R, RES_I);
171  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
172 
173  RES_R.saveToTextFile("_out_fft2_real.txt");
174  RES_I.saveToTextFile("_out_fft2_imag.txt");
175 
176  printf(
177  "Computing 2D IFFT of %ux%u...", (unsigned int)A.rows(),
178  (unsigned int)A.cols());
179  tictac.Tic();
180  math::idft2_real(RES_R, RES_I, B);
181  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
182 
183  // B.saveToTextFile("_out_ifft2.txt");
184  D = B - A;
185  // D.saveToTextFile("_out_fft2_error_diffs.txt");
186 
187  float maxError;
188  size_t u, v;
189  D.find_index_max_value(u, v, maxError);
190 
191  printf("Maximum error between 'A' and 'IFFT(FFT(A))'=%e\n", maxError);
192 }
193 
194 // ------------------------------------------------------
195 // TestFFT_2D_complex
196 // ------------------------------------------------------
197 void TestFFT_2D_complex()
198 {
199  CMatrix DATA_R, DATA_I, RES_R, RES_I, B_R, B_I, D_R, D_I;
200  CTicTac tictac;
201 
202  printf("Loading matrix from file...");
203  DATA_R.loadFromTextFile(myDataDir + string("complex_fft2_test_real.txt"));
204  DATA_I.loadFromTextFile(myDataDir + string("complex_fft2_test_imag.txt"));
205  printf("ok\n");
206 
207  printf(
208  "Computing 2D complex FFT of %ux%u...", (unsigned int)DATA_R.rows(),
209  (unsigned int)DATA_R.cols());
210  tictac.Tic();
211  math::dft2_complex(DATA_R, DATA_I, RES_R, RES_I);
212  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
213 
214  SAVE_MATRIX(RES_R);
215  SAVE_MATRIX(RES_I);
216 
217  printf(
218  "Computing 2D complex IFFT of %ux%u...", (unsigned int)DATA_R.rows(),
219  (unsigned int)DATA_R.cols());
220  tictac.Tic();
221  math::idft2_complex(RES_R, RES_I, B_R, B_I);
222  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
223 
224  D_R = B_R - DATA_R;
225  D_I = B_I - DATA_I;
226 
227  float maxError_R, maxError_I;
228  size_t u, v;
229  D_R.find_index_max_value(u, v, maxError_R);
230  D_I.find_index_max_value(u, v, maxError_I);
231 
232  printf("Maximum error between 'A' and 'IFFT(FFT(A))'=%e\n", maxError_R);
233  printf("Maximum error between 'A' and 'IFFT(FFT(A))'=%e\n", maxError_I);
234 }
235 
236 // ------------------------------------------------------
237 // MAIN
238 // ------------------------------------------------------
239 int main()
240 {
241  try
242  {
243  // TestFFT_2D_real();
244  // TestFFT_2D_complex();
245  TestImageFFT();
246  // TestWindow();
247 
248  return 0;
249  }
250  catch (std::exception& e)
251  {
252  std::cout << "MRPT exception caught: " << e.what() << std::endl;
253  return -1;
254  }
255  catch (...)
256  {
257  printf("Untyped exception!!");
258  return -1;
259  }
260 }
mrpt::img::CImage::getWidth
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:864
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
SAVE_MATRIX
#define SAVE_MATRIX(M)
A useful macro for saving matrixes to a file while debugging.
Definition: ops_matrices.h:159
CMatrix.h
mrpt::img::CImage::cross_correlation
void cross_correlation(const CImage &patch_img, size_t &u_max, size_t &v_max, double &max_val, int u_search_ini=-1, int v_search_ini=-1, int u_search_size=-1, int v_search_size=-1, CImage *out_corr_image=nullptr) const
Computes the correlation between this image and another one, encapsulating the openCV function cvMatc...
Definition: CImage.cpp:1454
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
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
ops_matrices.h
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
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::img::CImage::getHeight
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:892
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
CTicTac.h
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
TestWindow
void TestWindow()
Definition: vision_stereo_rectify/test.cpp:29
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
CDisplayWindow.h
uint32_t
unsigned __int32 uint32_t
Definition: rptypes.h:47
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