File: testHSVGradient.cpp

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 (390 lines) | stat: -rw-r--r-- 14,546 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * 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 vpImageFilter::gaussianBlur() new implementation and compare it to the old one.
 */
/*!
  \example testHSVGradient.cpp

  \brief Test vpImageFilter::gaussianBlur() new implementation and compare it to the old one.
*/

#include <iostream>
#include <limits>

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

#include "hsvUtils.h"

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

#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif

#ifndef DOXYGEN_SHOULD_SKIP_THIS
void gradientFilter(const vpImage<unsigned char> &I, const vpImage<double> &filter, vpImage<double> &GIy)
{
  const unsigned int nbRows = I.getRows(), nbCols = I.getCols();
  for (unsigned int r = 1; r < nbRows - 1; ++r) {
    for (unsigned int c = 1; c < nbCols - 1; ++c) {
      for (unsigned int i = 0; i <= 2; ++i) {
        for (unsigned int j = 0; j <= 2; ++j) {
          GIy[r][c] += filter[i][j] * I[r-1 + i][c-1 + j];
        }
      }
    }
  }
}

void gradientFilter(const vpImage<unsigned char> &I, const vpImage<double> &filterX, const vpImage<double> &filterY, vpImage<double> &GIx, vpImage<double> &GIy)
{
  const unsigned int nbRows = I.getRows(), nbCols = I.getCols();
  GIx.resize(nbRows, nbCols, 0.);
  GIy.resize(nbRows, nbCols, 0.);
  gradientFilter(I, filterX, GIx);
  gradientFilter(I, filterY, GIy);
}

static bool checkBooleanMask(const vpImage<bool> *p_mask, const unsigned int &r, const unsigned int &c)
{
  bool computeVal = true;
  if (p_mask != nullptr) {
    computeVal = (*p_mask)[r][c];
  }
  return computeVal;
}

template <typename ArithmeticType, typename FilterType, bool useFullScale>
void gradientFilterX(const vpImage<vpHSV<ArithmeticType, useFullScale>> &I, vpImage<FilterType> &GIx, const vpImage<bool> *p_mask, const vpImageFilter::vpCannyFilteringAndGradientType &type)
{
  const unsigned int nbRows = I.getRows(), nbCols = I.getCols();
  GIx.resize(nbRows, nbCols, 0.);
  std::vector<FilterType> filter(3);
  FilterType scale;
  std::string name;
  switch (type) {
  case vpImageFilter::CANNY_GBLUR_SOBEL_FILTERING:
    filter = { 1., 2., 1. };
    scale = 8.;
    name = "Sobel";
    break;
  case vpImageFilter::CANNY_GBLUR_SCHARR_FILTERING:
    filter = { 3., 10., 3. };
    scale = 32.;
    name = "Scharr";
    break;
  default:
    throw(vpException(vpException::badValue, "Wrong type of filtering"));
  }
  // std::cout << "Using " << name << " filter" << std::endl;
  for (unsigned char i = 0; i < 3; ++i) {
    filter[i] = filter[i] / scale;
  }

  auto checkBooleanPatch = [](const vpImage<bool> *p_mask, const unsigned int &r, const unsigned int &c, const unsigned int &h, const unsigned int &w)
    {
      if (!p_mask) {
        return true;
      }
      bool hasToCompute = (*p_mask)[r][c];

      if (c < w - 1) { // We do not compute gradient on the last column
        hasToCompute |= (*p_mask)[r][c + 1];
        if (r < h - 1) { // We do not compute gradient on the last row
          hasToCompute |= (*p_mask)[r + 1][c + 1];
        }
      }

      if (r < h - 1) { // We do not compute gradient on the last row
        hasToCompute |= (*p_mask)[r + 1][c];
      }

      if (r > 1) { // We do not compute gradient on the first row
        hasToCompute |= (*p_mask)[r - 1][c];
        if (c < w - 1) { // We do not compute gradient on the last column
          hasToCompute |= (*p_mask)[r - 1][c + 1];
        }
      }
      return hasToCompute;
    };

  const unsigned int rStop = nbRows - 1, cStop = nbCols - 1;
  vpImage<double> IabsDiff(nbRows, nbCols);

  // Computation of the rest of the image
  for (unsigned int r = 1; r < rStop; ++r) {
    // Computation for I[r][0]
    if (checkBooleanPatch(p_mask, r, 0, nbRows, nbCols)) {
      IabsDiff[r][0] = I[r][1].V - I[r][0].V;
    }

      // Computation for all the other columns
    for (unsigned int c = 1; c < cStop; ++c) {
      if (checkBooleanPatch(p_mask, r, c, nbRows, nbCols)) {
        // Of the absolute value of the distance
        IabsDiff[r][c] = I[r][c + 1].V - I[r][c].V;
      }
    }
  }

  for (unsigned int r = 1; r < rStop; ++r) {
    for (unsigned int c = 1; c < cStop; ++c) {
      if (checkBooleanMask(p_mask, r, c)) {
        GIx[r][c] = 0.;
        for (int dr = -1; dr <= 1; ++dr) {
          GIx[r][c] += filter[dr + 1] * (IabsDiff[r + dr][c - 1] +  IabsDiff[r + dr][c]);
        }
      }
    }
  }
}

