File: testHSVtoHSV.cpp

package info (click to toggle)
visp 3.7.0-7
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 166,380 kB
  • sloc: cpp: 392,705; ansic: 224,448; xml: 23,444; python: 13,701; java: 4,792; sh: 206; objc: 145; makefile: 118
file content (260 lines) | stat: -rw-r--r-- 10,804 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Test vpImageConvert::convert() function for HSV to HSV.
 */
/*!
  \example testHSVtoHSV.cpp

  \brief Test vpImageConvert::convert() function for HSV to HSV.
*/
#include <iostream>
#include <limits>

#include <visp3/core/vpConfig.h>
#include <visp3/core/vpImage.h>
#include <visp3/core/vpImageConvert.h>
#include <visp3/core/vpRGBa.h>
#include <visp3/core/vpHSV.h>

#include "hsvUtils.h"

#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)

#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif

#ifndef DOXYGEN_SHOULD_SKIP_THIS
/**
 * \brief Check if the computed HSV value corresponds to the ground-truth.
 *
 * \tparam useFullScale True if vpHSV uses unsigned char and the full range [0; 255], false if vpHSV uses unsigned char and the limited range [0; vpHSV<unsigned char, false>::maxHueUsingLimitedRange].
 * \param[in] hsv_computed Computed vpRGBa value.
 * \param[in] rgb_truth vpRGBa value  that was used to compute rgb_computed.
 * \param[in] hsv_truth The HSVground-truth value.
 * \return true If hsv_computed and hsv_truth are equal.
 * \return false Otherwise
*/
template<bool useFullScale >
bool test_hsv(const vpHSV<unsigned char, useFullScale> &hsv_computed,
              const vpHSV<unsigned char, useFullScale> &hsv_truth)
{
  // Compare HSV values
  if ((hsv_computed.H != hsv_truth.H) ||
      (hsv_computed.S != hsv_truth.S) ||
      (hsv_computed.V != hsv_truth.V)) {

    std::cout << "Expected hsv value: ("
      << static_cast<int>(hsv_truth.H) << ","
      << static_cast<int>(hsv_truth.S) << ","
      << static_cast<int>(hsv_truth.V) << ") converted value: ("
      << static_cast<int>(hsv_computed.H) << ","
      << static_cast<int>(hsv_computed.S) << ","
      << static_cast<int>(hsv_computed.V) << ")" << std::endl;
    return false;
  }

  return true;
}

/**
 * \brief Check if the computed HSV value corresponds to the ground-truth.
 *
 * \tparam Type The type of the vpHSV channels.
 * \tparam useFullScale True if vpHSV uses unsigned char and the full range [0; 255], false if vpHSV uses unsigned char and the limited range [0; vpHSV<unsigned char, false>::maxHueUsingLimitedRange].
 * \param[in] hsv_computed Computed vpRGBa value.
 * \param[in] rgb_truth vpRGBa value  that was used to compute rgb_computed.
 * \param[in] hsv_truth The HSVground-truth value.
 * \return true If hsv_computed and hsv_truth are equal.
 * \return false Otherwise
 */
template<typename Type, bool useFullScale >
bool test_hsv(const vpHSV<Type, useFullScale> &hsv_computed,
              const vpHSV<Type, useFullScale> &hsv_truth)
{
  // Compare HSV values
  if ((!vpMath::equal(hsv_computed.H, hsv_truth.H)) ||
      (!vpMath::equal(hsv_computed.S, hsv_truth.S)) ||
      (!vpMath::equal(hsv_computed.V, hsv_truth.V))) {

    std::cout << "Expected hsv value: ("
      << static_cast<int>(hsv_truth.H) << ","
      << static_cast<int>(hsv_truth.S) << ","
      << static_cast<int>(hsv_truth.V) << ") converted value: ("
      << static_cast<int>(hsv_computed.H) << ","
      << static_cast<int>(hsv_computed.S) << ","
      << static_cast<int>(hsv_computed.V) << ")" << std::endl;
    return false;
  }

  return true;
}
#endif

