File: hsvUtils.h

package info (click to toggle)
visp 3.7.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 166,384 kB
  • sloc: cpp: 392,705; ansic: 224,448; xml: 23,444; python: 13,701; java: 4,792; sh: 207; objc: 145; makefile: 118
file content (361 lines) | stat: -rw-r--r-- 15,442 bytes parent folder | download
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * 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:
 * HSV color scale utils.
 */

#ifndef HSV_UTILS_H
#define HSV_UTILS_H

#include <iostream>
#include <map>

#include <visp3/core/vpColorGetter.h>
#include <visp3/core/vpImage.h>

#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
#include <type_traits>

BEGIN_VISP_NAMESPACE
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace vpHSVTests
{
/**
 * \brief Compute the error for an arithmetic type (uchar, float, double, ...).
 *
 * \tparam T An arithmetic type.
 * \param[in] a The first value.
 * \param[in] b The second value.
 * \return The absolute difference.
 */
template<typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, double>::type error(const T &a, const T &b)
{
  return std::abs(a - b);
}

/**
 * \brief Initialize the error to 0.
 *
 * \tparam Tp The type, that must have a nbChannels constexpr.
 * \tparam I The iterator.
 * \param[in] t1 The first value.
 * \param[in] t2 The second value.
 * \return double 0. to initialize the error.
 */
template<typename Tp, int I = 0>
inline typename std::enable_if<I == Tp::nbChannels, double>::type error(const Tp &t1, const Tp &t2)
{
  (void)t1;
  (void)t2;
  return 0.;
}

/**
 * \brief Compute the error for a channel and recursively call error for the other channels.
 *
 * \tparam Tp The type, that must have a nbChannels constexpr.
 * \tparam I The iterator.
 * \param[in] t1 The first value.
 * \param[in] t2 The second value.
 * \return double The weighted sum of the absolute error for all the channels.
 */
template<typename Tp, int I = 0>
inline typename std::enable_if<I < Tp::nbChannels, double>::type error(const Tp &t1, const Tp &t2)
{
  const double &val1 = vpColorGetter<I>::get(t1);
  const double &val2 = vpColorGetter<I>::get(t2);
  const double den = 1. / Tp::nbChannels;
  double err = den * std::abs(val1 - val2) + error<Tp, I + 1>(t1, t2);
  return err;
}

/**
 * \brief Indicates if 2 vpImage are almost equal.
 *
 * \tparam T The type of the image.
 * \param[in] I1 The first image.
 * \param[in] I2 The second image.
 * \param[in] thresh The maximum tolerated error.
 * \return true The images are almost equal.
 * \return false Otherwise.
 */
template<typename T>
bool areAlmostEqual(const vpImage<T> &I1, const std::string &nameI1, const vpImage<T> &I2, const std::string &nameI2, const double &thresh = 1e-3)
{
  bool areEqual = true;
  if (I1.getWidth() != I2.getWidth()) {
    std::cerr << "ERROR: Image width differ." << std::endl;
  }

  if (I1.getHeight() != I2.getHeight()) {
    std::cerr << "ERROR: Image height differ." << std::endl;
  }

  unsigned int width = I1.getWidth(), height = I1.getHeight();
  for (unsigned int r = 0; (r < height) && areEqual; ++r) {
    for (unsigned int c = 0; (c < width) && areEqual; ++c) {
      double err = error(I1[r][c], I2[r][c]);
      if (err > thresh) {
        std::cerr << "ERROR: Error (" << err << ") > thresh (" << thresh << ")" << std::endl;
        std::cerr << "\t" << nameI1 << "[" << r << "][" << c << "] = (" << I1[r][c] << ") , " << nameI2 << "[" << r << "][" << c << "] = (" << I2[r][c] << ")" << std::endl;
        areEqual = false;
      }
    }
  }
  return areEqual;
}

/*!
 * Print HSV values.
 * @tparam ArithmeticType Arithmetic type.
 * @tparam useFullScale When true use HSV full scale.
 * @param[in] I HSV image.
 * @param[in] name Legend.
 */
template<typename ArithmeticType, bool useFullScale>
void print(const vpImage<vpHSV<ArithmeticType, useFullScale>> &I, const std::string &name)
{
  std::cout << name << " = " << std::endl;
  const unsigned int nbRows = I.getRows(), nbCols = I.getCols();
  for (unsigned int r = 0; r < nbRows; ++r) {
    for (unsigned int c = 0; c < nbCols; ++c) {
      std::string character;
      if (vpMath::nul(I[r][c].H, 1e-3)) {
        character = '0';
      }
      else {
        double val = static_cast<double>(I[r][c].H);
        if (val > 0 && val < 1.) {
          val *= 10.;
        }
        character = std::to_string(static_cast<unsigned int>(val));
      }
      std::cout << character << " ";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;
}

/*!
 * Print HSV values.
 * @tparam ArithmeticType Arithmetic type.
 * @param[in] I HSV image.
 * @param[in] name Legend.
 */
template<typename ArithmeticType>
void print(const vpImage<ArithmeticType> &I, const std::string &name)
{
  std::cout << name << " = " << std::endl;
  const unsigned int nbRows = I.getRows(), nbCols = I.getCols();
  for (unsigned int r = 0; r < nbRows; ++r) {
    for (unsigned int c = 0; c < nbCols; ++c) {
      char character;
      if (vpMath::nul(I[r][c], 1e-3)) {
        character = '0';
      }
      else if (I[r][c] > 0) {
        character = '+';
      }
      else {
        character = '-';
      }
      std::cout << character << " ";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl << std::flush;
}

/*!
 * Print HSV values.
 * @param[in] I HSV image.
 * @param[in] name Legend.
 */
void print(const vpImage<unsigned char> &I, const std::string &name)
{
  std::cout << name << " = " << std::endl;
  const unsigned int nbRows = I.getRows(), nbCols = I.getCols();
  for (unsigned int r = 0; r < nbRows; ++r) {
    for (unsigned int c = 0; c < nbCols; ++c) {
      std::cout << std::to_string(I[r][c]) << " ";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl << std::flush;
}

template <typename ImageType>
struct vpInputImage
{
  std::string m_errorMsg;
  vpImage<ImageType> m_I;

  vpInputImage(const std::string &errMsg = "", const vpImage<ImageType> &I = vpImage<ImageType>())
    : m_errorMsg(errMsg)
    , m_I(I)
  { }
};

typedef struct vpInputDataset
{
  std::vector<std::pair<std::string, vpInputImage<unsigned char>>> m_ucImages;
  std::vector<std::pair<std::string, vpInputImage<vpHSV<unsigned char, true>>>> m_hsvUCtrue;
  std::vector<std::pair<std::string, vpInputImage<vpHSV<unsigned char, false>>>> m_hsvUCfalse;
  std::vector<std::pair<std::string, vpInputImage<vpHSV<double>>>> m_hsvDouble;
  vpImage<bool> m_Imask;

  vpInputDataset()
  {
    vpImage<vpHSV<unsigned char, true>> Ihsv_uc_true_square(11, 11, vpHSV<unsigned char, true>(0U, 0U, 0U));
    vpImage<vpHSV<unsigned char, true>> Ihsv_uc_true_inversed = Ihsv_uc_true_square;
    vpImage<vpHSV<unsigned char, true>> Ihsv_uc_true_horizontal = Ihsv_uc_true_square;
    vpImage<vpHSV<unsigned char, true>> Ihsv_uc_true_vertical = Ihsv_uc_true_square;
    vpImage<vpHSV<unsigned char, true>> Ihsv_uc_true_pyramid = Ihsv_uc_true_square;

    vpImage<vpHSV<unsigned char, false>> Ihsv_uc_false_square(11, 11, vpHSV<unsigned char, false>(0U, 0U, 0U));
    vpImage<vpHSV<unsigned char, false>> Ihsv_uc_false_inversed = Ihsv_uc_false_square;
    vpImage<vpHSV<unsigned char, false>> Ihsv_uc_false_horizontal = Ihsv_uc_false_square;
    vpImage<vpHSV<unsigned char, false>> Ihsv_uc_false_vertical = Ihsv_uc_false_square;
    vpImage<vpHSV<unsigned char, false>> Ihsv_uc_false_pyramid = Ihsv_uc_false_square;

    vpImage<vpHSV<double>> Ihsv_square(11, 11, vpHSV<double>(0., 0., 0.));
    vpImage<vpHSV<double>> Ihsv_square_inversed = Ihsv_square;
    vpImage<vpHSV<double>> Ihsv_square_horizontal = Ihsv_square;
    vpImage<vpHSV<double>> Ihsv_square_vertical = Ihsv_square;
    vpImage<vpHSV<double>> Ihsv_pyramid = Ihsv_square;

    vpImage<unsigned char> Iuc_square(11, 11, 0);
    vpImage<unsigned char> Iuc_square_inversed = Iuc_square;
    vpImage<unsigned char> Iuc_square_horizontal = Iuc_square;
    vpImage<unsigned char> Iuc_square_vertical = Iuc_square;
    vpImage<unsigned char> Iuc_pyramid = Iuc_square;

    m_Imask.resize(11, 11, false);

    for (unsigned int r = 2; r <=8; ++r) {
      for (unsigned int c = 2; c <= 8; ++c) {
        if ((r == 2) || (r == 3) || (r==7) || (r == 8)) {
          m_Imask[r][c] = true;
        }
        if ((c == 2) || (c == 3) || (c==7) || (c == 8)) {
          m_Imask[r][c] = true;
        }
        double val = 0., val_inversed = 0., val_pyramid = 0.;
        unsigned char val_c = 0, val_c_inversed = 0, val_c_pyramid = 0;
        if (c <= 5) {
          val = 0.1 * static_cast<double>(c - 1);
          val_inversed = 0.1 * static_cast<double>(6 - c);
          val_c = c - 1;
          val_c_inversed = 6 - c;
        }
        else {
          val = 0.1 * static_cast<double>(9 - c);
          val_inversed = 0.1 * static_cast<double>(c - 4);
          val_c = 9 - c;
          val_c_inversed = c - 4;
        }
        if (r <= 5) {
          val_pyramid = val + 0.1 * (r - 2);
          val_c_pyramid = val_c + r - 2;
        }
        else {
          val_pyramid = val + 0.1 * (8 - r);
          val_c_pyramid = val_c + 8 - r;
        }
        if ((r == 2) || (r == 8) /* || (((r == 4) || (r == 6)) && ((c >=4) && (c <= 6))) */) {
          // Horizontal lines of the square
          Ihsv_square[r][c] = vpHSV<double>(val, val, val);
          Ihsv_square_inversed[r][c] = vpHSV<double>(val_inversed, val_inversed, val_inversed);
          Ihsv_square[c][r] = vpHSV<double>(val, val, val);
          Ihsv_square_inversed[c][r] = vpHSV<double>(val_inversed, val_inversed, val_inversed);

          Ihsv_uc_false_square[r][c] = vpHSV<unsigned char, false>(val_c, val_c, val_c);
          Ihsv_uc_false_inversed[r][c] = vpHSV<unsigned char, false>(val_c_inversed, val_c_inversed, val_c_inversed);
          Ihsv_uc_false_square[c][r] = vpHSV<unsigned char, false>(val_c, val_c, val_c);
          Ihsv_uc_false_inversed[c][r] = vpHSV<unsigned char, false>(val_c_inversed, val_c_inversed, val_c_inversed);

          Ihsv_uc_true_square[r][c] = vpHSV<unsigned char, true>(val_c, val_c, val_c);
          Ihsv_uc_true_inversed[r][c] = vpHSV<unsigned char, true>(val_c_inversed, val_c_inversed, val_c_inversed);
          Ihsv_uc_true_square[c][r] = vpHSV<unsigned char, true>(val_c, val_c, val_c);
          Ihsv_uc_true_inversed[c][r] = vpHSV<unsigned char, true>(val_c_inversed, val_c_inversed, val_c_inversed);

          Iuc_square[r][c] = val_c;
          Iuc_square_inversed[r][c] = val_c_inversed;
          Iuc_square[c][r] = val_c;
          Iuc_square_inversed[c][r] = val_c_inversed;
        }
        Ihsv_square_horizontal[r][c] = vpHSV<double>(val, val, val);
        Ihsv_uc_true_horizontal[r][c] = vpHSV<unsigned char, true>(val_c, val_c, val_c);
        Ihsv_uc_false_horizontal[r][c] = vpHSV<unsigned char, false>(val_c, val_c, val_c);
        Iuc_square_horizontal[r][c] = val_c;

        Ihsv_square_vertical[c][r] = vpHSV<double>(val, val, val);
        Ihsv_uc_true_vertical[c][r] = vpHSV<unsigned char, true>(val_c, val_c, val_c);
        Ihsv_uc_false_vertical[c][r] = vpHSV<unsigned char, false>(val_c, val_c, val_c);
        Iuc_square_vertical[c][r] = val_c;

        Ihsv_pyramid[r][c] = vpHSV<double>(val_pyramid, val_pyramid, val_pyramid);
        Ihsv_uc_true_pyramid[r][c] = vpHSV<unsigned char, true>(val_c_pyramid, val_c_pyramid, val_c_pyramid);
        Ihsv_uc_false_pyramid[r][c] = vpHSV<unsigned char, false>(val_c_pyramid, val_c_pyramid, val_c_pyramid);
        Iuc_pyramid[r][c] = val_c_pyramid;
      }
    }
    m_ucImages.emplace_back("Iuc_square", vpInputImage<unsigned char>("", Iuc_square));
    m_ucImages.emplace_back("Iuc_square_inversed", vpInputImage<unsigned char>("", Iuc_square_inversed));
    m_ucImages.emplace_back("Iuc_square_horizontal", vpInputImage<unsigned char>("GX is expected to have values on the whole surface of the square, GY only on the borders", Iuc_square_horizontal));
    m_ucImages.emplace_back("Iuc_square_vertical", vpInputImage<unsigned char>("GY is expected to have values on the whole surface of the square, GX only on the borders", Iuc_square_vertical));
    m_ucImages.emplace_back("Iuc_pyramid", vpInputImage<unsigned char>("GX and GY are expected to have values on the whole square", Iuc_pyramid));

    m_hsvUCtrue.emplace_back("Ihsv_uc_true_square", vpInputImage<vpHSV<unsigned char, true>>("", Ihsv_uc_true_square));
    m_hsvUCtrue.emplace_back("Ihsv_uc_true_inversed", vpInputImage<vpHSV<unsigned char, true>>("", Ihsv_uc_true_inversed));
    m_hsvUCtrue.emplace_back("Ihsv_uc_true_horizontal", vpInputImage<vpHSV<unsigned char, true>>("GX is expected to have values on the whole surface of the square, GY only on the borders", Ihsv_uc_true_horizontal));
    m_hsvUCtrue.emplace_back("Ihsv_uc_true_vertical", vpInputImage<vpHSV<unsigned char, true>>("GY is expected to have values on the whole surface of the square, GX only on the borders", Ihsv_uc_true_vertical));
    m_hsvUCtrue.emplace_back("Ihsv_uc_true_pyramid", vpInputImage<vpHSV<unsigned char, true>>("GX and GY are expected to have values on the whole square", Ihsv_uc_true_pyramid));

    m_hsvUCfalse.emplace_back("Ihsv_uc_false_square", vpInputImage<vpHSV<unsigned char, false>>("", Ihsv_uc_false_square));
    m_hsvUCfalse.emplace_back("Ihsv_uc_false_inversed", vpInputImage<vpHSV<unsigned char, false>>("", Ihsv_uc_false_inversed));
    m_hsvUCfalse.emplace_back("Ihsv_uc_false_horizontal", vpInputImage<vpHSV<unsigned char, false>>("GX is expected to have values on the whole surface of the square, GY only on the borders", Ihsv_uc_false_horizontal));
    m_hsvUCfalse.emplace_back("Ihsv_uc_false_vertical", vpInputImage<vpHSV<unsigned char, false>>("GY is expected to have values on the whole surface of the square, GX only on the borders", Ihsv_uc_false_vertical));
    m_hsvUCfalse.emplace_back("Ihsv_uc_false_pyramid", vpInputImage<vpHSV<unsigned char, false>>("GX and GY are expected to have values on the whole square", Ihsv_uc_false_pyramid));

    m_hsvDouble.emplace_back("I_hsv_double_square", vpInputImage<vpHSV<double>>("", Ihsv_square));
    m_hsvDouble.emplace_back("Ihsv_double_square_inversed", vpInputImage<vpHSV<double>>("", Ihsv_square_inversed));
    m_hsvDouble.emplace_back("Ihsv_double_horizontal", vpInputImage<vpHSV<double>>("GX is expected to have values on the whole surface of the square, GY only on the borders", Ihsv_square_horizontal));
    m_hsvDouble.emplace_back("Ihsv_double_vertical", vpInputImage<vpHSV<double>>("GY is expected to have values on the whole surface of the square, GX only on the borders", Ihsv_square_vertical));
    m_hsvDouble.emplace_back("Ihsv_double_pyramid", vpInputImage<vpHSV<double>>("GX and GY are expected to have values on the whole square", Ihsv_pyramid));
  }
}vpInputDataset;
}
#endif
END_VISP_NAMESPACE
#endif
#endif