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 imageMat = cv::imread(filename, 1);
433 if (imageMat.data == NULL) {
434 std::cout <<
" Cannot read image: " << filename << std::endl;
439 std::cout <<
" Resulting image saved in: " << filename << std::endl;
445 std::cout <<
" Reading the greyscale image with opencv: " << filename << std::endl;
446 imageMat = cv::imread(filename, 0);
447 if (imageMat.data == NULL) {
448 std::cout <<
" Cannot read image: " << filename << std::endl;
453 std::cout <<
" Resulting image saved in: " << filename << std::endl;
459 std::cout <<
"** Convert a cv::Mat to a vpImage<nsigned char>" << std::endl;
464 std::cout <<
" Reading the color image with opencv: " << filename << std::endl;
465 imageMat = cv::imread(filename, 1);
466 if (imageMat.data == NULL) {
467 std::cout <<
" Cannot read image: " << filename << std::endl;
472 std::cout <<
" Resulting image saved in: " << filename << std::endl;
479 std::cout <<
" Reading the greyscale image with opencv: " << filename << std::endl;
480 imageMat = cv::imread(filename, 0);
481 if (imageMat.data == NULL) {
482 std::cout <<
" Cannot read image: " << filename << std::endl;
487 std::cout <<
" Resulting image saved in: " << filename << std::endl;
490 std::cout <<
" Convert result in " << filename << std::endl;
495 std::cout <<
"** Convert a vpImage<vpRGBa> to a cv::Mat" << std::endl;
501 std::cout <<
" Load " << filename << std::endl;
506 std::cout <<
" Resulting image saved in: " << filename << std::endl;
507 if (!cv::imwrite(filename, imageMat)) {
508 std::cout <<
" Cannot write image: " << filename << std::endl;
511 std::cout <<
" Convert result in " << filename << std::endl;
516 std::cout <<
"** Convert a vpImage<unsigned char> to a cv::Mat" << std::endl;
522 std::cout <<
" Load " << filename << std::endl;
528 std::cout <<
" Resulting image saved in: " << filename << std::endl;
529 if (!cv::imwrite(filename, imageMat)) {
530 std::cout <<
" Cannot write image: " << filename << std::endl;
533 std::cout <<
" Convert result in " << filename << std::endl;
535 std::cout <<
"== Conversion c++ interface : " << chrono.
getDurationMs() <<
" ms" << std::endl;
542 std::cout <<
"** Split a vpImage<vpRGBa> to vpImage<unsigned char>" << std::endl;
548 std::cout <<
" Load " << filename << std::endl;
553 for (
int iteration = 0; iteration < nbIterations; iteration++) {
558 std::cout <<
" Time for " << nbIterations <<
" split (ms): " << chrono.
getDurationMs() << std::endl;
562 std::cout <<
" Save Klimt R channel: " << filename << std::endl;
567 std::cout <<
" Save Klimt B channel: " << filename << std::endl;
573 std::cout <<
"** Merge 4 vpImage<unsigned char> (RGBa) to vpImage<vpRGBa>" << std::endl;
577 for (
int iteration = 0; iteration < nbIterations; iteration++) {
582 std::cout <<
" Time for 1000 merge (ms): " << chrono.
getDurationMs() << std::endl;
585 std::cout <<
" Resulting image saved in: " << filename << std::endl;
592 std::cout <<
"** Convert a vpImage<vpRGBa> in RGB color space to a " 593 "vpImage<vpRGBa> in HSV color" 595 unsigned int size = Ic.
getSize();
597 std::vector<unsigned char> hue(size);
598 std::vector<unsigned char> saturation(size);
599 std::vector<unsigned char> value(size);
609 std::cout <<
" Resulting image saved in: " << filename << std::endl;
613 std::vector<double> hue2(size);
614 std::vector<double> saturation2(size);
615 std::vector<double> value2(size);
618 std::vector<unsigned char> rgba(size * 4);
621 vpImage<vpRGBa> I_HSV2RGBa(reinterpret_cast<vpRGBa *>(&rgba.front()), h, w);
623 std::cout <<
" Resulting image saved in: " << filename << std::endl;
626 for (
unsigned int i = 0; i < Ic.
getHeight(); i++) {
627 for (
unsigned int j = 0; j < Ic.
getWidth(); j++) {
628 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) {
629 std::cerr <<
"Ic[i][j].R=" <<
static_cast<unsigned>(Ic[i][j].R)
630 <<
" ; I_HSV2RGBa[i][j].R=" << static_cast<unsigned>(I_HSV2RGBa[i][j].R) << std::endl;
631 std::cerr <<
"Ic[i][j].G=" <<
static_cast<unsigned>(Ic[i][j].G)
632 <<
" ; I_HSV2RGBa[i][j].G=" << static_cast<unsigned>(I_HSV2RGBa[i][j].G) << std::endl;
633 std::cerr <<
"Ic[i][j].B=" <<
static_cast<unsigned>(Ic[i][j].B)
634 <<
" ; I_HSV2RGBa[i][j].B=" << static_cast<unsigned>(I_HSV2RGBa[i][j].B) << std::endl;
643 std::cout <<
"** Construction of a vpImage from an array with copyData==true" << std::endl;
644 std::vector<unsigned char> rgba2(size*4);
645 std::fill(rgba2.begin(), rgba2.end(), 127);
646 vpImage<vpRGBa> I_copyData(reinterpret_cast<vpRGBa *>(&rgba2.front()), h, w,
true);
649 std::cout <<
" Resulting image saved in: " << filename << std::endl;
652 if (I_copyData.getSize() > 0) {
653 I_copyData[0][0].R = 10;
658 std::cout <<
"** Test color conversion" << std::endl;
665 std::vector<unsigned char> rgb_array(I_color.
getSize()*3);
668 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) 670 std::cout <<
"\n BGR cv::Mat to Grayscale" << std::endl;
672 cv::Mat colorMat = cv::imread(filename);
673 std::cout <<
" colorMat=" << colorMat.cols <<
"x" << colorMat.rows << std::endl;
676 std::cout <<
"\n RGB to Grayscale + Flip" << std::endl;
677 std::vector<unsigned char> rgb2gray_flip_array_sse(I_color.
getSize());
682 std::cout <<
" Resulting image saved in: " << filename << std::endl;
686 std::cout <<
"\n Conversion BGR to Grayscale + Flip" << std::endl;
687 std::vector<unsigned char> bgr2gray_flip_array_sse(I_color.
getSize());
692 std::cout <<
" Resulting image saved in: " << filename << std::endl;
696 std::cout <<
"\n RGB to Grayscale + Flip + Crop" << std::endl;
697 cv::Rect rect_roi(11, 17, 347, 449);
698 cv::Mat colorMat_crop = colorMat(rect_roi);
699 cv::Mat colorMat_crop_continous = colorMat(rect_roi).clone();
700 std::cout <<
" colorMat_crop: " << colorMat_crop.cols <<
"x" << colorMat_crop.rows <<
" is continuous? " 701 << colorMat_crop.isContinuous() << std::endl;
702 std::cout <<
" colorMat_crop_continous: " << colorMat_crop_continous.cols <<
"x" << colorMat_crop_continous.rows
703 <<
" is continuous? " << colorMat_crop_continous.isContinuous() << std::endl;
705 vpImage<vpRGBa> I_color_crop((
unsigned int)(rect_roi.height - rect_roi.y),
706 (
unsigned int)(rect_roi.width - rect_roi.x));
707 for (
unsigned int i = (
unsigned int)rect_roi.y; i < (
unsigned int)rect_roi.height; i++) {
708 for (
unsigned int j = (
unsigned int)rect_roi.x; j < (
unsigned int)rect_roi.width; j++) {
709 I_color_crop[(
unsigned int)((
int)i - rect_roi.y)][(
unsigned int)((int)j - rect_roi.x)] = I_color[i][j];
713 std::cout <<
" Resulting image saved in: " << filename << std::endl;
716 std::vector<unsigned char> rgb_array_crop(I_color_crop.getSize() * 3);
719 std::vector<unsigned char> rgb2gray_flip_crop_array_sse(I_color_crop.getSize());
721 I_color_crop.getHeight(),
true);
722 vpImage<unsigned char> I_rgb2gray_flip_crop_sse(&rgb2gray_flip_crop_array_sse.front(), I_color_crop.getHeight(),
723 I_color_crop.getWidth());
726 std::cout <<
" Resulting image saved in: " << filename << std::endl;
730 std::cout <<
"\n BGR to Grayscale + Flip + Crop" << std::endl;
735 std::cout <<
" Resulting image saved in: " << filename << std::endl;
739 std::cout <<
"\n BGR to Grayscale + Flip + Crop + No continuous Mat" << std::endl;
740 vpImage<unsigned char> I_bgr2gray_flip_crop_no_continuous_sse(I_color_crop.getHeight(), I_color_crop.getWidth());
744 std::cout <<
" Resulting image saved in: " << filename << std::endl;
747 std::cout <<
"Test succeed" << std::endl;
752 std::cout <<
"Catch an exception: " << e.
getMessage() << std::endl;
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int size)
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 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)
const char * getMessage(void) const
unsigned int getHeight() const
static void read(vpImage< unsigned char > &I, const std::string &filename)
unsigned int getSize() const
static void RGBaToRGB(unsigned char *rgba, unsigned char *rgb, unsigned int size)
unsigned int getWidth() const