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> 58 #define GETOPTARGS "cdi:o:n:h" 71 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user,
int nbiter)
74 Test image conversions.\n\ 77 %s [-i <input image path>] [-o <output image path>] [-n <nb benchmark iterations>]\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\ 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\ 98 -n <nb benchmark iterations> %d\n\ 99 Set the number of benchmark iterations.\n\ 102 Print the help.\n\n", ipath.c_str(), opath.c_str(), user.c_str(), nbiter);
105 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
121 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath,
const std::string &user,
136 nbIterations = atoi(optarg_);
139 usage(argv[0], NULL, ipath, opath, user, nbIterations);
147 usage(argv[0], optarg_, ipath, opath, user, nbIterations);
152 if ((c == 1) || (c == -1)) {
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;
163 int main(
int argc,
const char **argv)
166 std::string env_ipath;
167 std::string opt_ipath;
168 std::string opt_opath;
171 std::string filename;
172 std::string username;
173 int nbIterations = 1;
180 if (!env_ipath.empty())
185 opt_opath =
"C:/temp";
194 if (getOptions(argc, argv, opt_ipath, opt_opath, username, nbIterations) ==
false) {
199 if (!opt_ipath.empty())
201 if (!opt_opath.empty())
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;
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;
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
251 std::cout <<
"** Convert a grey image (.pgm) to a color image (.ppm)" << std::endl;
254 std::cout <<
" Load " << filename << std::endl;
259 std::cout <<
" Resulting image saved in: " << filename << std::endl;
263 std::cout <<
"** Convert a color image (.ppm) to a grey image (.pgm)" << std::endl;
266 std::cout <<
" Load " << filename << std::endl;
271 std::cout <<
" Resulting image saved in: " << filename << std::endl;
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;
281 std::cout <<
" y(" << (int)y <<
") u(" << (
int)u << ") v(" << (
int)v << ") = r(" << (
int)r << ") g(" << (
int)g
282 << ") b(" << (
int)b << ")" <<
std::endl;
285 #ifdef VISP_HAVE_OPENCV 286 #if VISP_HAVE_OPENCV_VERSION < 0x020408 291 std::cout <<
"** Convert an IplImage to a vpImage<vpRGBa>" << std::endl;
292 IplImage *image = NULL;
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;
304 std::cout <<
" Resulting image saved in: " << filename << std::endl;
307 std::cout <<
" Convert result in " << filename << std::endl;
312 std::cout <<
" Reading the greyscale image with opencv: " << filename << std::endl;
314 cvReleaseImage(&image);
315 if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
316 std::cout <<
" Cannot read image: " << filename << std::endl;
321 std::cout <<
" Resulting image saved in: " << filename << std::endl;
324 std::cout <<
" Convert result in " << filename << std::endl;
329 std::cout <<
"** Convert an IplImage to a vpImage<unsigned char>" << std::endl;
334 std::cout <<
" Reading the color image with opencv: " << filename << std::endl;
336 cvReleaseImage(&image);
337 if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
338 std::cout <<
" Cannot read image: " << filename << std::endl;
343 std::cout <<
" Resulting image saved in: " << filename << std::endl;
346 std::cout <<
" Convert result in " << filename << std::endl;
352 std::cout <<
" Reading the greyscale image with opencv: " << filename << std::endl;
354 cvReleaseImage(&image);
355 if ((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
356 std::cout <<
" Cannot read image: " << filename << std::endl;
361 std::cout <<
" Resulting image saved in: " << filename << std::endl;
364 std::cout <<
" Convert result in " << filename << std::endl;
369 std::cout <<
"** Convert a vpImage<vpRGBa> to an IplImage" << std::endl;
375 std::cout <<
" Load " << filename << std::endl;
380 std::cout <<
" Write " << filename << std::endl;
381 if ((cvSaveImage(filename.c_str(), image)) == 0) {
382 std::cout <<
" Cannot write image: " << filename << std::endl;
384 cvReleaseImage(&image);
387 std::cout <<
" Convert result in " << filename << std::endl;
392 std::cout <<
"** Convert a vpImage<unsigned char> to an IplImage" << std::endl;
398 std::cout <<
" Load " << filename << std::endl;
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;
408 cvReleaseImage(&image);
411 std::cout <<
" Convert result in " << filename << std::endl;
414 cvReleaseImage(&image);
416 std::cout <<
"== Conversion c interface : " << t1 - t0 <<
" ms" << std::endl;
423 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 428 std::cout <<
"** Convert a cv::Mat to a vpImage<vpRGBa>" << std::endl;
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;
437 imageMat = cv::imread(filename, flags);
438 if (imageMat.data == NULL) {
439 std::cout <<
" Cannot read image: " << filename << std::endl;
444 std::cout <<
" Resulting image saved in: " << filename << std::endl;
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;
456 imageMat = cv::imread(filename, flags);
457 if (imageMat.data == NULL) {
458 std::cout <<
" Cannot read image: " << filename << std::endl;
463 std::cout <<
" Resulting image saved in: " << filename << std::endl;
469 std::cout <<
"** Convert a cv::Mat to a vpImage<nsigned char>" << std::endl;
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;
480 imageMat = cv::imread(filename, flags);
481 if (imageMat.data == NULL) {
482 std::cout <<
" Cannot read image: " << filename << std::endl;
487 std::cout <<
" Resulting image saved in: " << filename << std::endl;
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;
500 imageMat = cv::imread(filename, flags);
501 if (imageMat.data == NULL) {
502 std::cout <<
" Cannot read image: " << filename << std::endl;
507 std::cout <<
" Resulting image saved in: " << filename << std::endl;
510 std::cout <<
" Convert result in " << filename << std::endl;
515 std::cout <<
"** Convert a vpImage<vpRGBa> to a cv::Mat" << std::endl;
521 std::cout <<
" Load " << filename << std::endl;
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;
531 std::cout <<
" Convert result in " << filename << std::endl;
536 std::cout <<
"** Convert a vpImage<unsigned char> to a cv::Mat" << std::endl;
542 std::cout <<
" Load " << filename << std::endl;
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;
553 std::cout <<
" Convert result in " << filename << std::endl;
555 std::cout <<
"== Conversion c++ interface : " << chrono.
getDurationMs() <<
" ms" << std::endl;
562 std::cout <<
"** Split a vpImage<vpRGBa> to vpImage<unsigned char>" << std::endl;
568 std::cout <<
" Load " << filename << std::endl;
573 for (
int iteration = 0; iteration < nbIterations; iteration++) {
578 std::cout <<
" Time for " << nbIterations <<
" split (ms): " << chrono.
getDurationMs() << std::endl;
582 std::cout <<
" Save Klimt R channel: " << filename << std::endl;
587 std::cout <<
" Save Klimt B channel: " << filename << std::endl;
593 std::cout <<
"** Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>" << std::endl;
597 for (
int iteration = 0; iteration < nbIterations; iteration++) {
602 std::cout <<
" Time for 1000 merge (ms): " << chrono.
getDurationMs() << std::endl;
605 std::cout <<
" Resulting image saved in: " << filename << std::endl;
612 std::cout <<
"** Convert a vpImage<vpRGBa> in RGB color space to a " 613 "vpImage<vpRGBa> in HSV color" 615 unsigned int size = Ic.
getSize();
617 std::vector<unsigned char> hue(size);
618 std::vector<unsigned char> saturation(size);
619 std::vector<unsigned char> value(size);
629 std::cout <<
" Resulting image saved in: " << filename << std::endl;
633 std::vector<double> hue2(size);
634 std::vector<double> saturation2(size);
635 std::vector<double> value2(size);
638 std::vector<unsigned char> rgba(size * 4);
641 vpImage<vpRGBa> I_HSV2RGBa(reinterpret_cast<vpRGBa *>(&rgba.front()), h, w);
643 std::cout <<
" Resulting image saved in: " << filename << std::endl;
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;
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);
669 std::cout <<
" Resulting image saved in: " << filename << std::endl;
672 if (I_copyData.getSize() > 0) {
673 I_copyData[0][0].R = 10;
678 std::cout <<
"** Test color conversion" << std::endl;
685 std::vector<unsigned char> rgb_array(I_color.
getSize()*3);
688 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) 690 std::cout <<
"\n BGR cv::Mat to Grayscale" << std::endl;
692 cv::Mat colorMat = cv::imread(filename);
693 std::cout <<
" colorMat=" << colorMat.cols <<
"x" << colorMat.rows << std::endl;
696 std::cout <<
"\n RGB to Grayscale + Flip" << std::endl;
697 std::vector<unsigned char> rgb2gray_flip_array_sse(I_color.
getSize());
702 std::cout <<
" Resulting image saved in: " << filename << std::endl;
706 std::cout <<
"\n Conversion BGR to Grayscale + Flip" << std::endl;
707 std::vector<unsigned char> bgr2gray_flip_array_sse(I_color.
getSize());
712 std::cout <<
" Resulting image saved in: " << filename << std::endl;
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;
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];
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);
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());
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());
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);
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);
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);
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);
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);
767 std::cout <<
"Test succeed" << std::endl;
772 std::cout <<
"Catch an exception: " << e.
getMessage() << std::endl;
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void RGBaToHSV(const unsigned char *rgba, double *hue, double *saturation, double *value, unsigned int size)
Type * bitmap
points toward the bitmap
error that can be emited by ViSP classes.
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 YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g, unsigned char &b)
VISP_EXPORT double measureTimeMs()
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
void start(bool reset=true)
static void write(const vpImage< unsigned char > &I, const std::string &filename)
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int width, unsigned int height, bool flip=false)
unsigned int getSize() const
Read/write images with various image format.
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 read(vpImage< unsigned char > &I, const std::string &filename)
unsigned int getHeight() const
static void RGBaToRGB(unsigned char *rgba, unsigned char *rgb, unsigned int size)
const char * getMessage() const