int main()
{
  bool isSuccess = true;

  std::cout << std::endl << "----- Testing single pixel HSV to HSV conversions -----" << std::endl;
  vpHSV<unsigned char, false> hsvucff(vpHSV<float>(1., 1., 1.));
  isSuccess = isSuccess && test_hsv(hsvucff, vpHSV<unsigned char, false>(vpHSV<unsigned char, false>::maxHueUsingLimitedRange, 255, 255));

  vpHSV<unsigned char, true > hsvucft(vpHSV<float>(1., 1., 1.));
  isSuccess = isSuccess && test_hsv(hsvucft, vpHSV<unsigned char, true>(255, 255, 255));

  vpHSV<unsigned char, false> hsvucdf(vpHSV<double>(1., 1., 1.));
  isSuccess = isSuccess && test_hsv(hsvucdf, vpHSV<unsigned char, false>(vpHSV<unsigned char, false>::maxHueUsingLimitedRange, 255, 255));

  vpHSV<unsigned char, true > hsvucdt(vpHSV<double>(1., 1., 1.));
  isSuccess = isSuccess && test_hsv(hsvucdt, vpHSV<unsigned char, true>(255, 255, 255));

  vpHSV<float> hsvfucf(vpHSV<unsigned char, false>(vpHSV<unsigned char, false>::maxHueUsingLimitedRange, 255, 255));
  isSuccess = isSuccess && test_hsv(hsvfucf, vpHSV<float>(1., 1., 1.));

  vpHSV<float> hsvfuct(vpHSV<unsigned char, true >(255, 255, 255));
  isSuccess = isSuccess && test_hsv(hsvfuct, vpHSV<float>(1., 1., 1.));

  vpHSV<double> hsvducf(vpHSV<unsigned char, false>(vpHSV<unsigned char, false>::maxHueUsingLimitedRange, 255, 255));
  isSuccess = isSuccess && test_hsv(hsvducf, vpHSV<double>(1., 1., 1.));

  vpHSV<double> hsvduct(vpHSV<unsigned char, true >(255, 255, 255));
  isSuccess = isSuccess && test_hsv(hsvduct, vpHSV<double>(1., 1., 1.));

  std::cout << std::endl << "----- Testing vpImageConvert(HSV, HSV) conversions -----" << std::endl;
  {
    std::cout << std::endl << "\t UCHAR -> floating types" << std::endl;
    vpImage<vpHSV<unsigned char, false>> Iucf(480, 640, vpHSV<unsigned char, false>(vpHSV<unsigned char, false>::maxHueUsingLimitedRange, 255, 255));
    vpImage<vpHSV<float>> Ihff, Ihff_truth(480, 640, vpHSV<float>(1., 1., 1.));
    vpImageConvert::convert(Iucf, Ihff);
    bool localSuccess = vpHSVTests::areAlmostEqual(Ihff, "Ihff", Ihff_truth, "Ihff_truth");
    if (!localSuccess) {
      std::cerr << "vpImageConvert(HSV<uchar, false>, HSV<float>) failed!" << std::endl;
    }
    isSuccess = isSuccess && localSuccess;

    vpImage<vpHSV<unsigned char, true>> Iuct(480, 640, vpHSV<unsigned char, true>(255, 255, 255));
    vpImage<vpHSV<float>> Ihft, Ihft_truth(480, 640, vpHSV<float>(1., 1., 1.));
    vpImageConvert::convert(Iuct, Ihft);
    localSuccess = vpHSVTests::areAlmostEqual(Ihft, "Ihft", Ihft_truth, "Ihft_truth");
    if (!localSuccess) {
      std::cerr << "vpImageConvert(HSV<uchar, true>, HSV<float>) failed!" << std::endl;
    }
    isSuccess = isSuccess && localSuccess;

    vpImage<vpHSV<double>> Ihdf, Ihdf_truth(480, 640, vpHSV<double>(1., 1., 1.));
    vpImageConvert::convert(Iucf, Ihdf);
    localSuccess = vpHSVTests::areAlmostEqual(Ihdf, "Ihdf", Ihdf_truth, "Ihdf_truth");
    if (!localSuccess) {
      std::cerr << "vpImageConvert(HSV<uchar, false>, HSV<double>) failed!" << std::endl;
    }
    isSuccess = isSuccess && localSuccess;

    vpImage<vpHSV<double>> Ihdt, Ihdt_truth(480, 640, vpHSV<double>(1., 1., 1.));
    vpImageConvert::convert(Iuct, Ihdt);
    localSuccess = vpHSVTests::areAlmostEqual(Ihdt, "Ihdt", Ihdt_truth, "Ihdt_truth");
    if (!localSuccess) {
      std::cerr << "vpImageConvert(HSV<uchar, true>, HSV<double>) failed!" << std::endl;
    }
    isSuccess = isSuccess && localSuccess;
  }

  {
    std::cout << std::endl << "\t floating types -> UCHAR" << std::endl;
    vpImage<vpHSV<float>> Ihff(480, 640, vpHSV<float>(1., 1., 1.));
    vpImage<vpHSV<unsigned char, false>> Iucf, Iucf_truth(480, 640, vpHSV<unsigned char, false>(vpHSV<unsigned char, false>::maxHueUsingLimitedRange, 255, 255));
    vpImageConvert::convert(Ihff, Iucf);
    bool localSuccess = vpHSVTests::areAlmostEqual(Iucf, "Iucf", Iucf_truth, "Iucf_truth");
    if (!localSuccess) {
      std::cerr << "vpImageConvert(HSV<float>, HSV<uchar, false>) failed!" << std::endl;
    }
    isSuccess = isSuccess && localSuccess;

    vpImage<vpHSV<float>> Ihft(480, 640, vpHSV<float>(1., 1., 1.));
    vpImage<vpHSV<unsigned char, true>> Iuct, Iuct_truth(480, 640, vpHSV<unsigned char, true>(255, 255, 255));
    vpImageConvert::convert(Ihft, Iuct);
    localSuccess = vpHSVTests::areAlmostEqual(Iuct, "Iuct", Iuct_truth, "Iuct_truth");
    if (!localSuccess) {
      std::cerr << "vpImageConvert(HSV<float>, HSV<uchar, true>) failed!" << std::endl;
    }
    isSuccess = isSuccess && localSuccess;

    vpImage<vpHSV<double>> Ihdf(480, 640, vpHSV<double>(1., 1., 1.));
    vpImageConvert::convert(Ihdf, Iucf);
    localSuccess = vpHSVTests::areAlmostEqual(Iucf, "Iucf", Iucf_truth, "Iucf_truth");
    if (!localSuccess) {
      std::cerr << "vpImageConvert(HSV<double>, HSV<uchar, false>) failed!" << std::endl;
    }
    isSuccess = isSuccess && localSuccess;

    vpImage<vpHSV<double>> Ihdt(480, 640, vpHSV<double>(1., 1., 1.));
    vpImageConvert::convert(Ihdt, Iuct);
    localSuccess = vpHSVTests::areAlmostEqual(Iuct, "Iuct", Iuct_truth, "Iuct_truth");
    if (!localSuccess) {
      std::cerr << "vpImageConvert(HSV<double>, HSV<uchar, true>) failed!" << std::endl;
    }
    isSuccess = isSuccess && localSuccess;
  }

  {
    std::cout << std::endl << "\t floating types -> floating types" << std::endl;
    vpImage<vpHSV<float>> Ifloat_truth(480, 640, vpHSV<float>(1., 1., 1.));
    vpImage<vpHSV<double>> Idouble, Idouble_truth(480, 640, vpHSV<double>(1., 1., 1.));
    vpImageConvert::convert(Ifloat_truth, Idouble);
    bool localSuccess = vpHSVTests::areAlmostEqual(Idouble, "Idouble", Idouble_truth, "Idouble_truth");
    if (!localSuccess) {
      std::cerr << "vpImageConvert(HSV<float>, HSV<double>) failed!" << std::endl;
    }
    isSuccess = isSuccess && localSuccess;

    vpImage<vpHSV<float>> Ifloat;
    vpImageConvert::convert(Idouble_truth, Ifloat);
    localSuccess = vpHSVTests::areAlmostEqual(Ifloat, "Ifloat", Ifloat_truth, "Ifloat_truth");
    if (!localSuccess) {
      std::cerr << "vpImageConvert(HSV<double>, HSV<float>) failed!" << std::endl;
    }
    isSuccess = isSuccess && localSuccess;
  }

  if (isSuccess) {
    std::cout << "All tests were successful !" << std::endl;
    return EXIT_SUCCESS;
  }
  std::cerr << "ERROR: Something went wrong !" << std::endl;
  return EXIT_FAILURE;
}

#else
int main()
{
  std::cout << "vpHSV class is not available, please use CXX 11 standard" << std::endl;
  return EXIT_SUCCESS;
}
#endif