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>
55#define GETOPTARGS "cdi:o:n:h"
68void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user,
int nbiter)
71Test image conversions.\n\
74 %s [-i <input image path>] [-o <output image path>] [-n <nb benchmark iterations>]\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\
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\
96 -n <nb benchmark iterations> %d\n\
97 Set the number of benchmark iterations.\n\
100 Print the help.\n\n",
101 ipath.c_str(), opath.c_str(), user.c_str(), nbiter);
104 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
120bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath,
const std::string &user,
135 nbIterations = atoi(optarg_);
138 usage(argv[0], NULL, ipath, opath, user, nbIterations);
146 usage(argv[0], optarg_, ipath, opath, user, nbIterations);
151 if ((c == 1) || (c == -1)) {
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;
162int main(
int argc,
const char **argv)
165 std::string env_ipath;
166 std::string opt_ipath;
167 std::string opt_opath;
170 std::string filename;
171 std::string username;
172 int nbIterations = 1;
179 if (!env_ipath.empty())
184 opt_opath =
"C:/temp";
193 if (getOptions(argc, argv, opt_ipath, opt_opath, username, nbIterations) ==
false) {
198 if (!opt_ipath.empty())
200 if (!opt_opath.empty())
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;
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;
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
250 std::cout <<
"** Convert a grey image (.pgm) to a color image (.ppm)" << std::endl;
253 std::cout <<
" Load " << filename << std::endl;
258 std::cout <<
" Resulting image saved in: " << filename << std::endl;
262 std::cout <<
"** Convert a color image (.ppm) to a grey image (.pgm)" << std::endl;
265 std::cout <<
" Load " << filename << std::endl;
270 std::cout <<
" Resulting image saved in: " << filename << std::endl;
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;
280 std::cout <<
" y(" << (int)y <<
") u(" << (
int)u <<
") v(" << (int)v <<
") = r(" << (
int)r <<
") g(" << (int)g
281 <<
") b(" << (
int)b <<
")" << std::endl;
289#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
294 std::cout <<
"** Convert a cv::Mat to a vpImage<vpRGBa>" << std::endl;
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;
301 int flags = CV_LOAD_IMAGE_COLOR;
303 imageMat = cv::imread(filename, flags);
304 if (imageMat.data == NULL) {
305 std::cout <<
" Cannot read image: " << filename << std::endl;
310 std::cout <<
" Resulting image saved in: " << filename << std::endl;
316 std::cout <<
" Reading the grayscale image with opencv: " << filename << std::endl;
317#if VISP_HAVE_OPENCV_VERSION >= 0x030000
318 flags = cv::IMREAD_GRAYSCALE;
320 flags = CV_LOAD_IMAGE_GRAYSCALE;
322 imageMat = cv::imread(filename, flags);
323 if (imageMat.data == NULL) {
324 std::cout <<
" Cannot read image: " << filename << std::endl;
329 std::cout <<
" Resulting image saved in: " << filename << std::endl;
335 std::cout <<
"** Convert a cv::Mat to a vpImage<nsigned char>" << std::endl;
340 std::cout <<
" Reading the color image with opencv: " << filename << std::endl;
341#if VISP_HAVE_OPENCV_VERSION >= 0x030000
342 flags = cv::IMREAD_COLOR;
344 flags = CV_LOAD_IMAGE_COLOR;
346 imageMat = cv::imread(filename, flags);
347 if (imageMat.data == NULL) {
348 std::cout <<
" Cannot read image: " << filename << std::endl;
353 std::cout <<
" Resulting image saved in: " << filename << std::endl;
360 std::cout <<
" Reading the greyscale image with opencv: " << filename << std::endl;
361#if VISP_HAVE_OPENCV_VERSION >= 0x030000
362 flags = cv::IMREAD_GRAYSCALE;
364 flags = CV_LOAD_IMAGE_GRAYSCALE;
366 imageMat = cv::imread(filename, flags);
367 if (imageMat.data == NULL) {
368 std::cout <<
" Cannot read image: " << filename << std::endl;
373 std::cout <<
" Resulting image saved in: " << filename << std::endl;
376 std::cout <<
" Convert result in " << filename << std::endl;
381 std::cout <<
"** Convert a vpImage<vpRGBa> to a cv::Mat" << std::endl;
387 std::cout <<
" Load " << filename << std::endl;
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;
397 std::cout <<
" Convert result in " << filename << std::endl;
402 std::cout <<
"** Convert a vpImage<unsigned char> to a cv::Mat" << std::endl;
408 std::cout <<
" Load " << filename << std::endl;
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;
419 std::cout <<
" Convert result in " << filename << std::endl;
421 std::cout <<
"== Conversion c++ interface : " << chrono.
getDurationMs() <<
" ms" << std::endl;
427 std::cout <<
"** Split a vpImage<vpRGBa> to vpImage<unsigned char>" << std::endl;
433 std::cout <<
" Load " << filename << std::endl;
438 for (
int iteration = 0; iteration < nbIterations; iteration++) {
443 std::cout <<
" Time for " << nbIterations <<
" split (ms): " << chrono.
getDurationMs() << std::endl;
447 std::cout <<
" Save Klimt R channel: " << filename << std::endl;
452 std::cout <<
" Save Klimt B channel: " << filename << std::endl;
458 std::cout <<
"** Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>" << std::endl;
462 for (
int iteration = 0; iteration < nbIterations; iteration++) {
467 std::cout <<
" Time for 1000 merge (ms): " << chrono.
getDurationMs() << std::endl;
470 std::cout <<
" Resulting image saved in: " << filename << std::endl;
477 std::cout <<
"** Convert a vpImage<vpRGBa> in RGB color space to a "
478 "vpImage<vpRGBa> in HSV color"
480 unsigned int size = Ic.
getSize();
482 std::vector<unsigned char> hue(size);
483 std::vector<unsigned char> saturation(size);
484 std::vector<unsigned char> value(size);
494 std::cout <<
" Resulting image saved in: " << filename << std::endl;
498 std::vector<double> hue2(size);
499 std::vector<double> saturation2(size);
500 std::vector<double> value2(size);
503 std::vector<unsigned char> rgba(size * 4);
508 std::cout <<
" Resulting image saved in: " << filename << std::endl;
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;
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);
534 std::cout <<
" Resulting image saved in: " << filename << std::endl;
537 if (I_copyData.getSize() > 0) {
538 I_copyData[0][0].R = 10;
543 std::cout <<
"** Test color conversion" << std::endl;
550 std::vector<unsigned char> rgb_array(I_color.
getSize() * 3);
553#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
555 std::cout <<
"\n BGR cv::Mat to Grayscale" << std::endl;
557 cv::Mat colorMat = cv::imread(filename);
558 std::cout <<
" colorMat=" << colorMat.cols <<
"x" << colorMat.rows << std::endl;
561 std::cout <<
"\n RGB to Grayscale + Flip" << std::endl;
562 std::vector<unsigned char> rgb2gray_flip_array_sse(I_color.
getSize());
569 std::cout <<
" Resulting image saved in: " << filename << std::endl;
573 std::cout <<
"\n Conversion BGR to Grayscale + Flip" << std::endl;
574 std::vector<unsigned char> bgr2gray_flip_array_sse(I_color.
getSize());
580 std::cout <<
" Resulting image saved in: " << filename << std::endl;
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;
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];
601 std::cout <<
" Resulting image saved in: " << filename << std::endl;
604 std::vector<unsigned char> rgb_array_crop(I_color_crop.getSize() * 3);
607 std::vector<unsigned char> rgb2gray_flip_crop_array_sse(I_color_crop.getSize());
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());
614 std::cout <<
" Resulting image saved in: " << filename << std::endl;
618 std::cout <<
"\n BGR to Grayscale + Flip + Crop" << std::endl;
623 std::cout <<
" Resulting image saved in: " << filename << std::endl;
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());
632 std::cout <<
" Resulting image saved in: " << filename << std::endl;
635 std::cout <<
"Test succeed" << std::endl;
640 std::cout <<
"Catch an exception: " << e.
getMessage() << std::endl;
void start(bool reset=true)
error that can be emitted by ViSP classes.
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.
unsigned int getWidth() const
unsigned int getSize() const
Type * bitmap
points toward the bitmap
unsigned int getHeight() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)