Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testConversion.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 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 for image conversions.
32 */
33
39
40#include <iomanip>
41#include <stdlib.h>
42
43#include <visp3/core/vpConfig.h>
44#include <visp3/core/vpDebug.h>
45#include <visp3/core/vpImage.h>
46#include <visp3/core/vpImageConvert.h>
47#include <visp3/core/vpIoTools.h>
48#include <visp3/core/vpTime.h>
49#include <visp3/io/vpImageIo.h>
50#include <visp3/io/vpParseArgv.h>
51
52#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_IMGPROC)
53#include <opencv2/imgcodecs.hpp>
54#include <opencv2/imgproc/imgproc.hpp>
55#endif
56
57#if defined(VISP_HAVE_YARP)
58#include <yarp/sig/ImageFile.h>
59#endif
60
61#ifdef ENABLE_VISP_NAMESPACE
62using namespace VISP_NAMESPACE_NAME;
63#endif
64
65// List of allowed command line options
66#define GETOPTARGS "cdi:o:n:h"
67
68/*
69 Print the program options.
70
71 \param name : Program name.
72 \param badparam : Bad parameter name.
73 \param ipath : Input image path.
74 \param opath : Output image path.
75 \param user : Username.
76 \param nbiter : Iteration number.
77
78 */
79void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath, std::string user,
80 int nbiter)
81{
82 fprintf(stdout, "\n\
83Test image conversions.\n\
84\n\
85SYNOPSIS\n\
86 %s [-i <input image path>] [-o <output image path>] [-n <nb benchmark iterations>]\n\
87 [-h]\n\
88",
89name);
90
91 fprintf(stdout, "\n\
92OPTIONS: Default\n\
93 -i <input image path> %s\n\
94 Set image input path.\n\
95 From this path read \"Klimt/Klimt.pgm\"\n\
96 and \"Klimt/Klimt.ppm\" images.\n\
97 Setting the VISP_INPUT_IMAGE_PATH environment\n\
98 variable produces the same behaviour than using\n\
99 this option.\n\
100\n\
101 -o <output image path> %s\n\
102 Set image output path.\n\
103 From this directory, creates the \"%s\"\n\
104 subdirectory depending on the username, where \n\
105 Klimt_grey.pgm and Klimt_color.ppm output images\n\
106 are written.\n\
107\n\
108 -n <nb benchmark iterations> %d\n\
109 Set the number of benchmark iterations.\n\
110\n\
111 -h\n\
112 Print the help.\n\n",
113 ipath.c_str(), opath.c_str(), user.c_str(), nbiter);
114
115 if (badparam)
116 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
117}
118
132bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user,
133 int &nbIterations)
134{
135 const char *optarg_;
136 int c;
137 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
138
139 switch (c) {
140 case 'i':
141 ipath = optarg_;
142 break;
143 case 'o':
144 opath = optarg_;
145 break;
146 case 'n':
147 nbIterations = atoi(optarg_);
148 break;
149 case 'h':
150 usage(argv[0], nullptr, ipath, opath, user, nbIterations);
151 return false;
152
153 case 'c':
154 case 'd':
155 break;
156
157 default:
158 usage(argv[0], optarg_, ipath, opath, user, nbIterations);
159 return false;
160 }
161 }
162
163 if ((c == 1) || (c == -1)) {
164 // standalone param or error
165 usage(argv[0], nullptr, ipath, opath, user, nbIterations);
166 std::cerr << "ERROR: " << std::endl;
167 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
168 return false;
169 }
170
171 return true;
172}
173
174int main(int argc, const char **argv)
175{
176 try {
177 std::string env_ipath;
178 std::string opt_ipath;
179 std::string opt_opath;
180 std::string ipath;
181 std::string opath;
182 std::string filename;
183 std::string username;
184 int nbIterations = 1;
185
186 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
187 // environment variable value
189
190 // Set the default input path
191 if (!env_ipath.empty())
192 ipath = env_ipath;
193
194// Set the default output path
195#if defined(_WIN32)
196 opt_opath = "C:/temp";
197#else
198 opt_opath = "/tmp";
199#endif
200
201 // Get the user login name
202 vpIoTools::getUserName(username);
203
204 // Read the command line options
205 if (getOptions(argc, argv, opt_ipath, opt_opath, username, nbIterations) == false) {
206 return EXIT_FAILURE;
207 }
208
209 // Get the option values
210 if (!opt_ipath.empty())
211 ipath = opt_ipath;
212 if (!opt_opath.empty())
213 opath = opt_opath;
214
215 // Append to the output path string, the login name of the user
216 opath = vpIoTools::createFilePath(opath, username);
217
218 // Test if the output path exist. If no try to create it
219 if (vpIoTools::checkDirectory(opath) == false) {
220 try {
221 // Create the dirname
223 }
224 catch (...) {
225 usage(argv[0], nullptr, ipath, opt_opath, username, nbIterations);
226 std::cerr << std::endl << "ERROR:" << std::endl;
227 std::cerr << " Cannot create " << opath << std::endl;
228 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
229 return EXIT_FAILURE;
230 }
231 }
232
233 // Compare ipath and env_ipath. If they differ, we take into account
234 // the input path coming from the command line option
235 if (opt_ipath.empty()) {
236 if (ipath != env_ipath) {
237 std::cout << std::endl << "WARNING: " << std::endl;
238 std::cout << " Since -i <visp image path=" << ipath << "> "
239 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
240 << " we skip the environment variable." << std::endl;
241 }
242 }
243
244 // Test if an input path is set
245 if (opt_ipath.empty() && env_ipath.empty()) {
246 usage(argv[0], nullptr, ipath, opt_opath, username, nbIterations);
247 std::cerr << std::endl << "ERROR:" << std::endl;
248 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
249 << " environment variable to specify the location of the " << std::endl
250 << " image path where test images are located." << std::endl
251 << std::endl;
252 return EXIT_FAILURE;
253 }
254
255 //
256 // Here starts really the test
257 //
258
259 vpImage<unsigned char> Ig; // Grey image
260 vpImage<vpRGBa> Ic; // Color image
261
262 //-------------------- .pgm -> .ppm
263 std::cout << "** Convert a grey image (.pgm) to a color image (.ppm)" << std::endl;
264 // Load a grey image from the disk
265 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
266 std::cout << " Load " << filename << std::endl;
267 vpImageIo::read(Ig, filename);
268 // Create a color image from the grey
270 filename = vpIoTools::createFilePath(opath, "Klimt_color.ppm");
271 std::cout << " Resulting image saved in: " << filename << std::endl;
272 vpImageIo::write(Ic, filename);
273
274 //-------------------- .ppm -> .pgm
275 std::cout << "** Convert a color image (.ppm) to a grey image (.pgm)" << std::endl;
276 // Load a color image from the disk
277 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
278 std::cout << " Load " << filename << std::endl;
279 vpImageIo::read(Ic, filename);
280 // Create a grey image from the color
282 filename = vpIoTools::createFilePath(opath, "Klimt_grey.pgm");
283 std::cout << " Resulting image saved in: " << filename << std::endl;
284 vpImageIo::write(Ig, filename);
285
286 //-------------------- YUV -> RGB
287 std::cout << "** Convert YUV pixel value to a RGB value" << std::endl;
288 unsigned char y = 187, u = 10, v = 30;
289 unsigned char r, g, b;
290
291 // Convert a YUV pixel value to a RGB value
292 vpImageConvert::YUVToRGB(y, u, v, r, g, b);
293 std::cout << " y(" << static_cast<int>(y) << ") u(" << static_cast<int>(u) << ") v(" << static_cast<int>(v) << ") = r(" << static_cast<int>(r) << ") g(" << static_cast<int>(g)
294 << ") b(" << static_cast<int>(b) << ")" << std::endl;
295
296 vpChrono chrono;
297
298 /* ------------------------------------------------------------------------ */
299 /* conversion for the new c++ interface */
300 /* ------------------------------------------------------------------------ */
301
302#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
303 chrono.start();
305 // Convert a cv::Mat to a vpImage<vpRGBa>
307 std::cout << "** Convert a cv::Mat to a vpImage<vpRGBa>" << std::endl;
308 cv::Mat imageMat;
309 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
310 std::cout << " Reading the color image with c++ interface of opencv: " << filename << std::endl;
311#if VISP_HAVE_OPENCV_VERSION >= 0x030000
312 int flags = cv::IMREAD_COLOR;
313#else
314 int flags = CV_LOAD_IMAGE_COLOR;
315#endif
316 imageMat = cv::imread(filename, flags); // Force to a three channel BGR color image.
317 if (imageMat.data == nullptr) {
318 std::cout << " Cannot read image: " << filename << std::endl;
319 return EXIT_FAILURE;
320 }
321 vpImageConvert::convert(imageMat, Ic);
322 filename = vpIoTools::createFilePath(opath, "Klimt_color_cvMat.ppm");
323 std::cout << " Resulting image saved in: " << filename << std::endl;
324 vpImageIo::write(Ic, filename);
325
326 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
327 /* Read the pgm image */
328
329 std::cout << " Reading the grayscale image with opencv: " << filename << std::endl;
330#if VISP_HAVE_OPENCV_VERSION >= 0x030000
331 flags = cv::IMREAD_GRAYSCALE;
332#else
333 flags = CV_LOAD_IMAGE_GRAYSCALE;
334#endif
335 imageMat = cv::imread(filename, flags); // Forced to grayscale.
336 if (imageMat.data == nullptr) {
337 std::cout << " Cannot read image: " << filename << std::endl;
338 return EXIT_FAILURE;
339 }
340 vpImageConvert::convert(imageMat, Ic);
341 filename = vpIoTools::createFilePath(opath, "Klimt_grey_cvMat.ppm");
342 std::cout << " Resulting image saved in: " << filename << std::endl;
343 vpImageIo::write(Ic, filename);
344
346 // Convert a cv::Mat to a vpImage<unsigned char>
348 std::cout << "** Convert a cv::Mat to a vpImage<nsigned char>" << std::endl;
349 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
350
351 /* Read the color image */
352
353 std::cout << " Reading the color image with opencv: " << filename << std::endl;
354#if VISP_HAVE_OPENCV_VERSION >= 0x030000
355 flags = cv::IMREAD_COLOR;
356#else
357 flags = CV_LOAD_IMAGE_COLOR;
358#endif
359 imageMat = cv::imread(filename, flags); // Force to a three channel BGR color image.
360 if (imageMat.data == nullptr) {
361 std::cout << " Cannot read image: " << filename << std::endl;
362 return EXIT_FAILURE;
363 }
364 vpImageConvert::convert(imageMat, Ig);
365 filename = vpIoTools::createFilePath(opath, "Klimt_color_cvMat.pgm");
366 std::cout << " Resulting image saved in: " << filename << std::endl;
367 vpImageIo::write(Ig, filename);
368
369 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
370
371 /* Read the pgm image */
372
373 std::cout << " Reading the greyscale image with opencv: " << filename << std::endl;
374#if VISP_HAVE_OPENCV_VERSION >= 0x030000
375 flags = cv::IMREAD_GRAYSCALE;
376#else
377 flags = CV_LOAD_IMAGE_GRAYSCALE;
378#endif
379 imageMat = cv::imread(filename, flags);
380 if (imageMat.data == nullptr) {
381 std::cout << " Cannot read image: " << filename << std::endl;
382 return EXIT_FAILURE;
383 }
384 vpImageConvert::convert(imageMat, Ig);
385 filename = vpIoTools::createFilePath(opath, "Klimt_grey_cvMat.pgm");
386 std::cout << " Resulting image saved in: " << filename << std::endl;
387 vpImageIo::write(Ig, filename);
388
389 std::cout << " Convert result in " << filename << std::endl;
390
392 // Convert a vpImage<vpRGBa> to a cv::Mat
394 std::cout << "** Convert a vpImage<vpRGBa> to a cv::Mat" << std::endl;
395 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
396
397 /* Read the color image */
398
399 // Load a color image from the disk
400 std::cout << " Load " << filename << std::endl;
401 vpImageIo::read(Ic, filename);
402 vpImageConvert::convert(Ic, imageMat);
403 filename = vpIoTools::createFilePath(opath, "Klimt_ipl_color_cvMat.ppm");
404 /* Save the the current image */
405 std::cout << " Resulting image saved in: " << filename << std::endl;
406 if (!cv::imwrite(filename, imageMat)) {
407 std::cout << " Cannot write image: " << filename << std::endl;
408 return EXIT_FAILURE;
409 }
410 std::cout << " Convert result in " << filename << std::endl;
411
413 // Convert a vpImage<unsigned char> to a cv::Mat
415 std::cout << "** Convert a vpImage<unsigned char> to a cv::Mat" << std::endl;
416 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
417
418 /* Read the grey image */
419
420 // Load a color image from the disk
421 std::cout << " Load " << filename << std::endl;
422 vpImageIo::read(Ig, filename);
423 vpImageConvert::convert(Ig, imageMat);
424 filename = vpIoTools::createFilePath(opath, "Klimt_ipl_grey_cvMat.pgm");
425 /* Save the the current image */
426
427 std::cout << " Resulting image saved in: " << filename << std::endl;
428 if (!cv::imwrite(filename, imageMat)) {
429 std::cout << " Cannot write image: " << filename << std::endl;
430 return EXIT_FAILURE;
431 }
432 std::cout << " Convert result in " << filename << std::endl;
433 chrono.stop();
434 std::cout << "== Conversion c++ interface : " << chrono.getDurationMs() << " ms" << std::endl;
435#endif
436
438 // Split a vpImage<vpRGBa> to vpImage<unsigned char>
440 std::cout << "** Split a vpImage<vpRGBa> to vpImage<unsigned char>" << std::endl;
441 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
442
443 /* Read the color image */
444
445 // Load a color image from the disk
446 std::cout << " Load " << filename << std::endl;
447 vpImageIo::read(Ic, filename);
448 vpImage<unsigned char> R, G, B, a;
449 vpImageConvert::split(Ic, &R, nullptr, &B);
450 chrono.start();
451 for (int iteration = 0; iteration < nbIterations; iteration++) {
452 vpImageConvert::split(Ic, &R, nullptr, &B);
453 }
454 chrono.stop();
455
456 std::cout << " Time for " << nbIterations << " split (ms): " << chrono.getDurationMs() << std::endl;
457
458 filename = vpIoTools::createFilePath(opath, "Klimt_RChannel.pgm");
459 /* Save the the current image */
460 std::cout << " Save Klimt R channel: " << filename << std::endl;
461 vpImageIo::write(R, filename);
462
463 filename = vpIoTools::createFilePath(opath, "Klimt_BChannel.pgm");
464 /* Save the the current image */
465 std::cout << " Save Klimt B channel: " << filename << std::endl;
466 vpImageIo::write(B, filename);
467
469 // Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>
471 std::cout << "** Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>" << std::endl;
472 vpImageConvert::split(Ic, &R, &G, &B, &a);
473 chrono.start();
474 vpImage<vpRGBa> I_merge;
475 for (int iteration = 0; iteration < nbIterations; iteration++) {
476 vpImageConvert::merge(&R, &G, &B, &a, I_merge);
477 }
478 chrono.stop();
479
480 std::cout << " Time for 1000 merge (ms): " << chrono.getDurationMs() << std::endl;
481
482 filename = vpIoTools::createFilePath(opath, "Klimt_merge.ppm");
483 std::cout << " Resulting image saved in: " << filename << std::endl;
484 vpImageIo::write(I_merge, filename);
485
487 // Convert a vpImage<vpRGBa> in RGB color space to a vpImage<vpRGBa> in
488 // HSV color
490 std::cout << "** Convert a vpImage<vpRGBa> in RGB color space to a vpImage<vpRGBa> in HSV color" << std::endl;
491 unsigned int size = Ic.getSize();
492 unsigned int w = Ic.getWidth(), h = Ic.getHeight();
493 // Check the conversion RGBa <==> HSV(unsigned char)
494 std::vector<unsigned char> hue(size);
495 std::vector<unsigned char> saturation(size);
496 std::vector<unsigned char> value(size);
497
498 vpImageConvert::RGBaToHSV((unsigned char *)Ic.bitmap, &hue.front(), &saturation.front(), &value.front(), size);
499 vpImage<unsigned char> I_hue(&hue.front(), h, w);
500 vpImage<unsigned char> I_saturation(&saturation.front(), h, w);
501 vpImage<unsigned char> I_value(&value.front(), h, w);
502 vpImage<vpRGBa> I_HSV;
503 vpImageConvert::merge(&I_hue, &I_saturation, &I_value, nullptr, I_HSV);
504
505 filename = vpIoTools::createFilePath(opath, "Klimt_HSV.ppm");
506 std::cout << " Resulting image saved in: " << filename << std::endl;
507 vpImageIo::write(I_HSV, filename);
508
509 vpImage<vpRGBa> Ic_from_hsv(Ic.getHeight(), Ic.getWidth());
510 vpImageConvert::HSVToRGBa(&hue.front(), &saturation.front(), &value.front(), reinterpret_cast<unsigned char *>(Ic_from_hsv.bitmap), size);
511 for (unsigned int i = 0; i < Ic.getHeight(); i++) {
512 for (unsigned int j = 0; j < Ic.getWidth(); j++) {
513 double precision = 10.; // Due to cast to unsigned char
514 if ((!vpMath::equal(static_cast<double>(Ic[i][j].R), static_cast<double>(Ic_from_hsv[i][j].R), precision))
515 || (!vpMath::equal(static_cast<double>(Ic[i][j].G), static_cast<double>(Ic_from_hsv[i][j].G), precision))
516 || (!vpMath::equal(static_cast<double>(Ic[i][j].B), static_cast<double>(Ic_from_hsv[i][j].B), precision))) {
517 std::cerr << "Ic[i][j].R=" << static_cast<unsigned int>(Ic[i][j].R)
518 << " ; Ic_from_hsv[i][j].R=" << static_cast<unsigned int>(Ic_from_hsv[i][j].R) << " precision: " << precision << std::endl;
519 std::cerr << "Ic[i][j].G=" << static_cast<unsigned int>(Ic[i][j].G)
520 << " ; Ic_from_hsv[i][j].G=" << static_cast<unsigned int>(Ic_from_hsv[i][j].G) << " precision: " << precision << std::endl;
521 std::cerr << "Ic[i][j].B=" << static_cast<unsigned int>(Ic[i][j].B)
522 << " ; Ic_from_hsv[i][j].B=" << static_cast<unsigned int>(Ic_from_hsv[i][j].B) << " precision: " << precision << std::endl;
523 throw vpException(vpException::fatalError, "Problem with conversion between RGB <==> HSV(unsigned char)");
524 }
525 }
526 }
527 // Check the conversion RGBa <==> HSV(double)
528 std::vector<double> hue2(size);
529 std::vector<double> saturation2(size);
530 std::vector<double> value2(size);
531 vpImageConvert::RGBaToHSV((unsigned char *)Ic.bitmap, &hue2.front(), &saturation2.front(), &value2.front(), size);
532
533 std::vector<unsigned char> rgba(size * 4);
534 vpImageConvert::HSVToRGBa(&hue2.front(), &saturation2.front(), &value2.front(), &rgba.front(), size);
535
536 vpImage<vpRGBa> I_HSV2RGBa(reinterpret_cast<vpRGBa *>(&rgba.front()), h, w);
537 filename = vpIoTools::createFilePath(opath, "Klimt_HSV2RGBa.ppm");
538 std::cout << " Resulting image saved in: " << filename << std::endl;
539 vpImageIo::write(I_HSV2RGBa, filename);
540
541 for (unsigned int i = 0; i < Ic.getHeight(); i++) {
542 for (unsigned int j = 0; j < Ic.getWidth(); j++) {
543 if (Ic[i][j].R != I_HSV2RGBa[i][j].R || Ic[i][j].G != I_HSV2RGBa[i][j].G || Ic[i][j].B != I_HSV2RGBa[i][j].B) {
544 std::cerr << "Ic[i][j].R=" << static_cast<unsigned>(Ic[i][j].R)
545 << " ; I_HSV2RGBa[i][j].R=" << static_cast<unsigned>(I_HSV2RGBa[i][j].R) << std::endl;
546 std::cerr << "Ic[i][j].G=" << static_cast<unsigned>(Ic[i][j].G)
547 << " ; I_HSV2RGBa[i][j].G=" << static_cast<unsigned>(I_HSV2RGBa[i][j].G) << std::endl;
548 std::cerr << "Ic[i][j].B=" << static_cast<unsigned>(Ic[i][j].B)
549 << " ; I_HSV2RGBa[i][j].B=" << static_cast<unsigned>(I_HSV2RGBa[i][j].B) << std::endl;
550 throw vpException(vpException::fatalError, "Problem with conversion between RGB <==> HSV(double)");
551 }
552 }
553 }
554
556 // Test construction of a vpImage from an array with copyData==true
558 std::cout << "** Construction of a vpImage from an array with copyData==true" << std::endl;
559 std::vector<unsigned char> rgba2(size * 4);
560 std::fill(rgba2.begin(), rgba2.end(), 127);
561 vpImage<vpRGBa> I_copyData(reinterpret_cast<vpRGBa *>(&rgba2.front()), h, w, true);
562
563 filename = vpIoTools::createFilePath(opath, "I_copyData.ppm");
564 std::cout << " Resulting image saved in: " << filename << std::endl;
565 vpImageIo::write(I_copyData, filename);
566
567 if (I_copyData.getSize() > 0) {
568 I_copyData[0][0].R = 10;
569 }
570
571 // Test color conversion
572 {
573 std::cout << "** Test color conversion" << std::endl;
574 // RGBa to Grayscale
575 vpImage<vpRGBa> I_color;
576 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
577 vpImageIo::read(I_color, filename);
578
579 // RGB to Grayscale conversion
580 std::vector<unsigned char> rgb_array(I_color.getSize() * 3);
581 vpImageConvert::RGBaToRGB((unsigned char *)I_color.bitmap, &rgb_array.front(), I_color.getSize());
582
583#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
584 // BGR cv::Mat to Grayscale
585 std::cout << "\n BGR cv::Mat to Grayscale" << std::endl;
586 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
587 cv::Mat colorMat = cv::imread(filename);
588 std::cout << " colorMat=" << colorMat.cols << "x" << colorMat.rows << std::endl;
589
590 // Test RGB to Grayscale + Flip
591 std::cout << "\n RGB to Grayscale + Flip" << std::endl;
592 std::vector<unsigned char> rgb2gray_flip_array_sse(I_color.getSize());
593 vpImageConvert::RGBToGrey(&rgb_array.front(), &rgb2gray_flip_array_sse.front(), I_color.getWidth(),
594 I_color.getHeight(), true);
595 vpImage<unsigned char> I_rgb2gray_flip_sse(&rgb2gray_flip_array_sse.front(), I_color.getHeight(),
596 I_color.getWidth());
597
598 filename = vpIoTools::createFilePath(opath, "I_rgb2gray_flip_sse.pgm");
599 std::cout << " Resulting image saved in: " << filename << std::endl;
600 vpImageIo::write(I_rgb2gray_flip_sse, filename);
601
602 // Test BGR to Grayscale + Flip
603 std::cout << "\n Conversion BGR to Grayscale + Flip" << std::endl;
604 std::vector<unsigned char> bgr2gray_flip_array_sse(I_color.getSize());
605 vpImage<unsigned char> I_bgr2gray_flip_sse(&bgr2gray_flip_array_sse.front(), I_color.getHeight(),
606 I_color.getWidth());
607 vpImageConvert::convert(colorMat, I_bgr2gray_flip_sse, true);
608
609 filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_sse.pgm");
610 std::cout << " Resulting image saved in: " << filename << std::endl;
611 vpImageIo::write(I_bgr2gray_flip_sse, filename);
612
613 // Test RGB to Grayscale + Flip + Crop
614 std::cout << "\n RGB to Grayscale + Flip + Crop" << std::endl;
615 cv::Rect rect_roi(11, 17, 347, 449);
616 cv::Mat colorMat_crop = colorMat(rect_roi);
617 cv::Mat colorMat_crop_continuous = colorMat(rect_roi).clone();
618 std::cout << " colorMat_crop: " << colorMat_crop.cols << "x" << colorMat_crop.rows << " is continuous? "
619 << colorMat_crop.isContinuous() << std::endl;
620 std::cout << " colorMat_crop_continuous: " << colorMat_crop_continuous.cols << "x" << colorMat_crop_continuous.rows
621 << " is continuous? " << colorMat_crop_continuous.isContinuous() << std::endl;
622
623 vpImage<vpRGBa> I_color_crop(static_cast<unsigned int>(rect_roi.height - rect_roi.y),
624 static_cast<unsigned int>(rect_roi.width - rect_roi.x));
625 for (unsigned int i = static_cast<unsigned int>(rect_roi.y); i < static_cast<unsigned int>(rect_roi.height); i++) {
626 for (unsigned int j = static_cast<unsigned int>(rect_roi.x); j < static_cast<unsigned int>(rect_roi.width); j++) {
627 I_color_crop[static_cast<unsigned int>(static_cast<int>(i) - rect_roi.y)][static_cast<unsigned int>(static_cast<int>(j) - rect_roi.x)] = I_color[i][j];
628 }
629 }
630 filename = vpIoTools::createFilePath(opath, "I_color_crop.ppm");
631 std::cout << " Resulting image saved in: " << filename << std::endl;
632 vpImageIo::write(I_color_crop, filename);
633
634 std::vector<unsigned char> rgb_array_crop(I_color_crop.getSize() * 3);
635 vpImageConvert::RGBaToRGB((unsigned char *)I_color_crop.bitmap, &rgb_array_crop.front(), I_color_crop.getSize());
636
637 std::vector<unsigned char> rgb2gray_flip_crop_array_sse(I_color_crop.getSize());
638 vpImageConvert::RGBToGrey(&rgb_array_crop.front(), &rgb2gray_flip_crop_array_sse.front(), I_color_crop.getWidth(),
639 I_color_crop.getHeight(), true);
640 vpImage<unsigned char> I_rgb2gray_flip_crop_sse(&rgb2gray_flip_crop_array_sse.front(), I_color_crop.getHeight(),
641 I_color_crop.getWidth());
642
643 filename = vpIoTools::createFilePath(opath, "I_rgb2gray_flip_crop_sse.pgm");
644 std::cout << " Resulting image saved in: " << filename << std::endl;
645 vpImageIo::write(I_rgb2gray_flip_crop_sse, filename);
646
647 // Test BGR to Grayscale + Flip + Crop
648 std::cout << "\n BGR to Grayscale + Flip + Crop" << std::endl;
649 vpImage<unsigned char> I_bgr2gray_flip_crop_sse(I_color_crop.getHeight(), I_color_crop.getWidth());
650 vpImageConvert::convert(colorMat_crop_continuous, I_bgr2gray_flip_crop_sse, true);
651
652 filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_crop_sse.pgm");
653 std::cout << " Resulting image saved in: " << filename << std::endl;
654 vpImageIo::write(I_bgr2gray_flip_crop_sse, filename);
655
656 // Test BGR to Grayscale + Flip + Crop + No continuous Mat
657 std::cout << "\n BGR to Grayscale + Flip + Crop + No continuous Mat" << std::endl;
658 vpImage<unsigned char> I_bgr2gray_flip_crop_no_continuous_sse(I_color_crop.getHeight(), I_color_crop.getWidth());
659 vpImageConvert::convert(colorMat_crop, I_bgr2gray_flip_crop_no_continuous_sse, true);
660
661 filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_crop_no_continuous_sse.pgm");
662 std::cout << " Resulting image saved in: " << filename << std::endl;
663 vpImageIo::write(I_bgr2gray_flip_crop_no_continuous_sse, filename);
664#endif
665 std::cout << " Test succeed" << std::endl;
666 }
667
668#if defined(VISP_HAVE_YARP)
670 // Convert a ViSP to Yarp uchar image
672 std::cout << "** Test ViSP to Yarp image conversion by copy" << std::endl;
673 {
674 bool convert_by_copy = true;
675 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
676 std::cout << " Reading the gray image with ViSP: " << filename << std::endl;
678 // Read an image on a disk
679 vpImageIo::read(I, filename);
680
681 yarp::sig::ImageOf< yarp::sig::PixelMono > *Iyarp = new yarp::sig::ImageOf<yarp::sig::PixelMono >();
682 // Convert the vpImage<unsigned char> to a yarp::sig::ImageOf<yarp::sig::PixelMono>
683 vpImageConvert::convert(I, Iyarp, convert_by_copy);
684 // Write the image
685 filename = vpIoTools::createFilePath(opath, "Klimt_yarp_copy.pgm");
686 std::cout << " Converted Yarp image saved in: " << filename << std::endl;
687 yarp::sig::file::write(*Iyarp, filename, yarp::sig::file::FORMAT_PGM);
688
689 std::cout << " Reading the gray image with Yarp: " << filename << std::endl;
690 yarp::sig::ImageOf< yarp::sig::PixelMono > *IIyarp = new yarp::sig::ImageOf<yarp::sig::PixelMono >();
691 yarp::sig::file::read(*IIyarp, filename, yarp::sig::file::FORMAT_PGM);
693 vpImageConvert::convert(IIyarp, II, convert_by_copy);
694 filename = vpIoTools::createFilePath(opath, "Klimt_yarp_copy_visp.pgm");
695 std::cout << " Converted image in ViSP saved in: " << filename << std::endl;
696 vpImageIo::write(II, filename);
697 if (I != II) {
698 std::cout << " Yarp gray conversion test failed" << std::endl;
699 return EXIT_FAILURE;
700 }
701 std::cout << std::endl;
702 }
703 {
704 bool convert_by_copy = true;
705 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
706 std::cout << " Reading the color image with ViSP: " << filename << std::endl;
708 // Read an image on a disk
709 vpImageIo::read(I, filename);
710
711 yarp::sig::ImageOf< yarp::sig::PixelRgba > *Iyarp = new yarp::sig::ImageOf<yarp::sig::PixelRgba >();
712 // Convert the vpImage<vpRGBa> to a yarp::sig::ImageOf<yarp::sig::PixelRgba>
713 vpImageConvert::convert(I, Iyarp, convert_by_copy);
714 // Write the image
715 filename = vpIoTools::createFilePath(opath, "Klimt_yarp_copy.ppm");
716 std::cout << " Converted image saved in: " << filename << std::endl;
717 yarp::sig::file::write(*Iyarp, filename, yarp::sig::file::FORMAT_PPM);
718
719 std::cout << " Reading the color image with Yarp: " << filename << std::endl;
720 yarp::sig::ImageOf< yarp::sig::PixelRgba > *IIyarp = new yarp::sig::ImageOf<yarp::sig::PixelRgba >();
721 yarp::sig::file::read(*IIyarp, filename, yarp::sig::file::FORMAT_PPM);
723 vpImageConvert::convert(IIyarp, II, convert_by_copy);
724 filename = vpIoTools::createFilePath(opath, "Klimt_yarp_copy_visp.ppm");
725 std::cout << " Converted image in ViSP saved in: " << filename << std::endl;
726 vpImageIo::write(II, filename);
727 if (I != II) {
728 std::cout << " Yarp color conversion test failed" << std::endl;
729 return EXIT_FAILURE;
730 }
731 std::cout << std::endl;
732 }
733
734 std::cout << "** Test ViSP to Yarp image conversion without copy" << std::endl;
735 {
736 bool convert_by_copy = false;
737 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
738 std::cout << " Reading the gray image with ViSP: " << filename << std::endl;
740 // Read an image on a disk
741 vpImageIo::read(I, filename);
742
743 yarp::sig::ImageOf< yarp::sig::PixelMono > *Iyarp = new yarp::sig::ImageOf<yarp::sig::PixelMono >();
744 // Convert the vpImage<unsigned char> to a yarp::sig::ImageOf<yarp::sig::PixelMono>
745 vpImageConvert::convert(I, Iyarp, convert_by_copy);
746 // Write the image
747 filename = vpIoTools::createFilePath(opath, "Klimt_yarp_without_copy.pgm");
748 std::cout << " Converted Yarp image saved in: " << filename << std::endl;
749 yarp::sig::file::write(*Iyarp, filename, yarp::sig::file::FORMAT_PGM);
750
751 std::cout << " Reading the gray image with Yarp: " << filename << std::endl;
752 yarp::sig::ImageOf< yarp::sig::PixelMono > *IIyarp = new yarp::sig::ImageOf<yarp::sig::PixelMono >();
753 yarp::sig::file::read(*IIyarp, filename, yarp::sig::file::FORMAT_PGM);
755 vpImageConvert::convert(IIyarp, II, convert_by_copy);
756 filename = vpIoTools::createFilePath(opath, "Klimt_yarp_without_copy_visp.pgm");
757 std::cout << " Converted image in ViSP saved in: " << filename << std::endl;
758 vpImageIo::write(II, filename);
759 if (I != II) {
760 std::cout << " Yarp gray conversion test failed" << std::endl;
761 return EXIT_FAILURE;
762 }
763 else {
764 std::cout << " Yarp gray conversion test succeed" << std::endl;
765 }
766 std::cout << std::endl;
767 }
768 {
769 bool convert_by_copy = false;
770 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
771 std::cout << " Reading the color image with ViSP: " << filename << std::endl;
773 // Read an image on a disk
774 vpImageIo::read(I, filename);
775
776 yarp::sig::ImageOf< yarp::sig::PixelRgba > *Iyarp = new yarp::sig::ImageOf<yarp::sig::PixelRgba >();
777 // Convert the vpImage<vpRGBa> to a yarp::sig::ImageOf<yarp::sig::PixelRgba>
778 vpImageConvert::convert(I, Iyarp, convert_by_copy);
779 // Write the image
780 filename = vpIoTools::createFilePath(opath, "Klimt_yarp_without_copy.ppm");
781 std::cout << " Converted image saved in: " << filename << std::endl;
782 yarp::sig::file::write(*Iyarp, filename, yarp::sig::file::FORMAT_PPM);
783
784 std::cout << " Reading the color image with Yarp: " << filename << std::endl;
785 yarp::sig::ImageOf< yarp::sig::PixelRgba > *IIyarp = new yarp::sig::ImageOf<yarp::sig::PixelRgba >();
786 yarp::sig::file::read(*IIyarp, filename, yarp::sig::file::FORMAT_PPM);
788 vpImageConvert::convert(IIyarp, II, convert_by_copy);
789 filename = vpIoTools::createFilePath(opath, "Klimt_yarp_without_copy_visp.ppm");
790 std::cout << " Converted image in ViSP saved in: " << filename << std::endl;
791 vpImageIo::write(II, filename);
792 if (I != II) {
793 std::cout << " Yarp RGBa color conversion test failed" << std::endl;
794 return EXIT_FAILURE;
795 }
796 else {
797 std::cout << " Yarp RGBa color conversion test succeed" << std::endl;
798 }
799
800 yarp::sig::ImageOf< yarp::sig::PixelRgb > *IIIyarp = new yarp::sig::ImageOf<yarp::sig::PixelRgb >();
801 // Convert the vpImage<vpRGBa> to a yarp::sig::ImageOf<yarp::sig::PixelRgb>
802 vpImageConvert::convert(I, IIIyarp);
803 // Write the image
804 filename = vpIoTools::createFilePath(opath, "Klimt_yarp_without_copy_rgb.ppm");
805 std::cout << " Converted RGB image saved in: " << filename << std::endl;
806 yarp::sig::file::write(*Iyarp, filename, yarp::sig::file::FORMAT_PPM);
807
808 std::cout << " Reading the RGB color image with Yarp: " << filename << std::endl;
809 yarp::sig::file::read(*IIIyarp, filename, yarp::sig::file::FORMAT_PPM);
810 vpImage<vpRGBa> III;
811 vpImageConvert::convert(IIIyarp, III);
812 filename = vpIoTools::createFilePath(opath, "Klimt_yarp_without_copy_visp_rgb.ppm");
813 std::cout << " Converted RGB image in ViSP saved in: " << filename << std::endl;
814 vpImageIo::write(II, filename);
815 if (I != III) {
816 std::cout << " Yarp RGB color conversion test failed" << std::endl;
817 return EXIT_FAILURE;
818 }
819 else {
820 std::cout << " Yarp RGB color conversion test succeed" << std::endl;
821 }
822
823 std::cout << std::endl;
824 }
825#endif
826
827 std::cout << "** All the tests succeed" << std::endl;
828
829 return EXIT_SUCCESS;
830 }
831 catch (const vpException &e) {
832 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
833 return EXIT_FAILURE;
834 }
835}
void start(bool reset=true)
Definition vpTime.cpp:411
void stop()
Definition vpTime.cpp:426
double getDurationMs()
Definition vpTime.cpp:400
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ fatalError
Fatal error.
Definition vpException.h:72
static void HSVToRGBa(const double *hue, const double *saturation, const double *value, unsigned char *rgba, unsigned int size)
static void merge(const vpImage< unsigned char > *R, const vpImage< unsigned char > *G, const vpImage< unsigned char > *B, const vpImage< unsigned char > *a, vpImage< vpRGBa > &RGBa)
static void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g, unsigned char &b)
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=nullptr)
static void RGBaToHSV(const unsigned char *rgba, double *hue, double *saturation, double *value, unsigned int size)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int width, unsigned int height, bool flip=false)
static void RGBaToRGB(unsigned char *rgba, unsigned char *rgb, unsigned int size)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:131
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getSize() const
Definition vpImage.h:221
Type * bitmap
points toward the bitmap
Definition vpImage.h:135
unsigned int getHeight() const
Definition vpImage.h:181
static std::string getViSPImagesDataPath()
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static std::string createFilePath(const std::string &parent, const std::string &child)
static void makeDirectory(const std::string &dirname)
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:470
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)