template <typename ArithmeticType, typename FilterType, bool useFullScale>
void gradientFilterY(const vpImage<vpHSV<ArithmeticType, useFullScale>> &I, vpImage<FilterType> &GIy, const vpImage<bool> *p_mask, const vpImageFilter::vpCannyFilteringAndGradientType &type)
{
  const unsigned int nbRows = I.getRows(), nbCols = I.getCols();
  std::vector<FilterType> filter(3);
  FilterType scale;
  switch (type) {
  case vpImageFilter::CANNY_GBLUR_SOBEL_FILTERING:
    filter = { 1., 2., 1. };
    scale = 8.;
    break;
  case vpImageFilter::CANNY_GBLUR_SCHARR_FILTERING:
    filter = { 3., 10., 3. };
    scale = 32.;
    break;
  default:
    throw(vpException(vpException::badValue, "Wrong type of filtering"));
  }
  for (unsigned char i = 0; i < 3; ++i) {
    filter[i] = filter[i] / scale;
  }

  const unsigned int rStop = nbRows - 1, cStop = nbCols - 1;
  vpImage<double> IabsDiff(nbRows, nbCols);

  auto checkBooleanPatch = [](const vpImage<bool> *p_mask, const unsigned int &r, const unsigned int &c, const unsigned int &h, const unsigned int &w)
    {
      if (!p_mask) {
        return true;
      }

      bool hasToCompute = (*p_mask)[r][c];
      if (c < w - 1) { // We do not compute gradient on the last column
        hasToCompute |= (*p_mask)[r][c + 1];
        if (r < h - 1) { // We do not compute gradient on the last row
          hasToCompute |= (*p_mask)[r + 1][c + 1];
        }
      }

      if (r < h - 1) { // We do not compute gradient on the last row
        hasToCompute |= (*p_mask)[r + 1][c];
      }

      if (c > 1) { // We do not compute gradient on the first column
        hasToCompute |= (*p_mask)[r][c - 1];
        if (r < h - 1) { // We do not compute gradient on the last row
          hasToCompute |= (*p_mask)[r + 1][c - 1];
        }
      }
      return hasToCompute;
    };

  // Computation for the first row
  for (unsigned int c = 0; c < nbCols; ++c) {
    if (checkBooleanPatch(p_mask, 0, c, nbRows, nbCols)) {
      IabsDiff[0][c] = I[1][c].V - I[0][c].V;
    }
  }

  // Computation for the rest of the image of d and sign
  for (unsigned int r = 1; r < rStop; ++r) {
    for (unsigned int c = 0; c < nbCols; ++c) {
      // Of the absolute value of the distance
      if (checkBooleanPatch(p_mask, r, c, nbRows, nbCols)) {
        IabsDiff[r][c] = I[r + 1][c].V - I[r][c].V;
      }
    }
  }

  // Computation of the gradient
  for (unsigned int r = 1; r < rStop; ++r) {
    for (unsigned int c = 1; c < cStop; ++c) {
      if (checkBooleanMask(p_mask, r, c)) {
        GIy[r][c] = 0.;
        for (int dc = -1; dc <= 1; ++dc) {
          GIy[r][c] += filter[dc + 1] * (IabsDiff[r - 1][c + dc] + IabsDiff[r][c + dc]);
        }
      }
    }
  }
}

