Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testHSVtoHSV.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Test vpImageConvert::convert() function for HSV to HSV.
32 */
38#include <iostream>
39#include <limits>
40
41#include <visp3/core/vpConfig.h>
42#include <visp3/core/vpImage.h>
43#include <visp3/core/vpImageConvert.h>
44#include <visp3/core/vpRGBa.h>
45#include <visp3/core/vpHSV.h>
46
47#include "hsvUtils.h"
48
49#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
50
51#ifdef ENABLE_VISP_NAMESPACE
52using namespace VISP_NAMESPACE_NAME;
53#endif
54
55#ifndef DOXYGEN_SHOULD_SKIP_THIS
66template<bool useFullScale >
67bool test_hsv(const vpHSV<unsigned char, useFullScale> &hsv_computed,
69{
70 // Compare HSV values
71 if ((hsv_computed.H != hsv_truth.H) ||
72 (hsv_computed.S != hsv_truth.S) ||
73 (hsv_computed.V != hsv_truth.V)) {
74
75 std::cout << "Expected hsv value: ("
76 << static_cast<int>(hsv_truth.H) << ","
77 << static_cast<int>(hsv_truth.S) << ","
78 << static_cast<int>(hsv_truth.V) << ") converted value: ("
79 << static_cast<int>(hsv_computed.H) << ","
80 << static_cast<int>(hsv_computed.S) << ","
81 << static_cast<int>(hsv_computed.V) << ")" << std::endl;
82 return false;
83 }
84
85 return true;
86}
87
99template<typename Type, bool useFullScale >
100bool test_hsv(const vpHSV<Type, useFullScale> &hsv_computed,
101 const vpHSV<Type, useFullScale> &hsv_truth)
102{
103 // Compare HSV values
104 if ((!vpMath::equal(hsv_computed.H, hsv_truth.H)) ||
105 (!vpMath::equal(hsv_computed.S, hsv_truth.S)) ||
106 (!vpMath::equal(hsv_computed.V, hsv_truth.V))) {
107
108 std::cout << "Expected hsv value: ("
109 << static_cast<int>(hsv_truth.H) << ","
110 << static_cast<int>(hsv_truth.S) << ","
111 << static_cast<int>(hsv_truth.V) << ") converted value: ("
112 << static_cast<int>(hsv_computed.H) << ","
113 << static_cast<int>(hsv_computed.S) << ","
114 << static_cast<int>(hsv_computed.V) << ")" << std::endl;
115 return false;
116 }
117
118 return true;
119}
120#endif
121
122int main()
123{
124 bool isSuccess = true;
125
126 std::cout << std::endl << "----- Testing single pixel HSV to HSV conversions -----" << std::endl;
127 vpHSV<unsigned char, false> hsvucff(vpHSV<float>(1., 1., 1.));
128 isSuccess = isSuccess && test_hsv(hsvucff, vpHSV<unsigned char, false>(vpHSV<unsigned char, false>::maxHueUsingLimitedRange, 255, 255));
129
130 vpHSV<unsigned char, true > hsvucft(vpHSV<float>(1., 1., 1.));
131 isSuccess = isSuccess && test_hsv(hsvucft, vpHSV<unsigned char, true>(255, 255, 255));
132
133 vpHSV<unsigned char, false> hsvucdf(vpHSV<double>(1., 1., 1.));
134 isSuccess = isSuccess && test_hsv(hsvucdf, vpHSV<unsigned char, false>(vpHSV<unsigned char, false>::maxHueUsingLimitedRange, 255, 255));
135
136 vpHSV<unsigned char, true > hsvucdt(vpHSV<double>(1., 1., 1.));
137 isSuccess = isSuccess && test_hsv(hsvucdt, vpHSV<unsigned char, true>(255, 255, 255));
138
140 isSuccess = isSuccess && test_hsv(hsvfucf, vpHSV<float>(1., 1., 1.));
141
142 vpHSV<float> hsvfuct(vpHSV<unsigned char, true >(255, 255, 255));
143 isSuccess = isSuccess && test_hsv(hsvfuct, vpHSV<float>(1., 1., 1.));
144
146 isSuccess = isSuccess && test_hsv(hsvducf, vpHSV<double>(1., 1., 1.));
147
148 vpHSV<double> hsvduct(vpHSV<unsigned char, true >(255, 255, 255));
149 isSuccess = isSuccess && test_hsv(hsvduct, vpHSV<double>(1., 1., 1.));
150
151 std::cout << std::endl << "----- Testing vpImageConvert(HSV, HSV) conversions -----" << std::endl;
152 {
153 std::cout << std::endl << "\t UCHAR -> floating types" << std::endl;
155 vpImage<vpHSV<float>> Ihff, Ihff_truth(480, 640, vpHSV<float>(1., 1., 1.));
156 vpImageConvert::convert(Iucf, Ihff);
157 bool localSuccess = vpHSVTests::areAlmostEqual(Ihff, "Ihff", Ihff_truth, "Ihff_truth");
158 if (!localSuccess) {
159 std::cerr << "vpImageConvert(HSV<uchar, false>, HSV<float>) failed!" << std::endl;
160 }
161 isSuccess = isSuccess && localSuccess;
162
164 vpImage<vpHSV<float>> Ihft, Ihft_truth(480, 640, vpHSV<float>(1., 1., 1.));
165 vpImageConvert::convert(Iuct, Ihft);
166 localSuccess = vpHSVTests::areAlmostEqual(Ihft, "Ihft", Ihft_truth, "Ihft_truth");
167 if (!localSuccess) {
168 std::cerr << "vpImageConvert(HSV<uchar, true>, HSV<float>) failed!" << std::endl;
169 }
170 isSuccess = isSuccess && localSuccess;
171
172 vpImage<vpHSV<double>> Ihdf, Ihdf_truth(480, 640, vpHSV<double>(1., 1., 1.));
173 vpImageConvert::convert(Iucf, Ihdf);
174 localSuccess = vpHSVTests::areAlmostEqual(Ihdf, "Ihdf", Ihdf_truth, "Ihdf_truth");
175 if (!localSuccess) {
176 std::cerr << "vpImageConvert(HSV<uchar, false>, HSV<double>) failed!" << std::endl;
177 }
178 isSuccess = isSuccess && localSuccess;
179
180 vpImage<vpHSV<double>> Ihdt, Ihdt_truth(480, 640, vpHSV<double>(1., 1., 1.));
181 vpImageConvert::convert(Iuct, Ihdt);
182 localSuccess = vpHSVTests::areAlmostEqual(Ihdt, "Ihdt", Ihdt_truth, "Ihdt_truth");
183 if (!localSuccess) {
184 std::cerr << "vpImageConvert(HSV<uchar, true>, HSV<double>) failed!" << std::endl;
185 }
186 isSuccess = isSuccess && localSuccess;
187 }
188
189 {
190 std::cout << std::endl << "\t floating types -> UCHAR" << std::endl;
191 vpImage<vpHSV<float>> Ihff(480, 640, vpHSV<float>(1., 1., 1.));
193 vpImageConvert::convert(Ihff, Iucf);
194 bool localSuccess = vpHSVTests::areAlmostEqual(Iucf, "Iucf", Iucf_truth, "Iucf_truth");
195 if (!localSuccess) {
196 std::cerr << "vpImageConvert(HSV<float>, HSV<uchar, false>) failed!" << std::endl;
197 }
198 isSuccess = isSuccess && localSuccess;
199
200 vpImage<vpHSV<float>> Ihft(480, 640, vpHSV<float>(1., 1., 1.));
201 vpImage<vpHSV<unsigned char, true>> Iuct, Iuct_truth(480, 640, vpHSV<unsigned char, true>(255, 255, 255));
202 vpImageConvert::convert(Ihft, Iuct);
203 localSuccess = vpHSVTests::areAlmostEqual(Iuct, "Iuct", Iuct_truth, "Iuct_truth");
204 if (!localSuccess) {
205 std::cerr << "vpImageConvert(HSV<float>, HSV<uchar, true>) failed!" << std::endl;
206 }
207 isSuccess = isSuccess && localSuccess;
208
209 vpImage<vpHSV<double>> Ihdf(480, 640, vpHSV<double>(1., 1., 1.));
210 vpImageConvert::convert(Ihdf, Iucf);
211 localSuccess = vpHSVTests::areAlmostEqual(Iucf, "Iucf", Iucf_truth, "Iucf_truth");
212 if (!localSuccess) {
213 std::cerr << "vpImageConvert(HSV<double>, HSV<uchar, false>) failed!" << std::endl;
214 }
215 isSuccess = isSuccess && localSuccess;
216
217 vpImage<vpHSV<double>> Ihdt(480, 640, vpHSV<double>(1., 1., 1.));
218 vpImageConvert::convert(Ihdt, Iuct);
219 localSuccess = vpHSVTests::areAlmostEqual(Iuct, "Iuct", Iuct_truth, "Iuct_truth");
220 if (!localSuccess) {
221 std::cerr << "vpImageConvert(HSV<double>, HSV<uchar, true>) failed!" << std::endl;
222 }
223 isSuccess = isSuccess && localSuccess;
224 }
225
226 {
227 std::cout << std::endl << "\t floating types -> floating types" << std::endl;
228 vpImage<vpHSV<float>> Ifloat_truth(480, 640, vpHSV<float>(1., 1., 1.));
229 vpImage<vpHSV<double>> Idouble, Idouble_truth(480, 640, vpHSV<double>(1., 1., 1.));
230 vpImageConvert::convert(Ifloat_truth, Idouble);
231 bool localSuccess = vpHSVTests::areAlmostEqual(Idouble, "Idouble", Idouble_truth, "Idouble_truth");
232 if (!localSuccess) {
233 std::cerr << "vpImageConvert(HSV<float>, HSV<double>) failed!" << std::endl;
234 }
235 isSuccess = isSuccess && localSuccess;
236
237 vpImage<vpHSV<float>> Ifloat;
238 vpImageConvert::convert(Idouble_truth, Ifloat);
239 localSuccess = vpHSVTests::areAlmostEqual(Ifloat, "Ifloat", Ifloat_truth, "Ifloat_truth");
240 if (!localSuccess) {
241 std::cerr << "vpImageConvert(HSV<double>, HSV<float>) failed!" << std::endl;
242 }
243 isSuccess = isSuccess && localSuccess;
244 }
245
246 if (isSuccess) {
247 std::cout << "All tests were successful !" << std::endl;
248 return EXIT_SUCCESS;
249 }
250 std::cerr << "ERROR: Something went wrong !" << std::endl;
251 return EXIT_FAILURE;
252}
253
254#else
255int main()
256{
257 std::cout << "vpHSV class is not available, please use CXX 11 standard" << std::endl;
258 return EXIT_SUCCESS;
259}
260#endif
Class implementing the HSV pixel format.
Definition vpHSV.h:135
T V
Definition vpHSV.h:539
T S
Definition vpHSV.h:538
static constexpr unsigned char maxHueUsingLimitedRange
Maximum value of the Hue channel when using unsigned char and the limited range.
Definition vpHSV.h:549
T H
Definition vpHSV.h:537
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:470