MRPT  2.0.3
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
11 #include <mrpt/img/CImage.h>
12 #include <mrpt/math/CMatrixF.h>
13 #include <mrpt/math/fourier.h>
14 #include <mrpt/system/CTicTac.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  CMatrixF 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  CMatrixF 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  CMatrixF 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.maxCoeff(), i1.minCoeff());
71 
72  // FFT:
73  CMatrixF I1_R, I1_I, I2_R, I2_I;
74  CMatrixF 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(y, x);
83  float r2 = I2_R(y, x);
84  float i1 = I1_I(y, x);
85  float i2 = I2_I(y, x);
86 
87  I2_R(y, x) = r1 * r2 - i1 * i2;
88  I2_I(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.maxCoeff(), res_R.minCoeff());
98  }
99 
100  printf("Mean time: %.06fms\n", 1000.0f * meanTime / N);
101 
102  CDisplayWindow winR("real");
103 
104  CImage imgR;
105  imgR.setFromMatrix(res_R, false /*it is not normalized */);
106  winR.showImage(imgR);
107  winR.waitForKey();
108 
109  // DEBUG_SAVE_MATRIX(res_R);
110  // DEBUG_SAVE_MATRIX(res_I);
111 }
112 
113 // ------------------------------------------------------
114 // MAIN
115 // ------------------------------------------------------
116 int main()
117 {
118  try
119  {
121 
122  return 0;
123  }
124  catch (const std::exception& e)
125  {
126  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
127  return -1;
128  }
129  catch (...)
130  {
131  printf("Untyped exception!!");
132  return -1;
133  }
134 }
mrpt::img::CImage::getWidth
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:818
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:1332
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:17
mrpt::math::MatrixVectorBase::loadFromTextFile
void loadFromTextFile(std::istream &f)
Loads a vector/matrix from a text file, compatible with MATLAB text format.
Definition: MatrixVectorBase_impl.h:238
mrpt::math::CMatrixDynamic::setSize
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
Definition: CMatrixDynamic.h:352
TestImageConvolutionFFT
void TestImageConvolutionFFT()
Definition: vision_stereo_rectify/test.cpp:30
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: BaseAppDataSource.h:15
mrpt::math::MatrixVectorBase::maxCoeff
Scalar maxCoeff() const
Maximum value in the matrix/vector.
Definition: MatrixVectorBase_impl.h:363
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::math::CMatrixF
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrixF.h:22
mrpt::img::CImage::setFromMatrix
void setFromMatrix(const MAT &m, bool matrix_is_normalized=true)
Set the image from a matrix, interpreted as grayscale intensity values, in the range [0,...
Definition: img/CImage.h:844
mrpt::img::CImage::getAsMatrix
void getAsMatrix(mrpt::math::CMatrixFloat &outMatrix, bool doResize=true, int x_min=0, int y_min=0, int x_max=-1, int y_max=-1, bool normalize_01=true) const
Returns the image as a matrix with pixel grayscale values in the range [0,1].
Definition: CImage.cpp:1238
mrpt::img
Definition: CCanvas.h:16
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:177
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:75
mrpt::gui::CDisplayWindow
This class creates a window as a graphical user interface (GUI) for displaying images to the user.
Definition: CDisplayWindow.h:33
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148
mrpt::math::MatrixVectorBase::minCoeff
Scalar minCoeff() const
Minimum value in the matrix/vector.
Definition: MatrixVectorBase_impl.h:357
mrpt::img::CImage::getHeight
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:849
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:1227
CTicTac.h
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:11
CImage.h
mrpt::exception_to_str
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
CMatrixF.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:305
CDisplayWindow.h
mrpt::system
Definition: backtrace.h:14



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Thu May 21 21:53:32 UTC 2020