template <typename ArithmeticType, bool useFullScale>
void gradientFilter(const vpImage<vpHSV<ArithmeticType, useFullScale>> &I, vpImage<double> &GIx, vpImage<double> &GIy, const vpImage<bool> *p_mask, const vpImageFilter::vpCannyFilteringAndGradientType &type)
{
  const unsigned int nbRows = I.getRows(), nbCols = I.getCols();
  GIx.resize(nbRows, nbCols, 0.);
  GIy.resize(nbRows, nbCols, 0.);
  gradientFilterX(I, GIx, p_mask, type);
  gradientFilterY(I, GIy, p_mask, type);
}
#endif

int main()
{
  bool isSuccess = true;

  // Inputs
  vpHSVTests::vpInputDataset dataset;
  bool useSobel = false;
  vpImage<double> filterX(3, 3);
  filterX[0][0] = -1.; filterX[0][1] = 0.; filterX[0][2] = 1.;
  filterX[1][0] = (useSobel ? -2. : -1.); filterX[1][1] = 0.; filterX[1][2] = (useSobel ? 2. : 1.);
  filterX[2][0] = -1.; filterX[2][1] = 0.; filterX[2][2] = 1.;

  vpImage<double> filterY(3, 3);
  filterY[0][0] = -1.; filterY[0][1] = (useSobel ? -2. : -1.); filterY[0][2] = -1.;
  filterY[1][0] = 0.; filterY[1][1] = 0.; filterY[1][2] = 0.;
  filterY[2][0] = 1.; filterY[2][1] = (useSobel ? 2. : 1.); filterY[2][2] = 1.;

  // Outputs
  vpImage<double> GIx, GIx_ref, GIy, GIy_ref;

  std::vector<vpImageFilter::vpCannyFilteringAndGradientType> types = {
    vpImageFilter::CANNY_GBLUR_SOBEL_FILTERING,
    vpImageFilter::CANNY_GBLUR_SCHARR_FILTERING
  };

  std::vector<int> nbThreads = { 1, 2 };

  vpImage<bool> *p_mask = nullptr;
  for (unsigned int i = 0; i < 2; ++i) {
    if (i == 1) {
      p_mask = &dataset.m_Imask;
    }
    for (auto type: types) {
      for (auto nbThread: nbThreads) {
        for (auto input: dataset.m_hsvUCtrue) {
          vpImageFilter::gradientFilter(input.second.m_I, GIx, GIy, nbThread, p_mask, type);
          gradientFilter(input.second.m_I, GIx_ref, GIy_ref, p_mask, type);
          bool isSuccessGIx = vpHSVTests::areAlmostEqual(GIx, "GIx", GIx_ref, "GIx_ref");
          bool isSuccessGIy = vpHSVTests::areAlmostEqual(GIy, "GIy", GIy_ref, "GIy_ref");
          isSuccess = isSuccess && isSuccessGIx && isSuccessGIy;
          if (!isSuccessGIx) {
            std::cerr << "ERROR: " << vpImageFilter::vpCannyFiltAndGradTypeToStr(type) << " along X on HSV<uchar, true> failed ! " << std::endl;
          }
          if (!isSuccessGIy) {
            std::cerr << "ERROR: " << vpImageFilter::vpCannyFiltAndGradTypeToStr(type) << " along Y on HSV<uchar, true> failed ! " << std::endl;
          }
          if (!(isSuccessGIx && isSuccessGIy)) {
            std::cout << "Type:  " << vpImageFilter::vpCannyFiltAndGradTypeToStr(type) << std::endl;
            std::cout << "nbThread:  " << nbThread << std::endl;
            std::cout << "mask ? :  " << (p_mask ? std::string("true") : std::string("false")) << std::endl;
            vpHSVTests::print(input.second.m_I, input.first);
            vpHSVTests::print(GIx, "GIx");
            vpHSVTests::print(GIy, "GIy");
            vpHSVTests::print(GIx_ref, "GIx_ref");
            vpHSVTests::print(GIy_ref, "GIy_ref");
          }
        }

        for (auto input: dataset.m_hsvUCfalse) {
          vpImageFilter::gradientFilter(input.second.m_I, GIx, GIy, nbThread, p_mask, type);
          gradientFilter(input.second.m_I, GIx_ref, GIy_ref, p_mask, type);
          bool isSuccessGIx = vpHSVTests::areAlmostEqual(GIx, "GIx", GIx_ref, "GIx_ref");
          bool isSuccessGIy = vpHSVTests::areAlmostEqual(GIy, "GIy", GIy_ref, "GIy_ref");
          isSuccess = isSuccess && isSuccessGIx && isSuccessGIy;
          if (!isSuccessGIx) {
            std::cerr << "ERROR: " << vpImageFilter::vpCannyFiltAndGradTypeToStr(type) << " along X on HSV<uchar, false> failed ! " << std::endl;
          }
          if (!isSuccessGIy) {
            std::cerr << "ERROR: " << vpImageFilter::vpCannyFiltAndGradTypeToStr(type) << " along Y on HSV<uchar, false> failed ! " << std::endl;
          }
          if (!(isSuccessGIx && isSuccessGIy)) {
            std::cout << "Type:  " << vpImageFilter::vpCannyFiltAndGradTypeToStr(type) << std::endl;
            std::cout << "nbThread:  " << nbThread << std::endl;
            std::cout << "mask ? :  " << (p_mask ? std::string("true") : std::string("false")) << std::endl;
            vpHSVTests::print(input.second.m_I, input.first);
            vpHSVTests::print(GIx, "GIx");
            vpHSVTests::print(GIy, "GIy");
            vpHSVTests::print(GIx_ref, "GIx_ref");
            vpHSVTests::print(GIy_ref, "GIy_ref");
          }
        }

        for (auto input: dataset.m_hsvDouble) {
          vpImageFilter::gradientFilter(input.second.m_I, GIx, GIy, nbThread, p_mask, type);
          gradientFilter(input.second.m_I, GIx_ref, GIy_ref, p_mask, type);
          bool isSuccessGIx = vpHSVTests::areAlmostEqual(GIx, "GIx", GIx_ref, "GIx_ref");
          bool isSuccessGIy = vpHSVTests::areAlmostEqual(GIy, "GIy", GIy_ref, "GIy_ref");
          isSuccess = isSuccess && isSuccessGIx && isSuccessGIy;
          if (!isSuccessGIx) {
            std::cerr << "ERROR: " << vpImageFilter::vpCannyFiltAndGradTypeToStr(type) << " along X on HSV<double> failed ! " << std::endl;
          }
          if (!isSuccessGIy) {
            std::cerr << "ERROR: " << vpImageFilter::vpCannyFiltAndGradTypeToStr(type) << " along Y on HSV<double> failed ! " << std::endl;
          }
          if (!(isSuccessGIx && isSuccessGIy)) {
            std::cout << "Type:  " << vpImageFilter::vpCannyFiltAndGradTypeToStr(type) << std::endl;
            std::cout << "nbThread:  " << nbThread << std::endl;
            std::cout << "mask ? :  " << (p_mask ? std::string("true") : std::string("false")) << std::endl;
            vpHSVTests::print(input.second.m_I, input.first);
            vpHSVTests::print(GIx, "GIx");
            vpHSVTests::print(GIy, "GIy");
            vpHSVTests::print(GIx_ref, "GIx_ref");
            vpHSVTests::print(GIy_ref, "GIy_ref");
          }
        }
      }
    }
  }

  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