Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testConversion.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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*****************************************************************************/
35
36#include <iomanip>
37#include <stdlib.h>
38
39#include <visp3/core/vpConfig.h>
40#include <visp3/core/vpDebug.h>
41#include <visp3/core/vpImage.h>
42#include <visp3/core/vpImageConvert.h>
43#include <visp3/core/vpIoTools.h>
44#include <visp3/core/vpTime.h>
45#include <visp3/io/vpImageIo.h>
46#include <visp3/io/vpParseArgv.h>
47
54// List of allowed command line options
55#define GETOPTARGS "cdi:o:n:h"
56
57/*
58 Print the program options.
59
60 \param name : Program name.
61 \param badparam : Bad parameter name.
62 \param ipath: Input image path.
63 \param opath : Output image path.
64 \param user : Username.
65 \param nbiter : Iteration number.
66
67 */
68void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user, int nbiter)
69{
70 fprintf(stdout, "\n\
71Test image conversions.\n\
72\n\
73SYNOPSIS\n\
74 %s [-i <input image path>] [-o <output image path>] [-n <nb benchmark iterations>]\n\
75 [-h]\n\
76",
77 name);
78
79 fprintf(stdout, "\n\
80OPTIONS: Default\n\
81 -i <input image path> %s\n\
82 Set image input path.\n\
83 From this path read \"Klimt/Klimt.pgm\"\n\
84 and \"Klimt/Klimt.ppm\" images.\n\
85 Setting the VISP_INPUT_IMAGE_PATH environment\n\
86 variable produces the same behaviour than using\n\
87 this option.\n\
88\n\
89 -o <output image path> %s\n\
90 Set image output path.\n\
91 From this directory, creates the \"%s\"\n\
92 subdirectory depending on the username, where \n\
93 Klimt_grey.pgm and Klimt_color.ppm output images\n\
94 are written.\n\
95\n\
96 -n <nb benchmark iterations> %d\n\
97 Set the number of benchmark iterations.\n\
98\n\
99 -h\n\
100 Print the help.\n\n",
101 ipath.c_str(), opath.c_str(), user.c_str(), nbiter);
102
103 if (badparam)
104 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
105}
106
120bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user,
121 int &nbIterations)
122{
123 const char *optarg_;
124 int c;
125 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
126
127 switch (c) {
128 case 'i':
129 ipath = optarg_;
130 break;
131 case 'o':
132 opath = optarg_;
133 break;
134 case 'n':
135 nbIterations = atoi(optarg_);
136 break;
137 case 'h':
138 usage(argv[0], NULL, ipath, opath, user, nbIterations);
139 return false;
140
141 case 'c':
142 case 'd':
143 break;
144
145 default:
146 usage(argv[0], optarg_, ipath, opath, user, nbIterations);
147 return false;
148 }
149 }
150
151 if ((c == 1) || (c == -1)) {
152 // standalone param or error
153 usage(argv[0], NULL, ipath, opath, user, nbIterations);
154 std::cerr << "ERROR: " << std::endl;
155 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
156 return false;
157 }
158
159 return true;
160}
161
162int main(int argc, const char **argv)
163{
164 try {
165 std::string env_ipath;
166 std::string opt_ipath;
167 std::string opt_opath;
168 std::string ipath;
169 std::string opath;
170 std::string filename;
171 std::string username;
172 int nbIterations = 1;
173
174 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
175 // environment variable value
177
178 // Set the default input path
179 if (!env_ipath.empty())
180 ipath = env_ipath;
181
182// Set the default output path
183#if defined(_WIN32)
184 opt_opath = "C:/temp";
185#else
186 opt_opath = "/tmp";
187#endif
188
189 // Get the user login name
190 vpIoTools::getUserName(username);
191
192 // Read the command line options
193 if (getOptions(argc, argv, opt_ipath, opt_opath, username, nbIterations) == false) {
194 return EXIT_FAILURE;
195 }
196
197 // Get the option values
198 if (!opt_ipath.empty())
199 ipath = opt_ipath;
200 if (!opt_opath.empty())
201 opath = opt_opath;
202
203 // Append to the output path string, the login name of the user
204 opath = vpIoTools::createFilePath(opath, username);
205
206 // Test if the output path exist. If no try to create it
207 if (vpIoTools::checkDirectory(opath) == false) {
208 try {
209 // Create the dirname
211 } catch (...) {
212 usage(argv[0], NULL, ipath, opt_opath, username, nbIterations);
213 std::cerr << std::endl << "ERROR:" << std::endl;
214 std::cerr << " Cannot create " << opath << std::endl;
215 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
216 return EXIT_FAILURE;
217 }
218 }
219
220 // Compare ipath and env_ipath. If they differ, we take into account
221 // the input path comming from the command line option
222 if (opt_ipath.empty()) {
223 if (ipath != env_ipath) {
224 std::cout << std::endl << "WARNING: " << std::endl;
225 std::cout << " Since -i <visp image path=" << ipath << "> "
226 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
227 << " we skip the environment variable." << std::endl;
228 }
229 }
230
231 // Test if an input path is set
232 if (opt_ipath.empty() && env_ipath.empty()) {
233 usage(argv[0], NULL, ipath, opt_opath, username, nbIterations);
234 std::cerr << std::endl << "ERROR:" << std::endl;
235 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
236 << " environment variable to specify the location of the " << std::endl
237 << " image path where test images are located." << std::endl
238 << std::endl;
239 return EXIT_FAILURE;
240 }
241
242 //
243 // Here starts really the test
244 //
245
246 vpImage<unsigned char> Ig; // Grey image
247 vpImage<vpRGBa> Ic; // Color image
248
249 //-------------------- .pgm -> .ppm
250 std::cout << "** Convert a grey image (.pgm) to a color image (.ppm)" << std::endl;
251 // Load a grey image from the disk
252 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
253 std::cout << " Load " << filename << std::endl;
254 vpImageIo::read(Ig, filename);
255 // Create a color image from the grey
257 filename = vpIoTools::createFilePath(opath, "Klimt_color.ppm");
258 std::cout << " Resulting image saved in: " << filename << std::endl;
259 vpImageIo::write(Ic, filename);
260
261 //-------------------- .ppm -> .pgm
262 std::cout << "** Convert a color image (.ppm) to a grey image (.pgm)" << std::endl;
263 // Load a color image from the disk
264 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
265 std::cout << " Load " << filename << std::endl;
266 vpImageIo::read(Ic, filename);
267 // Create a grey image from the color
269 filename = vpIoTools::createFilePath(opath, "Klimt_grey.pgm");
270 std::cout << " Resulting image saved in: " << filename << std::endl;
271 vpImageIo::write(Ig, filename);
272
273 //-------------------- YUV -> RGB
274 std::cout << "** Convert YUV pixel value to a RGB value" << std::endl;
275 unsigned char y = 187, u = 10, v = 30;
276 unsigned char r, g, b;
277
278 // Convert a YUV pixel value to a RGB value
279 vpImageConvert::YUVToRGB(y, u, v, r, g, b);
280 std::cout << " y(" << (int)y << ") u(" << (int)u << ") v(" << (int)v << ") = r(" << (int)r << ") g(" << (int)g
281 << ") b(" << (int)b << ")" << std::endl;
282
283 vpChrono chrono;
284
285 /* ------------------------------------------------------------------------ */
286 /* conversion for the new c++ interface */
287 /* ------------------------------------------------------------------------ */
288
289#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
290 chrono.start();
292 // Convert a cv::Mat to a vpImage<vpRGBa>
294 std::cout << "** Convert a cv::Mat to a vpImage<vpRGBa>" << std::endl;
295 cv::Mat imageMat;
296 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
297 std::cout << " Reading the color image with c++ interface of opencv: " << filename << std::endl;
298#if VISP_HAVE_OPENCV_VERSION >= 0x030000
299 int flags = cv::IMREAD_COLOR;
300#else
301 int flags = CV_LOAD_IMAGE_COLOR;
302#endif
303 imageMat = cv::imread(filename, flags); // Force to a three channel BGR color image.
304 if (imageMat.data == NULL) {
305 std::cout << " Cannot read image: " << filename << std::endl;
306 return EXIT_FAILURE;
307 }
308 vpImageConvert::convert(imageMat, Ic);
309 filename = vpIoTools::createFilePath(opath, "Klimt_color_cvMat.ppm");
310 std::cout << " Resulting image saved in: " << filename << std::endl;
311 vpImageIo::write(Ic, filename);
312
313 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
314 /* Read the pgm image */
315
316 std::cout << " Reading the grayscale image with opencv: " << filename << std::endl;
317#if VISP_HAVE_OPENCV_VERSION >= 0x030000
318 flags = cv::IMREAD_GRAYSCALE;
319#else
320 flags = CV_LOAD_IMAGE_GRAYSCALE;
321#endif
322 imageMat = cv::imread(filename, flags); // Forced to grayscale.
323 if (imageMat.data == NULL) {
324 std::cout << " Cannot read image: " << filename << std::endl;
325 return EXIT_FAILURE;
326 }
327 vpImageConvert::convert(imageMat, Ic);
328 filename = vpIoTools::createFilePath(opath, "Klimt_grey_cvMat.ppm");
329 std::cout << " Resulting image saved in: " << filename << std::endl;
330 vpImageIo::write(Ic, filename);
331
333 // Convert a cv::Mat to a vpImage<unsigned char>
335 std::cout << "** Convert a cv::Mat to a vpImage<nsigned char>" << std::endl;
336 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
337
338 /* Read the color image */
339
340 std::cout << " Reading the color image with opencv: " << filename << std::endl;
341#if VISP_HAVE_OPENCV_VERSION >= 0x030000
342 flags = cv::IMREAD_COLOR;
343#else
344 flags = CV_LOAD_IMAGE_COLOR;
345#endif
346 imageMat = cv::imread(filename, flags); // Force to a three channel BGR color image.
347 if (imageMat.data == NULL) {
348 std::cout << " Cannot read image: " << filename << std::endl;
349 return EXIT_FAILURE;
350 }
351 vpImageConvert::convert(imageMat, Ig);
352 filename = vpIoTools::createFilePath(opath, "Klimt_color_cvMat.pgm");
353 std::cout << " Resulting image saved in: " << filename << std::endl;
354 vpImageIo::write(Ig, filename);
355
356 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
357
358 /* Read the pgm image */
359
360 std::cout << " Reading the greyscale image with opencv: " << filename << std::endl;
361#if VISP_HAVE_OPENCV_VERSION >= 0x030000
362 flags = cv::IMREAD_GRAYSCALE;
363#else
364 flags = CV_LOAD_IMAGE_GRAYSCALE;
365#endif
366 imageMat = cv::imread(filename, flags);
367 if (imageMat.data == NULL) {
368 std::cout << " Cannot read image: " << filename << std::endl;
369 return EXIT_FAILURE;
370 }
371 vpImageConvert::convert(imageMat, Ig);
372 filename = vpIoTools::createFilePath(opath, "Klimt_grey_cvMat.pgm");
373 std::cout << " Resulting image saved in: " << filename << std::endl;
374 vpImageIo::write(Ig, filename);
375
376 std::cout << " Convert result in " << filename << std::endl;
377
379 // Convert a vpImage<vpRGBa> to a cv::Mat
381 std::cout << "** Convert a vpImage<vpRGBa> to a cv::Mat" << std::endl;
382 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
383
384 /* Read the color image */
385
386 // Load a color image from the disk
387 std::cout << " Load " << filename << std::endl;
388 vpImageIo::read(Ic, filename);
389 vpImageConvert::convert(Ic, imageMat);
390 filename = vpIoTools::createFilePath(opath, "Klimt_ipl_color_cvMat.ppm");
391 /* Save the the current image */
392 std::cout << " Resulting image saved in: " << filename << std::endl;
393 if (!cv::imwrite(filename, imageMat)) {
394 std::cout << " Cannot write image: " << filename << std::endl;
395 return EXIT_FAILURE;
396 }
397 std::cout << " Convert result in " << filename << std::endl;
398
400 // Convert a vpImage<unsigned char> to a cv::Mat
402 std::cout << "** Convert a vpImage<unsigned char> to a cv::Mat" << std::endl;
403 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
404
405 /* Read the grey image */
406
407 // Load a color image from the disk
408 std::cout << " Load " << filename << std::endl;
409 vpImageIo::read(Ig, filename);
410 vpImageConvert::convert(Ig, imageMat);
411 filename = vpIoTools::createFilePath(opath, "Klimt_ipl_grey_cvMat.pgm");
412 /* Save the the current image */
413
414 std::cout << " Resulting image saved in: " << filename << std::endl;
415 if (!cv::imwrite(filename, imageMat)) {
416 std::cout << " Cannot write image: " << filename << std::endl;
417 return EXIT_FAILURE;
418 }
419 std::cout << " Convert result in " << filename << std::endl;
420 chrono.stop();
421 std::cout << "== Conversion c++ interface : " << chrono.getDurationMs() << " ms" << std::endl;
422#endif
423
425 // Split a vpImage<vpRGBa> to vpImage<unsigned char>
427 std::cout << "** Split a vpImage<vpRGBa> to vpImage<unsigned char>" << std::endl;
428 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
429
430 /* Read the color image */
431
432 // Load a color image from the disk
433 std::cout << " Load " << filename << std::endl;
434 vpImageIo::read(Ic, filename);
435 vpImage<unsigned char> R, G, B, a;
436 vpImageConvert::split(Ic, &R, NULL, &B);
437 chrono.start();
438 for (int iteration = 0; iteration < nbIterations; iteration++) {
439 vpImageConvert::split(Ic, &R, NULL, &B);
440 }
441 chrono.stop();
442
443 std::cout << " Time for " << nbIterations << " split (ms): " << chrono.getDurationMs() << std::endl;
444
445 filename = vpIoTools::createFilePath(opath, "Klimt_RChannel.pgm");
446 /* Save the the current image */
447 std::cout << " Save Klimt R channel: " << filename << std::endl;
448 vpImageIo::write(R, filename);
449
450 filename = vpIoTools::createFilePath(opath, "Klimt_BChannel.pgm");
451 /* Save the the current image */
452 std::cout << " Save Klimt B channel: " << filename << std::endl;
453 vpImageIo::write(B, filename);
454
456 // Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>
458 std::cout << "** Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>" << std::endl;
459 vpImageConvert::split(Ic, &R, &G, &B, &a);
460 chrono.start();
461 vpImage<vpRGBa> I_merge;
462 for (int iteration = 0; iteration < nbIterations; iteration++) {
463 vpImageConvert::merge(&R, &G, &B, &a, I_merge);
464 }
465 chrono.stop();
466
467 std::cout << " Time for 1000 merge (ms): " << chrono.getDurationMs() << std::endl;
468
469 filename = vpIoTools::createFilePath(opath, "Klimt_merge.ppm");
470 std::cout << " Resulting image saved in: " << filename << std::endl;
471 vpImageIo::write(I_merge, filename);
472
474 // Convert a vpImage<vpRGBa> in RGB color space to a vpImage<vpRGBa> in
475 // HSV color
477 std::cout << "** Convert a vpImage<vpRGBa> in RGB color space to a "
478 "vpImage<vpRGBa> in HSV color"
479 << std::endl;
480 unsigned int size = Ic.getSize();
481 unsigned int w = Ic.getWidth(), h = Ic.getHeight();
482 std::vector<unsigned char> hue(size);
483 std::vector<unsigned char> saturation(size);
484 std::vector<unsigned char> value(size);
485
486 vpImageConvert::RGBaToHSV((unsigned char *)Ic.bitmap, &hue.front(), &saturation.front(), &value.front(), size);
487 vpImage<unsigned char> I_hue(&hue.front(), h, w);
488 vpImage<unsigned char> I_saturation(&saturation.front(), h, w);
489 vpImage<unsigned char> I_value(&value.front(), h, w);
490 vpImage<vpRGBa> I_HSV;
491 vpImageConvert::merge(&I_hue, &I_saturation, &I_value, NULL, I_HSV);
492
493 filename = vpIoTools::createFilePath(opath, "Klimt_HSV.ppm");
494 std::cout << " Resulting image saved in: " << filename << std::endl;
495 vpImageIo::write(I_HSV, filename);
496
497 // Check the conversion RGBa <==> HSV
498 std::vector<double> hue2(size);
499 std::vector<double> saturation2(size);
500 std::vector<double> value2(size);
501 vpImageConvert::RGBaToHSV((unsigned char *)Ic.bitmap, &hue2.front(), &saturation2.front(), &value2.front(), size);
502
503 std::vector<unsigned char> rgba(size * 4);
504 vpImageConvert::HSVToRGBa(&hue2.front(), &saturation2.front(), &value2.front(), &rgba.front(), size);
505
506 vpImage<vpRGBa> I_HSV2RGBa(reinterpret_cast<vpRGBa *>(&rgba.front()), h, w);
507 filename = vpIoTools::createFilePath(opath, "Klimt_HSV2RGBa.ppm");
508 std::cout << " Resulting image saved in: " << filename << std::endl;
509 vpImageIo::write(I_HSV2RGBa, filename);
510
511 for (unsigned int i = 0; i < Ic.getHeight(); i++) {
512 for (unsigned int j = 0; j < Ic.getWidth(); j++) {
513 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) {
514 std::cerr << "Ic[i][j].R=" << static_cast<unsigned>(Ic[i][j].R)
515 << " ; I_HSV2RGBa[i][j].R=" << static_cast<unsigned>(I_HSV2RGBa[i][j].R) << std::endl;
516 std::cerr << "Ic[i][j].G=" << static_cast<unsigned>(Ic[i][j].G)
517 << " ; I_HSV2RGBa[i][j].G=" << static_cast<unsigned>(I_HSV2RGBa[i][j].G) << std::endl;
518 std::cerr << "Ic[i][j].B=" << static_cast<unsigned>(Ic[i][j].B)
519 << " ; I_HSV2RGBa[i][j].B=" << static_cast<unsigned>(I_HSV2RGBa[i][j].B) << std::endl;
520 throw vpException(vpException::fatalError, "Problem with conversion between RGB <==> HSV");
521 }
522 }
523 }
524
526 // Test construction of a vpImage from an array with copyData==true
528 std::cout << "** Construction of a vpImage from an array with copyData==true" << std::endl;
529 std::vector<unsigned char> rgba2(size * 4);
530 std::fill(rgba2.begin(), rgba2.end(), 127);
531 vpImage<vpRGBa> I_copyData(reinterpret_cast<vpRGBa *>(&rgba2.front()), h, w, true);
532
533 filename = vpIoTools::createFilePath(opath, "I_copyData.ppm");
534 std::cout << " Resulting image saved in: " << filename << std::endl;
535 vpImageIo::write(I_copyData, filename);
536
537 if (I_copyData.getSize() > 0) {
538 I_copyData[0][0].R = 10;
539 }
540
541 // Test color conversion
542 {
543 std::cout << "** Test color conversion" << std::endl;
544 // RGBa to Grayscale
545 vpImage<vpRGBa> I_color;
546 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
547 vpImageIo::read(I_color, filename);
548
549 // RGB to Grayscale conversion
550 std::vector<unsigned char> rgb_array(I_color.getSize() * 3);
551 vpImageConvert::RGBaToRGB((unsigned char *)I_color.bitmap, &rgb_array.front(), I_color.getSize());
552
553#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
554 // BGR cv::Mat to Grayscale
555 std::cout << "\n BGR cv::Mat to Grayscale" << std::endl;
556 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
557 cv::Mat colorMat = cv::imread(filename);
558 std::cout << " colorMat=" << colorMat.cols << "x" << colorMat.rows << std::endl;
559
560 // Test RGB to Grayscale + Flip
561 std::cout << "\n RGB to Grayscale + Flip" << std::endl;
562 std::vector<unsigned char> rgb2gray_flip_array_sse(I_color.getSize());
563 vpImageConvert::RGBToGrey(&rgb_array.front(), &rgb2gray_flip_array_sse.front(), I_color.getWidth(),
564 I_color.getHeight(), true);
565 vpImage<unsigned char> I_rgb2gray_flip_sse(&rgb2gray_flip_array_sse.front(), I_color.getHeight(),
566 I_color.getWidth());
567
568 filename = vpIoTools::createFilePath(opath, "I_rgb2gray_flip_sse.pgm");
569 std::cout << " Resulting image saved in: " << filename << std::endl;
570 vpImageIo::write(I_rgb2gray_flip_sse, filename);
571
572 // Test BGR to Grayscale + Flip
573 std::cout << "\n Conversion BGR to Grayscale + Flip" << std::endl;
574 std::vector<unsigned char> bgr2gray_flip_array_sse(I_color.getSize());
575 vpImage<unsigned char> I_bgr2gray_flip_sse(&bgr2gray_flip_array_sse.front(), I_color.getHeight(),
576 I_color.getWidth());
577 vpImageConvert::convert(colorMat, I_bgr2gray_flip_sse, true);
578
579 filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_sse.pgm");
580 std::cout << " Resulting image saved in: " << filename << std::endl;
581 vpImageIo::write(I_bgr2gray_flip_sse, filename);
582
583 // Test RGB to Grayscale + Flip + Crop
584 std::cout << "\n RGB to Grayscale + Flip + Crop" << std::endl;
585 cv::Rect rect_roi(11, 17, 347, 449);
586 cv::Mat colorMat_crop = colorMat(rect_roi);
587 cv::Mat colorMat_crop_continuous = colorMat(rect_roi).clone();
588 std::cout << " colorMat_crop: " << colorMat_crop.cols << "x" << colorMat_crop.rows << " is continuous? "
589 << colorMat_crop.isContinuous() << std::endl;
590 std::cout << " colorMat_crop_continuous: " << colorMat_crop_continuous.cols << "x" << colorMat_crop_continuous.rows
591 << " is continuous? " << colorMat_crop_continuous.isContinuous() << std::endl;
592
593 vpImage<vpRGBa> I_color_crop((unsigned int)(rect_roi.height - rect_roi.y),
594 (unsigned int)(rect_roi.width - rect_roi.x));
595 for (unsigned int i = (unsigned int)rect_roi.y; i < (unsigned int)rect_roi.height; i++) {
596 for (unsigned int j = (unsigned int)rect_roi.x; j < (unsigned int)rect_roi.width; j++) {
597 I_color_crop[(unsigned int)((int)i - rect_roi.y)][(unsigned int)((int)j - rect_roi.x)] = I_color[i][j];
598 }
599 }
600 filename = vpIoTools::createFilePath(opath, "I_color_crop.ppm");
601 std::cout << " Resulting image saved in: " << filename << std::endl;
602 vpImageIo::write(I_color_crop, filename);
603
604 std::vector<unsigned char> rgb_array_crop(I_color_crop.getSize() * 3);
605 vpImageConvert::RGBaToRGB((unsigned char *)I_color_crop.bitmap, &rgb_array_crop.front(), I_color_crop.getSize());
606
607 std::vector<unsigned char> rgb2gray_flip_crop_array_sse(I_color_crop.getSize());
608 vpImageConvert::RGBToGrey(&rgb_array_crop.front(), &rgb2gray_flip_crop_array_sse.front(), I_color_crop.getWidth(),
609 I_color_crop.getHeight(), true);
610 vpImage<unsigned char> I_rgb2gray_flip_crop_sse(&rgb2gray_flip_crop_array_sse.front(), I_color_crop.getHeight(),
611 I_color_crop.getWidth());
612
613 filename = vpIoTools::createFilePath(opath, "I_rgb2gray_flip_crop_sse.pgm");
614 std::cout << " Resulting image saved in: " << filename << std::endl;
615 vpImageIo::write(I_rgb2gray_flip_crop_sse, filename);
616
617 // Test BGR to Grayscale + Flip + Crop
618 std::cout << "\n BGR to Grayscale + Flip + Crop" << std::endl;
619 vpImage<unsigned char> I_bgr2gray_flip_crop_sse(I_color_crop.getHeight(), I_color_crop.getWidth());
620 vpImageConvert::convert(colorMat_crop_continuous, I_bgr2gray_flip_crop_sse, true);
621
622 filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_crop_sse.pgm");
623 std::cout << " Resulting image saved in: " << filename << std::endl;
624 vpImageIo::write(I_bgr2gray_flip_crop_sse, filename);
625
626 // Test BGR to Grayscale + Flip + Crop + No continuous Mat
627 std::cout << "\n BGR to Grayscale + Flip + Crop + No continuous Mat" << std::endl;
628 vpImage<unsigned char> I_bgr2gray_flip_crop_no_continuous_sse(I_color_crop.getHeight(), I_color_crop.getWidth());
629 vpImageConvert::convert(colorMat_crop, I_bgr2gray_flip_crop_no_continuous_sse, true);
630
631 filename = vpIoTools::createFilePath(opath, "I_bgr2gray_flip_crop_no_continuous_sse.pgm");
632 std::cout << " Resulting image saved in: " << filename << std::endl;
633 vpImageIo::write(I_bgr2gray_flip_crop_no_continuous_sse, filename);
634#endif
635 std::cout << "Test succeed" << std::endl;
636 }
637
638 return EXIT_SUCCESS;
639 } catch (const vpException &e) {
640 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
641 return EXIT_FAILURE;
642 }
643}
void start(bool reset=true)
Definition vpTime.cpp:397
void stop()
Definition vpTime.cpp:412
double getDurationMs()
Definition vpTime.cpp:386
error that can be emitted by ViSP classes.
Definition vpException.h:59
@ fatalError
Fatal error.
Definition vpException.h:84
const char * getMessage() const
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)
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:135
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getSize() const
Definition vpImage.h:223
Type * bitmap
points toward the bitmap
Definition vpImage.h:139
unsigned int getHeight() const
Definition vpImage.h:184
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 parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)