Visual Servoing Platform version 3.5.0
testGaussianFilter.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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_CATCH2)
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
52TEST_CASE("Test vpGaussianFilter (unsigned char)") {
54 "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(vpIoTools::getViSPImagesDataPath(),
67 "Gaussian-filter/Klimt_gray_Gaussian_blur_sigma=%.1f.png");
68 char buffer[256];
69 sprintf(buffer, 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
83TEST_CASE("Test vpGaussianFilter (vpRGBa)") {
85 "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(vpIoTools::getViSPImagesDataPath(),
98 "Gaussian-filter/Klimt_RGB_Gaussian_blur_sigma=%.1f.png");
99 char buffer[256];
100 sprintf(buffer, 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() << " ; I_diff_G_dbl: "
115 << I_diff_G_dbl.getMeanValue() << " ; I_diff_B_dbl: " << I_diff_B_dbl.getMeanValue() << std::endl;
116 const double threshold = 1.5;
117 CHECK(I_diff_R_dbl.getMeanValue() < threshold);
118 CHECK(I_diff_G_dbl.getMeanValue() < threshold);
119 CHECK(I_diff_B_dbl.getMeanValue() < threshold);
120 }
121}
122
123TEST_CASE("Test vpGaussianFilter (vpRGBa + deinterleave)") {
124 const std::string filepath = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(),
125 "Klimt/Klimt.ppm");
127 vpImageIo::read(I, filepath);
128
129 std::vector<float> sigmas = {0.5f, 2.0f, 5.0f, 7.0f};
130 for (auto sigma : sigmas) {
131 const bool deinterleave = true;
132 vpGaussianFilter gaussianFilter(I.getWidth(), I.getHeight(), sigma, deinterleave);
133
134 vpImage<vpRGBa> I_blurred;
135 gaussianFilter.apply(I, I_blurred);
136
137 vpImage<vpRGBa> I_blurred_ref;
138 const std::string filepath_ref = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(),
139 "Gaussian-filter/Klimt_RGB_Gaussian_blur_sigma=%.1f.png");
140 char buffer[256];
141 sprintf(buffer, filepath_ref.c_str(), sigma);
142 const std::string filename = buffer;
143 vpImageIo::read(I_blurred_ref, filename);
144
145 vpImage<vpRGBa> I_diff;
146 vpImageTools::imageDifferenceAbsolute(I_blurred, I_blurred_ref, I_diff);
147 vpImage<unsigned char> I_diff_R, I_diff_G, I_diff_B;
148 vpImageConvert::split(I_diff, &I_diff_R, &I_diff_G, &I_diff_B);
149
150 vpImage<double> I_diff_R_dbl, I_diff_G_dbl, I_diff_B_dbl;
151 vpImageConvert::convert(I_diff_R, I_diff_R_dbl);
152 vpImageConvert::convert(I_diff_G, I_diff_G_dbl);
153 vpImageConvert::convert(I_diff_B, I_diff_B_dbl);
154
155 std::cout << "sigma: " << sigma << " ; I_diff_R_dbl: " << I_diff_R_dbl.getMeanValue() << " ; I_diff_G_dbl: "
156 << I_diff_G_dbl.getMeanValue() << " ; I_diff_B_dbl: " << I_diff_B_dbl.getMeanValue() << std::endl;
157 const double threshold = 1.5;
158 CHECK(I_diff_R_dbl.getMeanValue() < threshold);
159 CHECK(I_diff_G_dbl.getMeanValue() < threshold);
160 CHECK(I_diff_B_dbl.getMeanValue() < threshold);
161 }
162}
163
164int main(int argc, char *argv[])
165{
166 Catch::Session session; // There must be exactly one instance
167
168 // Let Catch (using Clara) parse the command line
169 session.applyCommandLine(argc, argv);
170
171 int numFailed = session.run();
172
173 // numFailed is clamped to 255 as some unices only use the lower 8 bits.
174 // This clamping has already been applied, so just return it here
175 // You can also do any post run clean-up here
176 return numFailed;
177}
178#else
179int main()
180{
181 return 0;
182}
183#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=NULL)
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:149
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:907
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getHeight() const
Definition: vpImage.h:188
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1365
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1670