Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
testGaussianFilter.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test Gaussian filter.
33  *
34 *****************************************************************************/
35 
36 #include <visp3/core/vpConfig.h>
37 
44 #if defined(VISP_HAVE_SIMDLIB) && defined(VISP_HAVE_CATCH2) && (VISP_HAVE_DATASET_VERSION >= 0x030400)
45 #define CATCH_CONFIG_RUNNER
46 #include <catch.hpp>
47 #include <visp3/core/vpGaussianFilter.h>
48 #include <visp3/core/vpImageTools.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/io/vpImageIo.h>
51 
52 TEST_CASE("Test vpGaussianFilter (unsigned char)")
53 {
54  const std::string filepath = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "Klimt/Klimt.pgm");
56  vpImageIo::read(I, filepath);
57 
58  std::vector<float> sigmas = { 0.5f, 2.0f, 5.0f, 7.0f };
59  for (auto sigma : sigmas) {
60  vpGaussianFilter gaussianFilter(I.getWidth(), I.getHeight(), sigma);
61 
62  vpImage<unsigned char> I_blurred;
63  gaussianFilter.apply(I, I_blurred);
64 
65  vpImage<unsigned char> I_blurred_ref;
66  const std::string filepath_ref = vpIoTools::createFilePath(
67  vpIoTools::getViSPImagesDataPath(), "Gaussian-filter/Klimt_gray_Gaussian_blur_sigma=%.1f.png");
68  char buffer[FILENAME_MAX];
69  snprintf(buffer, FILENAME_MAX, filepath_ref.c_str(), sigma);
70  const std::string filename = buffer;
71  vpImageIo::read(I_blurred_ref, filename);
72 
74  vpImageTools::imageDifferenceAbsolute(I_blurred, I_blurred_ref, I_diff);
75  vpImage<double> I_diff_dbl;
76  vpImageConvert::convert(I_diff, I_diff_dbl);
77  std::cout << "sigma: " << sigma << " ; I_diff_dbl: " << I_diff_dbl.getMeanValue() << std::endl;
78  const double threshold = 1.5;
79  CHECK(I_diff_dbl.getMeanValue() < threshold);
80  }
81 }
82 
83 TEST_CASE("Test vpGaussianFilter (vpRGBa)")
84 {
85  const std::string filepath = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "Klimt/Klimt.ppm");
87  vpImageIo::read(I, filepath);
88 
89  std::vector<float> sigmas = { 0.5f, 2.0f, 5.0f, 7.0f };
90  for (auto sigma : sigmas) {
91  vpGaussianFilter gaussianFilter(I.getWidth(), I.getHeight(), sigma);
92 
93  vpImage<vpRGBa> I_blurred;
94  gaussianFilter.apply(I, I_blurred);
95 
96  vpImage<vpRGBa> I_blurred_ref;
97  const std::string filepath_ref = vpIoTools::createFilePath(
98  vpIoTools::getViSPImagesDataPath(), "Gaussian-filter/Klimt_RGB_Gaussian_blur_sigma=%.1f.png");
99  char buffer[FILENAME_MAX];
100  snprintf(buffer, FILENAME_MAX, filepath_ref.c_str(), sigma);
101  const std::string filename = buffer;
102  vpImageIo::read(I_blurred_ref, filename);
103 
104  vpImage<vpRGBa> I_diff;
105  vpImageTools::imageDifferenceAbsolute(I_blurred, I_blurred_ref, I_diff);
106  vpImage<unsigned char> I_diff_R, I_diff_G, I_diff_B;
107  vpImageConvert::split(I_diff, &I_diff_R, &I_diff_G, &I_diff_B);
108 
109  vpImage<double> I_diff_R_dbl, I_diff_G_dbl, I_diff_B_dbl;
110  vpImageConvert::convert(I_diff_R, I_diff_R_dbl);
111  vpImageConvert::convert(I_diff_G, I_diff_G_dbl);
112  vpImageConvert::convert(I_diff_B, I_diff_B_dbl);
113 
114  std::cout << "sigma: " << sigma << " ; I_diff_R_dbl: " << I_diff_R_dbl.getMeanValue()
115  << " ; I_diff_G_dbl: " << I_diff_G_dbl.getMeanValue()
116  << " ; I_diff_B_dbl: " << I_diff_B_dbl.getMeanValue() << std::endl;
117  const double threshold = 1.5;
118  CHECK(I_diff_R_dbl.getMeanValue() < threshold);
119  CHECK(I_diff_G_dbl.getMeanValue() < threshold);
120  CHECK(I_diff_B_dbl.getMeanValue() < threshold);
121  }
122 }
123 
124 TEST_CASE("Test vpGaussianFilter (vpRGBa + deinterleave)")
125 {
126  const std::string filepath = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "Klimt/Klimt.ppm");
127  vpImage<vpRGBa> I;
128  vpImageIo::read(I, filepath);
129 
130  std::vector<float> sigmas = { 0.5f, 2.0f, 5.0f, 7.0f };
131  for (auto sigma : sigmas) {
132  const bool deinterleave = true;
133  vpGaussianFilter gaussianFilter(I.getWidth(), I.getHeight(), sigma, deinterleave);
134 
135  vpImage<vpRGBa> I_blurred;
136  gaussianFilter.apply(I, I_blurred);
137 
138  vpImage<vpRGBa> I_blurred_ref;
139  const std::string filepath_ref = vpIoTools::createFilePath(
140  vpIoTools::getViSPImagesDataPath(), "Gaussian-filter/Klimt_RGB_Gaussian_blur_sigma=%.1f.png");
141  char buffer[FILENAME_MAX];
142  snprintf(buffer, FILENAME_MAX, filepath_ref.c_str(), sigma);
143  const std::string filename = buffer;
144  vpImageIo::read(I_blurred_ref, filename);
145 
146  vpImage<vpRGBa> I_diff;
147  vpImageTools::imageDifferenceAbsolute(I_blurred, I_blurred_ref, I_diff);
148  vpImage<unsigned char> I_diff_R, I_diff_G, I_diff_B;
149  vpImageConvert::split(I_diff, &I_diff_R, &I_diff_G, &I_diff_B);
150 
151  vpImage<double> I_diff_R_dbl, I_diff_G_dbl, I_diff_B_dbl;
152  vpImageConvert::convert(I_diff_R, I_diff_R_dbl);
153  vpImageConvert::convert(I_diff_G, I_diff_G_dbl);
154  vpImageConvert::convert(I_diff_B, I_diff_B_dbl);
155 
156  std::cout << "sigma: " << sigma << " ; I_diff_R_dbl: " << I_diff_R_dbl.getMeanValue()
157  << " ; I_diff_G_dbl: " << I_diff_G_dbl.getMeanValue()
158  << " ; I_diff_B_dbl: " << I_diff_B_dbl.getMeanValue() << std::endl;
159  const double threshold = 1.5;
160  CHECK(I_diff_R_dbl.getMeanValue() < threshold);
161  CHECK(I_diff_G_dbl.getMeanValue() < threshold);
162  CHECK(I_diff_B_dbl.getMeanValue() < threshold);
163  }
164 }
165 
166 int main(int argc, char *argv[])
167 {
168  Catch::Session session; // There must be exactly one instance
169 
170  // Let Catch (using Clara) parse the command line
171  session.applyCommandLine(argc, argv);
172 
173  int numFailed = session.run();
174 
175  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
176  // This clamping has already been applied, so just return it here
177  // You can also do any post run clean-up here
178  return numFailed;
179 }
180 #else
181 int main() { return EXIT_SUCCESS; }
182 #endif
Gaussian filter class.
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=nullptr)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:143
static void imageDifferenceAbsolute(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
Type getMeanValue() const
Return the mean value of the bitmap.
Definition: vpImage.h:942
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:184
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1786
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2147