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/math/CMatrix.h>
12 #include <mrpt/math/fourier.h>
13 #include <mrpt/system/CTicTac.h>
14 #include <mrpt/img/CImage.h>
15 #include <iostream>
16 
17 using namespace mrpt;
18 using namespace mrpt::img;
19 using namespace mrpt::gui;
20 using namespace mrpt::math;
21 using namespace mrpt::system;
22 using namespace std;
23 
24 #include <mrpt/examples_config.h>
25 string myDataDir(MRPT_EXAMPLES_BASE_DIRECTORY + string("img_convolution_fft/"));
26 
27 // ------------------------------------------------------
28 // TestImageConvolutionFFT
29 // ------------------------------------------------------
31 {
32  CTicTac tictac;
33  CImage img;
34  CMatrix imgCorr;
35 
36  // ==================== 1 ===================
37  if (!img.loadFromFile(myDataDir + string("test_image.jpg")))
38  throw std::runtime_error("Cannot load test image!");
39 
40  printf(
41  "Computing %ux%u image convolution ...", (unsigned)img.getWidth(),
42  (unsigned)img.getHeight());
43 
44  CMatrix res_R, res_I;
45 
46  double meanTime = 0;
47 
48  int N = 3;
49 
50  // Convolution using FFT 2D:
51  for (int nTimes = 0; nTimes < N; nTimes++)
52  {
53  tictac.Tic();
54 
55  size_t x, y;
56  size_t actual_lx = img.getWidth();
57  size_t actual_ly = img.getHeight();
58  size_t lx = mrpt::round2up(actual_lx);
59  size_t ly = mrpt::round2up(actual_ly);
60 
61  CMatrix i1(ly, lx), i2;
62  // Get as matrixes, padded with zeros up to power-of-two sizes:
63  img.getAsMatrix(i1, false);
64 
65  // imgWindow.getAsMatrix(i2,false);
66  i2.loadFromTextFile(myDataDir + string("test_convolution_window.txt"));
67  i2.setSize(ly, lx);
68 
69  if (nTimes == 0)
70  printf("\nMax real:%f Min real:%f\n", i1.maximum(), i1.minimum());
71 
72  // FFT:
73  CMatrix I1_R, I1_I, I2_R, I2_I;
74  CMatrix ZEROS(ly, lx);
75  math::dft2_complex(i1, ZEROS, I1_R, I1_I);
76  math::dft2_complex(i2, ZEROS, I2_R, I2_I);
77 
78  // Compute the COMPLEX MULTIPLICATION of I2 by I1:
79  for (y = 0; y < ly; y++)
80  for (x = 0; x < lx; x++)
81  {
82  float r1 = I1_R.get_unsafe(y, x);
83  float r2 = I2_R.get_unsafe(y, x);
84  float i1 = I1_I.get_unsafe(y, x);
85  float i2 = I2_I.get_unsafe(y, x);
86 
87  I2_R.set_unsafe(y, x, r1 * r2 - i1 * i2);
88  I2_I.set_unsafe(y, x, r2 * i1 + r1 * i2);
89  }
90 
91  // IFFT:
92  math::idft2_complex(I2_R, I2_I, res_R, res_I);
93  res_R *= 1.0f; // SCALE!
94 
95  meanTime += tictac.Tac();
96  printf(" Done,%.06fms\n", tictac.Tac() * 1000.0f);
97  printf("Max real:%f Min real:%f\n", res_R.maximum(), res_R.minimum());
98  }
99 
100  printf("Mean time: %.06fms\n", 1000.0f * meanTime / N);
101 
102  CDisplayWindow winR("real");
103 
104  res_R.adjustRange(0, 1);
105 
106  CImage imgR(res_R, true);
107  winR.showImage(imgR);
108  winR.waitForKey();
109 
110  // DEBUG_SAVE_MATRIX(res_R);
111  // DEBUG_SAVE_MATRIX(res_I);
112 }
113 
114 // ------------------------------------------------------
115 // MAIN
116 // ------------------------------------------------------
117 int main()
118 {
119  try
120  {
122 
123  return 0;
124  }
125  catch (std::exception& e)
126  {
127  std::cout << "MRPT exception caught: " << e.what() << std::endl;
128  return -1;
129  }
130  catch (...)
131  {
132  printf("Untyped exception!!");
133  return -1;
134  }
135 }
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
TestImageConvolutionFFT
void TestImageConvolutionFFT()
Definition: vision_stereo_rectify/test.cpp:30
CMatrix.h
myDataDir
std::string myDataDir
Definition: vision_stereo_rectify/test.cpp:23
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
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::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::round2up
T round2up(T val)
Round up to the nearest power of two of a given number.
Definition: core/include/mrpt/core/bits_math.h:156
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::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::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
CImage.h
CDisplayWindow.h
y
GLenum GLint GLint y
Definition: glext.h:3538
x
GLenum GLint x
Definition: glext.h:3538
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