25 #include <mrpt/examples_config.h> 27 MRPT_EXAMPLES_BASE_DIRECTORY +
string(
"img_correlation_example/"));
61 myDataDir +
string(
"fft2_test_image_patch.jpg"), 0);
63 myDataDir +
string(
"fft2_test_image.jpg"), 0);
67 "Computing images correlation %ux%u (FFT)...", (
unsigned)IM1.
getWidth(),
70 int searchWindow_x = 20;
71 int searchWindow_y = 20;
72 int searchWindow_lx = 64;
73 int searchWindow_ly = 64;
77 for (nTimes = 0; nTimes < N_TIMES; nTimes++)
80 IM1, imgCorr, searchWindow_x, searchWindow_y, searchWindow_lx,
83 printf(
" Done,%.06fms\n", tictac.
Tac() * 1000.0f / N_TIMES);
87 imgCorr.find_index_max_value(u,
v, valMaxF);
91 printf(
"Peak found at (%u,%u)=%f\n", (
unsigned)u, (
unsigned)
v, valMaxF);
93 imgCorr *= 1.0f / valMaxF;
94 CImage imFl(imgCorr,
true);
95 imFl.saveToFile(
"_OUT_CORRELATION_FFT.png");
100 "Computing images correlation %ux%u (OpenCV)...",
103 for (nTimes = 0; nTimes < N_TIMES; nTimes++)
106 IM1, u,
v, valMax, searchWindow_x, searchWindow_y, searchWindow_lx,
109 printf(
" Done,%.06fms\n", tictac.
Tac() * 1000.0f / N_TIMES);
111 "Peak found at (%u,%u)=%f || -image_size/2 = (%u,%u)\n", (
unsigned)u,
112 (
unsigned)
v, valMax, (
unsigned)(u - IM1.
getWidth() / 2),
116 printf(
"Computing correlation (non-FFT method)...");
124 IM1, x_max, y_max, corr_max, searchWindow_x, searchWindow_y,
125 searchWindow_lx, searchWindow_ly, &imgCorr2);
127 printf(
" Done,%.06fms\n", tictac.
Tac() * 1000.0f);
137 aux.drawImage(0, 0, IM2);
145 win2.setPos(30, 400);
146 win3.showImage(imFl);
147 win3.setPos(550, 30);
162 printf(
"Loading matrix from file...");
163 A.loadFromTextFile(
myDataDir +
string(
"fft2_test.txt"));
167 "Computing 2D FFT of %ux%u...", (
unsigned int)A.rows(),
168 (
unsigned int)A.cols());
171 printf(
" Done,%.06fms\n", tictac.
Tac() * 1000.0f);
173 RES_R.saveToTextFile(
"_out_fft2_real.txt");
174 RES_I.saveToTextFile(
"_out_fft2_imag.txt");
177 "Computing 2D IFFT of %ux%u...", (
unsigned int)A.rows(),
178 (
unsigned int)A.cols());
181 printf(
" Done,%.06fms\n", tictac.
Tac() * 1000.0f);
189 D.find_index_max_value(u,
v, maxError);
191 printf(
"Maximum error between 'A' and 'IFFT(FFT(A))'=%e\n", maxError);
199 CMatrix DATA_R, DATA_I, RES_R, RES_I, B_R, B_I, D_R, D_I;
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"));
208 "Computing 2D complex FFT of %ux%u...", (
unsigned int)DATA_R.rows(),
209 (
unsigned int)DATA_R.cols());
212 printf(
" Done,%.06fms\n", tictac.
Tac() * 1000.0f);
218 "Computing 2D complex IFFT of %ux%u...", (
unsigned int)DATA_R.rows(),
219 (
unsigned int)DATA_R.cols());
222 printf(
" Done,%.06fms\n", tictac.
Tac() * 1000.0f);
227 float maxError_R, maxError_I;
229 D_R.find_index_max_value(u,
v, maxError_R);
230 D_I.find_index_max_value(u,
v, maxError_I);
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);
250 catch (std::exception& e)
252 std::cout <<
"MRPT exception caught: " << e.what() << std::endl;
257 printf(
"Untyped exception!!");
double Tac() noexcept
Stops the stopwatch.
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...
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...
#define SAVE_MATRIX(M)
A useful macro for saving matrixes to a file while debugging.
A high-performance stopwatch, with typical resolution of nanoseconds.
size_t getHeight() const override
Returns the height of the image in pixels.
This file implements miscelaneous matrix and matrix/vector operations, and internal functions in mrpt...
bool loadFromFile(const std::string &fileName, int isColor=-1)
Load image from a file, whose format is determined from the extension (internally uses OpenCV)...
This base provides a set of functions for maths stuff.
size_t getWidth() const override
Returns the width of the image in pixels.
This class creates a window as a graphical user interface (GUI) for displaying images to the user...
mrpt::gui::CDisplayWindow3D::Ptr win
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.
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).
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void idft2_real(const CMatrixFloat &in_real, const CMatrixFloat &in_imag, CMatrixFloat &out_data)
Compute the 2D inverse Discrete Fourier Transform (DFT)
void TestFFT_2D_complex()
Classes for creating GUI windows for 2D and 3D visualization.
void Tic() noexcept
Starts the stopwatch.
This class is a "CSerializable" wrapper for "CMatrixFloat".
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...
unsigned __int32 uint32_t
A class for storing images as grayscale or RGB bitmaps.