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>
48 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_IMGPROC)
49 #include <opencv2/imgcodecs.hpp>
50 #include <opencv2/imgproc/imgproc.hpp>
60 #define GETOPTARGS "cdi:o:n:h"
73 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user,
int nbiter)
76 Test image conversions.\n\
79 %s [-i <input image path>] [-o <output image path>] [-n <nb benchmark iterations>]\n\
86 -i <input image path> %s\n\
87 Set image input path.\n\
88 From this path read \"Klimt/Klimt.pgm\"\n\
89 and \"Klimt/Klimt.ppm\" images.\n\
90 Setting the VISP_INPUT_IMAGE_PATH environment\n\
91 variable produces the same behaviour than using\n\
94 -o <output image path> %s\n\
95 Set image output path.\n\
96 From this directory, creates the \"%s\"\n\
97 subdirectory depending on the username, where \n\
98 Klimt_grey.pgm and Klimt_color.ppm output images\n\
101 -n <nb benchmark iterations> %d\n\
102 Set the number of benchmark iterations.\n\
105 Print the help.\n\n",
106 ipath.c_str(), opath.c_str(), user.c_str(), nbiter);
109 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
125 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath,
const std::string &user,
140 nbIterations = atoi(optarg_);
143 usage(argv[0],
nullptr, ipath, opath, user, nbIterations);
151 usage(argv[0], optarg_, ipath, opath, user, nbIterations);
156 if ((c == 1) || (c == -1)) {
158 usage(argv[0],
nullptr, ipath, opath, user, nbIterations);
159 std::cerr <<
"ERROR: " << std::endl;
160 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
167 int main(
int argc,
const char **argv)
170 std::string env_ipath;
171 std::string opt_ipath;
172 std::string opt_opath;
175 std::string filename;
176 std::string username;
177 int nbIterations = 1;
184 if (!env_ipath.empty())
189 opt_opath =
"C:/temp";
198 if (getOptions(argc, argv, opt_ipath, opt_opath, username, nbIterations) ==
false) {
203 if (!opt_ipath.empty())
205 if (!opt_opath.empty())
218 usage(argv[0],
nullptr, ipath, opt_opath, username, nbIterations);
219 std::cerr << std::endl <<
"ERROR:" << std::endl;
220 std::cerr <<
" Cannot create " << opath << std::endl;
221 std::cerr <<
" Check your -o " << opt_opath <<
" option " << std::endl;
228 if (opt_ipath.empty()) {
229 if (ipath != env_ipath) {
230 std::cout << std::endl <<
"WARNING: " << std::endl;
231 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
232 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
233 <<
" we skip the environment variable." << std::endl;
238 if (opt_ipath.empty() && env_ipath.empty()) {
239 usage(argv[0],
nullptr, ipath, opt_opath, username, nbIterations);
240 std::cerr << std::endl <<
"ERROR:" << std::endl;
241 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
242 <<
" environment variable to specify the location of the " << std::endl
243 <<
" image path where test images are located." << std::endl
256 std::cout <<
"** Convert a grey image (.pgm) to a color image (.ppm)" << std::endl;
259 std::cout <<
" Load " << filename << std::endl;
264 std::cout <<
" Resulting image saved in: " << filename << std::endl;
268 std::cout <<
"** Convert a color image (.ppm) to a grey image (.pgm)" << std::endl;
271 std::cout <<
" Load " << filename << std::endl;
276 std::cout <<
" Resulting image saved in: " << filename << std::endl;
280 std::cout <<
"** Convert YUV pixel value to a RGB value" << std::endl;
281 unsigned char y = 187, u = 10, v = 30;
282 unsigned char r, g, b;
286 std::cout <<
" y(" << (int)y <<
") u(" << (
int)u <<
") v(" << (int)v <<
") = r(" << (
int)r <<
") g(" << (int)g
287 <<
") b(" << (
int)b <<
")" << std::endl;
295 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
300 std::cout <<
"** Convert a cv::Mat to a vpImage<vpRGBa>" << std::endl;
303 std::cout <<
" Reading the color image with c++ interface of opencv: " << filename << std::endl;
304 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
305 int flags = cv::IMREAD_COLOR;
307 int flags = CV_LOAD_IMAGE_COLOR;
309 imageMat = cv::imread(filename, flags);
310 if (imageMat.data ==
nullptr) {
311 std::cout <<
" Cannot read image: " << filename << std::endl;
316 std::cout <<
" Resulting image saved in: " << filename << std::endl;
322 std::cout <<
" Reading the grayscale image with opencv: " << filename << std::endl;
323 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
324 flags = cv::IMREAD_GRAYSCALE;
326 flags = CV_LOAD_IMAGE_GRAYSCALE;
328 imageMat = cv::imread(filename, flags);
329 if (imageMat.data ==
nullptr) {
330 std::cout <<
" Cannot read image: " << filename << std::endl;
335 std::cout <<
" Resulting image saved in: " << filename << std::endl;
341 std::cout <<
"** Convert a cv::Mat to a vpImage<nsigned char>" << std::endl;
346 std::cout <<
" Reading the color image with opencv: " << filename << std::endl;
347 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
348 flags = cv::IMREAD_COLOR;
350 flags = CV_LOAD_IMAGE_COLOR;
352 imageMat = cv::imread(filename, flags);
353 if (imageMat.data ==
nullptr) {
354 std::cout <<
" Cannot read image: " << filename << std::endl;
359 std::cout <<
" Resulting image saved in: " << filename << std::endl;
366 std::cout <<
" Reading the greyscale image with opencv: " << filename << std::endl;
367 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
368 flags = cv::IMREAD_GRAYSCALE;
370 flags = CV_LOAD_IMAGE_GRAYSCALE;
372 imageMat = cv::imread(filename, flags);
373 if (imageMat.data ==
nullptr) {
374 std::cout <<
" Cannot read image: " << filename << std::endl;
379 std::cout <<
" Resulting image saved in: " << filename << std::endl;
382 std::cout <<
" Convert result in " << filename << std::endl;
387 std::cout <<
"** Convert a vpImage<vpRGBa> to a cv::Mat" << std::endl;
393 std::cout <<
" Load " << filename << std::endl;
398 std::cout <<
" Resulting image saved in: " << filename << std::endl;
399 if (!cv::imwrite(filename, imageMat)) {
400 std::cout <<
" Cannot write image: " << filename << std::endl;
403 std::cout <<
" Convert result in " << filename << std::endl;
408 std::cout <<
"** Convert a vpImage<unsigned char> to a cv::Mat" << std::endl;
414 std::cout <<
" Load " << filename << std::endl;
420 std::cout <<
" Resulting image saved in: " << filename << std::endl;
421 if (!cv::imwrite(filename, imageMat)) {
422 std::cout <<
" Cannot write image: " << filename << std::endl;
425 std::cout <<
" Convert result in " << filename << std::endl;
427 std::cout <<
"== Conversion c++ interface : " << chrono.
getDurationMs() <<
" ms" << std::endl;
433 std::cout <<
"** Split a vpImage<vpRGBa> to vpImage<unsigned char>" << std::endl;
439 std::cout <<
" Load " << filename << std::endl;
444 for (
int iteration = 0; iteration < nbIterations; iteration++) {
449 std::cout <<
" Time for " << nbIterations <<
" split (ms): " << chrono.
getDurationMs() << std::endl;
453 std::cout <<
" Save Klimt R channel: " << filename << std::endl;
458 std::cout <<
" Save Klimt B channel: " << filename << std::endl;
464 std::cout <<
"** Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>" << std::endl;
468 for (
int iteration = 0; iteration < nbIterations; iteration++) {
473 std::cout <<
" Time for 1000 merge (ms): " << chrono.
getDurationMs() << std::endl;
476 std::cout <<
" Resulting image saved in: " << filename << std::endl;
483 std::cout <<
"** Convert a vpImage<vpRGBa> in RGB color space to a vpImage<vpRGBa> in HSV color" << std::endl;
484 unsigned int size = Ic.
getSize();
487 std::vector<unsigned char> hue(size);
488 std::vector<unsigned char> saturation(size);
489 std::vector<unsigned char> value(size);
499 std::cout <<
" Resulting image saved in: " << filename << std::endl;
503 vpImageConvert::HSVToRGBa(&hue.front(), &saturation.front(), &value.front(),
reinterpret_cast<unsigned char *
>(Ic_from_hsv.bitmap), size);
504 for (
unsigned int i = 0; i < Ic.
getHeight(); i++) {
505 for (
unsigned int j = 0; j < Ic.
getWidth(); j++) {
507 if ((!
vpMath::equal(
static_cast<unsigned>(Ic[i][j].R),
static_cast<unsigned>(Ic_from_hsv[i][j].R), precision))
508 || (!
vpMath::equal(
static_cast<unsigned>(Ic[i][j].G),
static_cast<unsigned>(Ic_from_hsv[i][j].G), precision))
509 || (!
vpMath::equal(
static_cast<unsigned>(Ic[i][j].B),
static_cast<unsigned>(Ic_from_hsv[i][j].B), precision))) {
510 std::cerr <<
"Ic[i][j].R=" <<
static_cast<unsigned>(Ic[i][j].R)
511 <<
" ; Ic_from_hsv[i][j].R=" <<
static_cast<unsigned>(Ic_from_hsv[i][j].R) <<
" precision: " << precision << std::endl;
512 std::cerr <<
"Ic[i][j].G=" <<
static_cast<unsigned>(Ic[i][j].G)
513 <<
" ; Ic_from_hsv[i][j].G=" <<
static_cast<unsigned>(Ic_from_hsv[i][j].G) <<
" precision: " << precision << std::endl;
514 std::cerr <<
"Ic[i][j].B=" <<
static_cast<unsigned>(Ic[i][j].B)
515 <<
" ; Ic_from_hsv[i][j].B=" <<
static_cast<unsigned>(Ic_from_hsv[i][j].B) <<
" precision: " << precision << std::endl;
521 std::vector<double> hue2(size);
522 std::vector<double> saturation2(size);
523 std::vector<double> value2(size);
526 std::vector<unsigned char> rgba(size * 4);
531 std::cout <<
" Resulting image saved in: " << filename << std::endl;
534 for (
unsigned int i = 0; i < Ic.
getHeight(); i++) {
535 for (
unsigned int j = 0; j < Ic.
getWidth(); j++) {
536 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) {
537 std::cerr <<
"Ic[i][j].R=" <<
static_cast<unsigned>(Ic[i][j].R)
538 <<
" ; I_HSV2RGBa[i][j].R=" <<
static_cast<unsigned>(I_HSV2RGBa[i][j].R) << std::endl;
539 std::cerr <<
"Ic[i][j].G=" <<
static_cast<unsigned>(Ic[i][j].G)
540 <<
" ; I_HSV2RGBa[i][j].G=" <<
static_cast<unsigned>(I_HSV2RGBa[i][j].G) << std::endl;
541 std::cerr <<
"Ic[i][j].B=" <<
static_cast<unsigned>(Ic[i][j].B)
542 <<
" ; I_HSV2RGBa[i][j].B=" <<
static_cast<unsigned>(I_HSV2RGBa[i][j].B) << std::endl;
551 std::cout <<
"** Construction of a vpImage from an array with copyData==true" << std::endl;
552 std::vector<unsigned char> rgba2(size * 4);
553 std::fill(rgba2.begin(), rgba2.end(), 127);
557 std::cout <<
" Resulting image saved in: " << filename << std::endl;
560 if (I_copyData.getSize() > 0) {
561 I_copyData[0][0].R = 10;
566 std::cout <<
"** Test color conversion" << std::endl;
573 std::vector<unsigned char> rgb_array(I_color.
getSize() * 3);
576 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC)
578 std::cout <<
"\n BGR cv::Mat to Grayscale" << std::endl;
580 cv::Mat colorMat = cv::imread(filename);
581 std::cout <<
" colorMat=" << colorMat.cols <<
"x" << colorMat.rows << std::endl;
584 std::cout <<
"\n RGB to Grayscale + Flip" << std::endl;
585 std::vector<unsigned char> rgb2gray_flip_array_sse(I_color.
getSize());
592 std::cout <<
" Resulting image saved in: " << filename << std::endl;
596 std::cout <<
"\n Conversion BGR to Grayscale + Flip" << std::endl;
597 std::vector<unsigned char> bgr2gray_flip_array_sse(I_color.
getSize());
603 std::cout <<
" Resulting image saved in: " << filename << std::endl;
607 std::cout <<
"\n RGB to Grayscale + Flip + Crop" << std::endl;
608 cv::Rect rect_roi(11, 17, 347, 449);
609 cv::Mat colorMat_crop = colorMat(rect_roi);
610 cv::Mat colorMat_crop_continuous = colorMat(rect_roi).clone();
611 std::cout <<
" colorMat_crop: " << colorMat_crop.cols <<
"x" << colorMat_crop.rows <<
" is continuous? "
612 << colorMat_crop.isContinuous() << std::endl;
613 std::cout <<
" colorMat_crop_continuous: " << colorMat_crop_continuous.cols <<
"x" << colorMat_crop_continuous.rows
614 <<
" is continuous? " << colorMat_crop_continuous.isContinuous() << std::endl;
616 vpImage<vpRGBa> I_color_crop((
unsigned int)(rect_roi.height - rect_roi.y),
617 (
unsigned int)(rect_roi.width - rect_roi.x));
618 for (
unsigned int i = (
unsigned int)rect_roi.y; i < (
unsigned int)rect_roi.height; i++) {
619 for (
unsigned int j = (
unsigned int)rect_roi.x; j < (
unsigned int)rect_roi.width; j++) {
620 I_color_crop[(
unsigned int)((
int)i - rect_roi.y)][(
unsigned int)((int)j - rect_roi.x)] = I_color[i][j];
624 std::cout <<
" Resulting image saved in: " << filename << std::endl;
627 std::vector<unsigned char> rgb_array_crop(I_color_crop.getSize() * 3);
630 std::vector<unsigned char> rgb2gray_flip_crop_array_sse(I_color_crop.getSize());
632 I_color_crop.getHeight(),
true);
633 vpImage<unsigned char> I_rgb2gray_flip_crop_sse(&rgb2gray_flip_crop_array_sse.front(), I_color_crop.getHeight(),
634 I_color_crop.getWidth());
637 std::cout <<
" Resulting image saved in: " << filename << std::endl;
641 std::cout <<
"\n BGR to Grayscale + Flip + Crop" << std::endl;
646 std::cout <<
" Resulting image saved in: " << filename << std::endl;
650 std::cout <<
"\n BGR to Grayscale + Flip + Crop + No continuous Mat" << std::endl;
651 vpImage<unsigned char> I_bgr2gray_flip_crop_no_continuous_sse(I_color_crop.getHeight(), I_color_crop.getWidth());
655 std::cout <<
" Resulting image saved in: " << filename << std::endl;
658 std::cout <<
"Test succeed" << std::endl;
664 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 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)
unsigned int getWidth() const
unsigned int getSize() const
Type * bitmap
points toward the bitmap
unsigned int getHeight() const
static bool equal(double x, double y, double threshold=0.001)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)