Visual Servoing Platform version 3.5.0
testConversion.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See http://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Test for image conversions.
33 *
34 * Authors:
35 * Fabien Spindler
36 *
37 *****************************************************************************/
38
39#include <iomanip>
40#include <stdlib.h>
41
42#include <visp3/core/vpConfig.h>
43#include <visp3/core/vpDebug.h>
44#include <visp3/core/vpImage.h>
45#include <visp3/core/vpImageConvert.h>
46#include <visp3/core/vpIoTools.h>
47#include <visp3/core/vpTime.h>
48#include <visp3/io/vpImageIo.h>
49#include <visp3/io/vpParseArgv.h>
50
57// List of allowed command line options
58#define GETOPTARGS "cdi:o:n:h"
59
60/*
61 Print the program options.
62
63 \param name : Program name.
64 \param badparam : Bad parameter name.
65 \param ipath: Input image path.
66 \param opath : Output image path.
67 \param user : Username.
68 \param nbiter : Iteration number.
69
70 */
71void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user, int nbiter)
72{
73 fprintf(stdout, "\n\
74Test image conversions.\n\
75\n\
76SYNOPSIS\n\
77 %s [-i <input image path>] [-o <output image path>] [-n <nb benchmark iterations>]\n\
78 [-h]\n \
79", name);
80
81 fprintf(stdout, "\n\
82OPTIONS: Default\n\
83 -i <input image path> %s\n\
84 Set image input path.\n\
85 From this path read \"Klimt/Klimt.pgm\"\n\
86 and \"Klimt/Klimt.ppm\" images.\n\
87 Setting the VISP_INPUT_IMAGE_PATH environment\n\
88 variable produces the same behaviour than using\n\
89 this option.\n\
90\n\
91 -o <output image path> %s\n\
92 Set image output path.\n\
93 From this directory, creates the \"%s\"\n\
94 subdirectory depending on the username, where \n\
95 Klimt_grey.pgm and Klimt_color.ppm output images\n\
96 are written.\n\
97\n\
98 -n <nb benchmark iterations> %d\n\
99 Set the number of benchmark iterations.\n\
100\n\
101 -h\n\
102 Print the help.\n\n", ipath.c_str(), opath.c_str(), user.c_str(), nbiter);
103
104 if (badparam)
105 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
106}
107
121bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user,
122 int &nbIterations)
123{
124 const char *optarg_;
125 int c;
126 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
127
128 switch (c) {
129 case 'i':
130 ipath = optarg_;
131 break;
132 case 'o':
133 opath = optarg_;
134 break;
135 case 'n':
136 nbIterations = atoi(optarg_);
137 break;
138 case 'h':
139 usage(argv[0], NULL, ipath, opath, user, nbIterations);
140 return false;
141
142 case 'c':
143 case 'd':
144 break;
145
146 default:
147 usage(argv[0], optarg_, ipath, opath, user, nbIterations);
148 return false;
149 }
150 }
151
152 if ((c == 1) || (c == -1)) {
153 // standalone param or error
154 usage(argv[0], NULL, ipath, opath, user, nbIterations);
155 std::cerr << "ERROR: " << std::endl;
156 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
157 return false;
158 }
159
160 return true;
161}
162
163int main(int argc, const char **argv)
164{
165 try {
166 std::string env_ipath;
167 std::string opt_ipath;
168 std::string opt_opath;
169 std::string ipath;
170 std::string opath;
171 std::string filename;
172 std::string username;
173 int nbIterations = 1;
174
175 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
176 // environment variable value
178
179 // Set the default input path
180 if (!env_ipath.empty())
181 ipath = env_ipath;
182
183// Set the default output path
184#if defined(_WIN32)
185 opt_opath = "C:/temp";
186#else
187 opt_opath = "/tmp";
188#endif
189
190 // Get the user login name
191 vpIoTools::getUserName(username);
192
193 // Read the command line options
194 if (getOptions(argc, argv, opt_ipath, opt_opath, username, nbIterations) == false) {
195 exit(-1);
196 }
197
198 // Get the option values
199 if (!opt_ipath.empty())
200 ipath = opt_ipath;
201 if (!opt_opath.empty())
202 opath = opt_opath;
203
204 // Append to the output path string, the login name of the user
205 opath = vpIoTools::createFilePath(opath, username);
206
207 // Test if the output path exist. If no try to create it
208 if (vpIoTools::checkDirectory(opath) == false) {
209 try {
210 // Create the dirname
212 } catch (...) {
213 usage(argv[0], NULL, ipath, opt_opath, username, nbIterations);
214 std::cerr << std::endl << "ERROR:" << std::endl;
215 std::cerr << " Cannot create " << opath << std::endl;
216 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
217 exit(-1);
218 }
219 }
220
221 // Compare ipath and env_ipath. If they differ, we take into account
222 // the input path comming from the command line option
223 if (opt_ipath.empty()) {
224 if (ipath != env_ipath) {
225 std::cout << std::endl << "WARNING: " << std::endl;
226 std::cout << " Since -i <visp image path=" << ipath << "> "
227 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
228 << " we skip the environment variable." << std::endl;
229 }
230 }
231
232 // Test if an input path is set
233 if (opt_ipath.empty() && env_ipath.empty()) {
234 usage(argv[0], NULL, ipath, opt_opath, username, nbIterations);
235 std::cerr << std::endl << "ERROR:" << std::endl;
236 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
237 << " environment variable to specify the location of the " << std::endl
238 << " image path where test images are located." << std::endl
239 << std::endl;
240 exit(-1);
241 }
242
243 //
244 // Here starts really the test
245 //
246
247 vpImage<unsigned char> Ig; // Grey image
248 vpImage<vpRGBa> Ic; // Color image
249
250 //-------------------- .pgm -> .ppm
251 std::cout << "** Convert a grey image (.pgm) to a color image (.ppm)" << std::endl;
252 // Load a grey image from the disk
253 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
254 std::cout << " Load " << filename << std::endl;
255 vpImageIo::read(Ig, filename);
256 // Create a color image from the grey
258 filename = vpIoTools::createFilePath(opath, "Klimt_color.ppm");
259 std::cout << " Resulting image saved in: " << filename << std::endl;
260 vpImageIo::write(Ic, filename);
261
262 //-------------------- .ppm -> .pgm
263 std::cout << "** Convert a color image (.ppm) to a grey image (.pgm)" << std::endl;
264 // Load a color image from the disk
265 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
266 std::cout << " Load " << filename << std::endl;
267 vpImageIo::read(Ic, filename);
268 // Create a grey image from the color
270 filename = vpIoTools::createFilePath(opath, "Klimt_grey.pgm");
271 std::cout << " Resulting image saved in: " << filename << std::endl;
272 vpImageIo::write(Ig, filename);
273
274 //-------------------- YUV -> RGB
275 std::cout << "** Convert YUV pixel value to a RGB value" << std::endl;
276 unsigned char y = 187, u = 10, v = 30;
277 unsigned char r, g, b;
278
279 // Convert a YUV pixel value to a RGB value
280 vpImageConvert::YUVToRGB(y, u, v, r, g, b);
281 std::cout << " y(" << (int)y << ") u(" << (int)u << ") v(" << (int)v << ") = r(" << (int)r << ") g(" << (int)g
282 << ") b(" << (int)b << ")" << std::endl;
283
284 vpChrono chrono;
285#ifdef VISP_HAVE_OPENCV
286#if VISP_HAVE_OPENCV_VERSION < 0x020408
287 double t0 = vpTime::measureTimeMs();
289 // Convert a IplImage to a vpImage<vpRGBa>
291 std::cout << "** Convert an IplImage to a vpImage<vpRGBa>" << std::endl;
292 IplImage *image = NULL;
293 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
294
295 /* Read the color image */
296
297 std::cout << " Reading the color image with opencv: " << filename << std::endl;
298 if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
299 std::cout << " Cannot read image: " << filename << std::endl;
300 return (-1);
301 }
302 vpImageConvert::convert(image, Ic);
303 filename = vpIoTools::createFilePath(opath, "Klimt_color_cv.ppm");
304 std::cout << " Resulting image saved in: " << filename << std::endl;
305 vpImageIo::write(Ic, filename);
306
307 std::cout << " Convert result in " << filename << std::endl;
308
309 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
310
311 /* Read the pgm image */
312 std::cout << " Reading the greyscale image with opencv: " << filename << std::endl;
313 if (image != NULL)
314 cvReleaseImage(&image);
315 if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
316 std::cout << " Cannot read image: " << filename << std::endl;
317 return (-1);
318 }
319 vpImageConvert::convert(image, Ic);
320 filename = vpIoTools::createFilePath(opath, "Klimt_grey_cv.ppm");
321 std::cout << " Resulting image saved in: " << filename << std::endl;
322 vpImageIo::write(Ic, filename);
323
324 std::cout << " Convert result in " << filename << std::endl;
325
327 // Convert a IplImage to a vpImage<unsigned char>
329 std::cout << "** Convert an IplImage to a vpImage<unsigned char>" << std::endl;
330 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
331
332 /* Read the color image */
333
334 std::cout << " Reading the color image with opencv: " << filename << std::endl;
335 if (image != NULL)
336 cvReleaseImage(&image);
337 if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
338 std::cout << " Cannot read image: " << filename << std::endl;
339 return (-1);
340 }
341 vpImageConvert::convert(image, Ig);
342 filename = vpIoTools::createFilePath(opath, "Klimt_color_cv.pgm");
343 std::cout << " Resulting image saved in: " << filename << std::endl;
344 vpImageIo::write(Ig, filename);
345
346 std::cout << " Convert result in " << filename << std::endl;
347
348 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
349
350 /* Read the pgm image */
351
352 std::cout << " Reading the greyscale image with opencv: " << filename << std::endl;
353 if (image != NULL)
354 cvReleaseImage(&image);
355 if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
356 std::cout << " Cannot read image: " << filename << std::endl;
357 return (-1);
358 }
359 vpImageConvert::convert(image, Ig);
360 filename = vpIoTools::createFilePath(opath, "Klimt_grey_cv.pgm");
361 std::cout << " Resulting image saved in: " << filename << std::endl;
362 vpImageIo::write(Ig, filename);
363
364 std::cout << " Convert result in " << filename << std::endl;
365
367 // Convert a vpImage<vpRGBa> to a IplImage
369 std::cout << "** Convert a vpImage<vpRGBa> to an IplImage" << std::endl;
370 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
371
372 /* Read the color image */
373
374 // Load a color image from the disk
375 std::cout << " Load " << filename << std::endl;
376 vpImageIo::read(Ic, filename);
377 vpImageConvert::convert(Ic, image);
378 filename = vpIoTools::createFilePath(opath, "Klimt_ipl_color_cv.ppm");
379 /* Save the the current image */
380 std::cout << " Write " << filename << std::endl;
381 if ((cvSaveImage(filename.c_str(), image)) == 0) {
382 std::cout << " Cannot write image: " << filename << std::endl;
383 if (image != NULL)
384 cvReleaseImage(&image);
385 return (-1);
386 }
387 std::cout << " Convert result in " << filename << std::endl;
388
390 // Convert a vpImage<unsigned char> to an IplImage
392 std::cout << "** Convert a vpImage<unsigned char> to an IplImage" << std::endl;
393 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
394
395 /* Read the grey image */
396
397 // Load a color image from the disk
398 std::cout << " Load " << filename << std::endl;
399 vpImageIo::read(Ig, filename);
400 vpImageConvert::convert(Ig, image);
401 filename = vpIoTools::createFilePath(opath, "Klimt_ipl_grey_cv.pgm");
402 /* Save the the current image */
403
404 std::cout << " Write " << filename << std::endl;
405 if ((cvSaveImage(filename.c_str(), image)) == 0) {
406 std::cout << " Cannot write image: " << std::endl << filename << std::endl;
407 if (image != NULL)
408 cvReleaseImage(&image);
409 return (-1);
410 }
411 std::cout << " Convert result in " << filename << std::endl;
412
413 if (image != NULL)
414 cvReleaseImage(&image);
415 double t1 = vpTime::measureTimeMs();
416 std::cout << "== Conversion c interface : " << t1 - t0 << " ms" << std::endl;
417#endif
418
419/* ------------------------------------------------------------------------ */
420/* conversion for the new c++ interface */
421/* ------------------------------------------------------------------------ */
422
423#if VISP_HAVE_OPENCV_VERSION >= 0x020100
424 chrono.start();
426 // Convert a cv::Mat to a vpImage<vpRGBa>
428 std::cout << "** Convert a cv::Mat to a vpImage<vpRGBa>" << std::endl;
429 cv::Mat imageMat;
430 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
431 std::cout << " Reading the color image with c++ interface of opencv: " << filename << std::endl;
432#if VISP_HAVE_OPENCV_VERSION >= 0x030000
433 int flags = cv::IMREAD_COLOR;
434#elif VISP_HAVE_OPENCV_VERSION >= 0x020100
435 int flags = CV_LOAD_IMAGE_COLOR;
436#endif
437 imageMat = cv::imread(filename, flags); // Force to a three channel BGR color image.
438 if (imageMat.data == NULL) {
439 std::cout << " Cannot read image: " << filename << std::endl;
440 return -1;
441 }
442 vpImageConvert::convert(imageMat, Ic);
443 filename = vpIoTools::createFilePath(opath, "Klimt_color_cvMat.ppm");
444 std::cout << " Resulting image saved in: " << filename << std::endl;
445 vpImageIo::write(Ic, filename);
446
447 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
448 /* Read the pgm image */
449
450 std::cout << " Reading the greyscale image with opencv: " << filename << std::endl;
451#if VISP_HAVE_OPENCV_VERSION >= 0x030000
452 flags = cv::IMREAD_GRAYSCALE;
453#elif VISP_HAVE_OPENCV_VERSION >= 0x020100
454 flags = CV_LOAD_IMAGE_GRAYSCALE;
455#endif
456 imageMat = cv::imread(filename, flags); // Forced to grayscale.
457 if (imageMat.data == NULL) {
458 std::cout << " Cannot read image: " << filename << std::endl;
459 return (-1);
460 }
461 vpImageConvert::convert(imageMat, Ic);
462 filename = vpIoTools::createFilePath(opath, "Klimt_grey_cvMat.ppm");
463 std::cout << " Resulting image saved in: " << filename << std::endl;
464 vpImageIo::write(Ic, filename);
465
467 // Convert a cv::Mat to a vpImage<unsigned char>
469 std::cout << "** Convert a cv::Mat to a vpImage<nsigned char>" << std::endl;
470 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
471
472 /* Read the color image */
473
474 std::cout << " Reading the color image with opencv: " << filename << std::endl;
475#if VISP_HAVE_OPENCV_VERSION >= 0x030000
476 flags = cv::IMREAD_COLOR;
477#elif VISP_HAVE_OPENCV_VERSION >= 0x020100
478 flags = CV_LOAD_IMAGE_COLOR;
479#endif
480 imageMat = cv::imread(filename, flags); // Force to a three channel BGR color image.
481 if (imageMat.data == NULL) {
482 std::cout << " Cannot read image: " << filename << std::endl;
483 return -1;
484 }
485 vpImageConvert::convert(imageMat, Ig);
486 filename = vpIoTools::createFilePath(opath, "Klimt_color_cvMat.pgm");
487 std::cout << " Resulting image saved in: " << filename << std::endl;
488 vpImageIo::write(Ig, filename);
489
490 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
491
492 /* Read the pgm image */
493
494 std::cout << " Reading the greyscale image with opencv: " << filename << std::endl;
495#if VISP_HAVE_OPENCV_VERSION >= 0x030000
496 flags = cv::IMREAD_GRAYSCALE;
497#elif VISP_HAVE_OPENCV_VERSION >= 0x020100
498 flags = CV_LOAD_IMAGE_GRAYSCALE;
499#endif
500 imageMat = cv::imread(filename, flags);
501 if (imageMat.data == NULL) {
502 std::cout << " Cannot read image: " << filename << std::endl;
503 return (-1);
504 }
505 vpImageConvert::convert(imageMat, Ig);
506 filename = vpIoTools::createFilePath(opath, "Klimt_grey_cvMat.pgm");
507 std::cout << " Resulting image saved in: " << filename << std::endl;
508 vpImageIo::write(Ig, filename);
509
510 std::cout << " Convert result in " << filename << std::endl;
511
513 // Convert a vpImage<vpRGBa> to a cv::Mat
515 std::cout << "** Convert a vpImage<vpRGBa> to a cv::Mat" << std::endl;
516 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
517
518 /* Read the color image */
519
520 // Load a color image from the disk
521 std::cout << " Load " << filename << std::endl;
522 vpImageIo::read(Ic, filename);
523 vpImageConvert::convert(Ic, imageMat);
524 filename = vpIoTools::createFilePath(opath, "Klimt_ipl_color_cvMat.ppm");
525 /* Save the the current image */
526 std::cout << " Resulting image saved in: " << filename << std::endl;
527 if (!cv::imwrite(filename, imageMat)) {
528 std::cout << " Cannot write image: " << filename << std::endl;
529 return (-1);
530 }
531 std::cout << " Convert result in " << filename << std::endl;
532
534 // Convert a vpImage<unsigned char> to a cv::Mat
536 std::cout << "** Convert a vpImage<unsigned char> to a cv::Mat" << std::endl;
537 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
538
539 /* Read the grey image */
540
541 // Load a color image from the disk
542 std::cout << " Load " << filename << std::endl;
543 vpImageIo::read(Ig, filename);
544 vpImageConvert::convert(Ig, imageMat);
545 filename = vpIoTools::createFilePath(opath, "Klimt_ipl_grey_cvMat.pgm");
546 /* Save the the current image */
547
548 std::cout << " Resulting image saved in: " << filename << std::endl;
549 if (!cv::imwrite(filename, imageMat)) {
550 std::cout << " Cannot write image: " << filename << std::endl;
551 return (-1);
552 }
553 std::cout << " Convert result in " << filename << std::endl;
554 chrono.stop();
555 std::cout << "== Conversion c++ interface : " << chrono.getDurationMs() << " ms" << std::endl;
556#endif
557#endif
558
560 // Split a vpImage<vpRGBa> to vpImage<unsigned char>
562 std::cout << "** Split a vpImage<vpRGBa> to vpImage<unsigned char>" << std::endl;
563 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
564
565 /* Read the color image */
566
567 // Load a color image from the disk
568 std::cout << " Load " << filename << std::endl;
569 vpImageIo::read(Ic, filename);
570 vpImage<unsigned char> R, G, B, a;
571 vpImageConvert::split(Ic, &R, NULL, &B);
572 chrono.start();
573 for (int iteration = 0; iteration < nbIterations; iteration++) {
574 vpImageConvert::split(Ic, &R, NULL, &B);
575 }
576 chrono.stop();
577
578 std::cout << " Time for " << nbIterations << " split (ms): " << chrono.getDurationMs() << std::endl;
579
580 filename = vpIoTools::createFilePath(opath, "Klimt_RChannel.pgm");
581 /* Save the the current image */
582 std::cout << " Save Klimt R channel: " << filename << std::endl;
583 vpImageIo::write(R, filename);
584
585 filename = vpIoTools::createFilePath(opath, "Klimt_BChannel.pgm");
586 /* Save the the current image */
587 std::cout << " Save Klimt B channel: " << filename << std::endl;
588 vpImageIo::write(B, filename);
589
591 // Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>
593 std::cout << "** Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>" << std::endl;
594 vpImageConvert::split(Ic, &R, &G, &B, &a);
595 chrono.start();
596 vpImage<vpRGBa> I_merge;
597 for (int iteration = 0; iteration < nbIterations; iteration++) {
598 vpImageConvert::merge(&R, &G, &B, &a, I_merge);
599 }
600 chrono.stop();
601
602 std::cout << " Time for 1000 merge (ms): " << chrono.getDurationMs() << std::endl;
603
604 filename = vpIoTools::createFilePath(opath, "Klimt_merge.ppm");
605 std::cout << " Resulting image saved in: " << filename << std::endl;
606 vpImageIo::write(I_merge, filename);
607
609 // Convert a vpImage<vpRGBa> in RGB color space to a vpImage<vpRGBa> in
610 // HSV color
612 std::cout << "** Convert a vpImage<vpRGBa> in RGB color space to a "
613 "vpImage<vpRGBa> in HSV color"
614 << std::endl;
615 unsigned int size = Ic.getSize();
616 unsigned int w = Ic.getWidth(), h = Ic.getHeight();
617 std::vector<unsigned char> hue(size);
618 std::vector<unsigned char> saturation(size);
619 std::vector<unsigned char> value(size);
620
621 vpImageConvert::RGBaToHSV((unsigned char *)Ic.bitmap, &hue.front(), &saturation.front(), &value.front(), size);
622 vpImage<unsigned char> I_hue(&hue.front(), h, w);
623 vpImage<unsigned char> I_saturation(&saturation.front(), h, w);
624 vpImage<unsigned char> I_value(&value.front(), h, w);
625 vpImage<vpRGBa> I_HSV;
626 vpImageConvert::merge(&I_hue, &I_saturation, &I_value, NULL, I_HSV);
627
628 filename = vpIoTools::createFilePath(opath, "Klimt_HSV.ppm");
629 std::cout << " Resulting image saved in: " << filename << std::endl;
630 vpImageIo::write(I_HSV, filename);
631
632 // Check the conversion RGBa <==> HSV
633 std::vector<double> hue2(size);
634 std::vector<double> saturation2(size);
635 std::vector<double> value2(size);
636 vpImageConvert::RGBaToHSV((unsigned char *)Ic.bitmap, &hue2.front(), &saturation2.front(), &value2.front(), size);
637
638 std::vector<unsigned char> rgba(size * 4);
639 vpImageConvert::HSVToRGBa(&hue2.front(), &saturation2.front(), &value2.front(), &rgba.front(), size);
640
641 vpImage<vpRGBa> I_HSV2RGBa(reinterpret_cast<vpRGBa *>(&rgba.front()), h, w);
642 filename = vpIoTools::createFilePath(opath, "Klimt_HSV2RGBa.ppm");
643 std::cout << " Resulting image saved in: " << filename << std::endl;
644 vpImageIo::write(I_HSV2RGBa, filename);
645
646 for (unsigned int i = 0; i < Ic.getHeight(); i++) {
647 for (unsigned int j = 0; j < Ic.getWidth(); j++) {
648 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) {
649 std::cerr << "Ic[i][j].R=" << static_cast<unsigned>(Ic[i][j].R)
650 << " ; I_HSV2RGBa[i][j].R=" << static_cast<unsigned>(I_HSV2RGBa[i][j].R) << std::endl;
651 std::cerr << "Ic[i][j].G=" << static_cast<unsigned>(Ic[i][j].G)
652 << " ; I_HSV2RGBa[i][j].G=" << static_cast<unsigned>(I_HSV2RGBa[i][j].G) << std::endl;
653 std::cerr << "Ic[i][j].B=" << static_cast<unsigned>(Ic[i][j].B)
654 << " ; I_HSV2RGBa[i][j].B=" << static_cast<unsigned>(I_HSV2RGBa[i][j].B) << std::endl;
655 throw vpException(vpException::fatalError, "Problem with conversion between RGB <==> HSV");
656 }
657 }
658 }
659
661 // Test construction of a vpImage from an array with copyData==true
663 std::cout << "** Construction of a vpImage from an array with copyData==true" << std::endl;
664 std::vector<unsigned char> rgba2(size*4);
665 std::fill(rgba2.begin(), rgba2.end(), 127);
666 vpImage<vpRGBa> I_copyData(reinterpret_cast<vpRGBa *>(&rgba2.front()), h, w, true);
667
668 filename = vpIoTools::createFilePath(opath, "I_copyData.ppm");
669 std::cout << " Resulting image saved in: " << filename << std::endl;
670 vpImageIo::write(I_copyData, filename);
671
672 if (I_copyData.getSize() > 0) {
673 I_copyData[0][0].R = 10;
674 }
675
676 // Test color conversion
677 {
678 std::cout << "** Test color conversion" << std::endl;
679 // RGBa to Grayscale
680 vpImage<vpRGBa> I_color;
681 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
682 vpImageIo::read(I_color, filename);
683
684 // RGB to Grayscale conversion
685 std::vector<unsigned char> rgb_array(I_color.getSize()*3);
686 vpImageConvert::RGBaToRGB((unsigned char *)I_color.bitmap, &rgb_array.front(), I_color.getSize());
687
688#if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
689 // BGR cv::Mat to Grayscale
690 std::cout << "\n BGR cv::Mat to Grayscale" << std::endl;
691 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
692 cv::Mat colorMat = cv::imread(filename);
693 std::cout << " colorMat=" << colorMat.cols << "x" << colorMat.rows << std::endl;
694
695 // Test RGB to Grayscale + Flip
696 std::cout << "\n RGB to Grayscale + Flip" << std::endl;
697 std::vector<unsigned char> rgb2gray_flip_array_sse(I_color.getSize());
698 vpImageConvert::RGBToGrey(&rgb_array.front(), &rgb2gray_flip_array_sse.front(), I_color.getWidth(), I_color.getHeight(), true);
699 vpImage<unsigned char> I_rgb2gray_flip_sse(&rgb2gray_flip_array_sse.front(), I_color.getHeight(), I_color.getWidth());
700
701 filename = vpIoTools::createFilePath(opath, "I_rgb2gray_flip_sse.pgm");
702 std::cout << " Resulting image saved in: " << filename << std::endl;
703 vpImageIo::write(I_rgb2gray_flip_sse, filename);
704
705 // Test BGR to Grayscale + Flip
706 std::cout << "\n Conversion BGR to Grayscale + Flip" << std::endl;
707 std::vector<unsigned char> bgr2gray_flip_array_sse(I_color.getSize());
708 vpImage<unsigned char> I_bgr2gray_flip_sse(&bgr2gray_flip_array_sse.front(), I_color.getHeight(), I_color.getWidth());
709 vpImageConvert::convert(colorMat, I_bgr2gray_flip_sse, true);
710
711 filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_sse.pgm");
712 std::cout << " Resulting image saved in: " << filename << std::endl;
713 vpImageIo::write(I_bgr2gray_flip_sse, filename);
714
715 // Test RGB to Grayscale + Flip + Crop
716 std::cout << "\n RGB to Grayscale + Flip + Crop" << std::endl;
717 cv::Rect rect_roi(11, 17, 347, 449);
718 cv::Mat colorMat_crop = colorMat(rect_roi);
719 cv::Mat colorMat_crop_continous = colorMat(rect_roi).clone();
720 std::cout << " colorMat_crop: " << colorMat_crop.cols << "x" << colorMat_crop.rows << " is continuous? "
721 << colorMat_crop.isContinuous() << std::endl;
722 std::cout << " colorMat_crop_continous: " << colorMat_crop_continous.cols << "x" << colorMat_crop_continous.rows
723 << " is continuous? " << colorMat_crop_continous.isContinuous() << std::endl;
724
725 vpImage<vpRGBa> I_color_crop((unsigned int)(rect_roi.height - rect_roi.y),
726 (unsigned int)(rect_roi.width - rect_roi.x));
727 for (unsigned int i = (unsigned int)rect_roi.y; i < (unsigned int)rect_roi.height; i++) {
728 for (unsigned int j = (unsigned int)rect_roi.x; j < (unsigned int)rect_roi.width; j++) {
729 I_color_crop[(unsigned int)((int)i - rect_roi.y)][(unsigned int)((int)j - rect_roi.x)] = I_color[i][j];
730 }
731 }
732 filename = vpIoTools::createFilePath(opath, "I_color_crop.ppm");
733 std::cout << " Resulting image saved in: " << filename << std::endl;
734 vpImageIo::write(I_color_crop, filename);
735
736 std::vector<unsigned char> rgb_array_crop(I_color_crop.getSize() * 3);
737 vpImageConvert::RGBaToRGB((unsigned char *)I_color_crop.bitmap, &rgb_array_crop.front(), I_color_crop.getSize());
738
739 std::vector<unsigned char> rgb2gray_flip_crop_array_sse(I_color_crop.getSize());
740 vpImageConvert::RGBToGrey(&rgb_array_crop.front(), &rgb2gray_flip_crop_array_sse.front(), I_color_crop.getWidth(),
741 I_color_crop.getHeight(), true);
742 vpImage<unsigned char> I_rgb2gray_flip_crop_sse(&rgb2gray_flip_crop_array_sse.front(), I_color_crop.getHeight(),
743 I_color_crop.getWidth());
744
745 filename = vpIoTools::createFilePath(opath, "I_rgb2gray_flip_crop_sse.pgm");
746 std::cout << " Resulting image saved in: " << filename << std::endl;
747 vpImageIo::write(I_rgb2gray_flip_crop_sse, filename);
748
749 // Test BGR to Grayscale + Flip + Crop
750 std::cout << "\n BGR to Grayscale + Flip + Crop" << std::endl;
751 vpImage<unsigned char> I_bgr2gray_flip_crop_sse(I_color_crop.getHeight(), I_color_crop.getWidth());
752 vpImageConvert::convert(colorMat_crop_continous, I_bgr2gray_flip_crop_sse, true);
753
754 filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_crop_sse.pgm");
755 std::cout << " Resulting image saved in: " << filename << std::endl;
756 vpImageIo::write(I_bgr2gray_flip_crop_sse, filename);
757
758 // Test BGR to Grayscale + Flip + Crop + No continuous Mat
759 std::cout << "\n BGR to Grayscale + Flip + Crop + No continuous Mat" << std::endl;
760 vpImage<unsigned char> I_bgr2gray_flip_crop_no_continuous_sse(I_color_crop.getHeight(), I_color_crop.getWidth());
761 vpImageConvert::convert(colorMat_crop, I_bgr2gray_flip_crop_no_continuous_sse, true);
762
763 filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_crop_no_continuous_sse.pgm");
764 std::cout << " Resulting image saved in: " << filename << std::endl;
765 vpImageIo::write(I_bgr2gray_flip_crop_no_continuous_sse, filename);
766#endif
767 std::cout << "Test succeed" << std::endl;
768 }
769
770 return EXIT_SUCCESS;
771 } catch (const vpException &e) {
772 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
773 return EXIT_FAILURE;
774 }
775}
void start(bool reset=true)
Definition: vpTime.cpp:409
void stop()
Definition: vpTime.cpp:424
double getDurationMs()
Definition: vpTime.cpp:392
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ fatalError
Fatal error.
Definition: vpException.h:96
const char * getMessage() const
Definition: vpException.cpp:90
static void HSVToRGBa(const double *hue, const double *saturation, const double *value, unsigned char *rgba, unsigned int size)
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=NULL)
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 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)
Definition: vpImageIo.cpp:149
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:293
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getSize() const
Definition: vpImage.h:227
Type * bitmap
points toward the bitmap
Definition: vpImage.h:143
unsigned int getHeight() const
Definition: vpImage.h:188
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1365
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:420
static std::string getUserName()
Definition: vpIoTools.cpp:316
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1670
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:570
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Definition: vpRGBa.h:67
VISP_EXPORT double measureTimeMs()