40 #include <visp3/core/vpConfig.h> 41 #include <visp3/core/vpImage.h> 42 #include <visp3/io/vpImageIo.h> 43 #include <visp3/core/vpImageConvert.h> 44 #include <visp3/io/vpParseArgv.h> 45 #include <visp3/core/vpIoTools.h> 46 #include <visp3/core/vpDebug.h> 47 #include <visp3/core/vpTime.h> 58 #define GETOPTARGS "cdi:o:h" 60 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user);
61 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user);
74 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user)
77 Test image conversions.\n\ 80 %s [-i <input image path>] [-o <output image path>]\n\ 86 -i <input image path> %s\n\ 87 Set image input path.\n\ 88 From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\ 89 and \"ViSP-images/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\ 102 Print the help.\n\n",
103 ipath.c_str(), opath.c_str(), user.c_str());
106 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
121 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user)
128 case 'i': ipath = optarg_;
break;
129 case 'o': opath = optarg_;
break;
130 case 'h': usage(argv[0], NULL, ipath, opath, user);
return false;
break;
137 usage(argv[0], optarg_, ipath, opath, user);
return false;
break;
141 if ((c == 1) || (c == -1)) {
143 usage(argv[0], NULL, ipath, opath, user);
144 std::cerr <<
"ERROR: " << std::endl;
145 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
153 main(
int argc,
const char ** argv)
156 std::string env_ipath;
157 std::string opt_ipath;
158 std::string opt_opath;
161 std::string filename;
162 std::string username;
168 if (! env_ipath.empty())
173 opt_opath =
"C:/temp";
182 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
187 if (!opt_ipath.empty())
189 if (!opt_opath.empty())
202 usage(argv[0], NULL, ipath, opt_opath, username);
203 std::cerr << std::endl
204 <<
"ERROR:" << std::endl;
205 std::cerr <<
" Cannot create " << opath << std::endl;
206 std::cerr <<
" Check your -o " << opt_opath <<
" option " << std::endl;
213 if (opt_ipath.empty()) {
214 if (ipath != env_ipath) {
215 std::cout << std::endl
216 <<
"WARNING: " << std::endl;
217 std::cout <<
" Since -i <visp image path=" << ipath <<
"> " 218 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
219 <<
" we skip the environment variable." << std::endl;
224 if (opt_ipath.empty() && env_ipath.empty()){
225 usage(argv[0], NULL, ipath, opt_opath, username);
226 std::cerr << std::endl
227 <<
"ERROR:" << std::endl;
228 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " 230 <<
" environment variable to specify the location of the " << std::endl
231 <<
" image path where test images are located." << std::endl << std::endl;
243 vpTRACE(
"Convert a grey image (.pgm) to a color image (.ppm)");
246 vpCTRACE <<
"Load " << filename << std::endl;
251 vpCTRACE <<
"Write " << filename << std::endl;
255 vpTRACE(
"Convert a color image (.ppm) to a grey image (.pgm)");
258 vpCTRACE <<
"Load " << filename << std::endl;
263 vpCTRACE <<
"Write " << filename << std::endl;
267 unsigned char y=187, u=10, v=30;
268 unsigned char r, g, b;
272 vpTRACE(
"y(%d) u(%d) v(%d) = r(%d) g(%d) b(%d)", y, u, v, r, g, b);
274 #ifdef VISP_HAVE_OPENCV 275 #if VISP_HAVE_OPENCV_VERSION < 0x020408 280 IplImage* image = NULL;
285 vpCTRACE <<
"Reading the color image with opencv: "<< std::endl
286 << filename << std::endl;
287 if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
288 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
296 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
302 vpCTRACE <<
"Reading the greyscale image with opencv: "<< std::endl
303 << filename << std::endl;
304 if(image!=NULL) cvReleaseImage( &image );
305 if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
306 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
314 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
323 vpCTRACE <<
"Reading the color image with opencv: "<< std::endl
324 << filename << std::endl;
325 if(image!=NULL) cvReleaseImage( &image );
326 if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
327 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
335 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
341 vpCTRACE <<
"Reading the greyscale image with opencv: "<< std::endl
342 << filename << std::endl;
343 if(image!=NULL) cvReleaseImage( &image );
344 if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
345 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
354 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
364 vpCTRACE <<
"Load " << filename << std::endl;
369 vpCTRACE <<
"Write " << filename << std::endl;
370 if((cvSaveImage(filename.c_str(), image)) == 0) {
371 vpCTRACE<<
"Cannot write image: "<< std::endl << filename << std::endl;
372 if(image!=NULL) cvReleaseImage( &image );
375 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
385 vpCTRACE <<
"Load " << filename << std::endl;
391 vpCTRACE <<
"Write " << filename << std::endl;
392 if((cvSaveImage(filename.c_str(), image)) == 0) {
393 vpCTRACE<<
"Cannot write image: "<< std::endl << filename << std::endl;
394 if(image!=NULL) cvReleaseImage( &image );
397 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
399 if(image!=NULL) cvReleaseImage( &image );
401 std::cout <<
"Conversion c interface : " << t1 - t0 <<
" ms" << std::endl;
408 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 415 vpCTRACE <<
"Reading the color image with c++ interface of opencv: "<< std::endl
416 << filename << std::endl;
417 imageMat = cv::imread(filename, 1);
418 if(imageMat.data == NULL){
419 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
426 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
431 vpCTRACE <<
"Reading the greyscale image with opencv: "<< std::endl
432 << filename << std::endl;
433 imageMat = cv::imread(filename, 0);
434 if(imageMat.data == NULL) {
435 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
442 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
451 vpCTRACE <<
"Reading the color image with opencv: "<< std::endl
452 << filename << std::endl;
453 imageMat = cv::imread(filename, 1);
454 if(imageMat.data == NULL){
455 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
462 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
468 vpCTRACE <<
"Reading the greyscale image with opencv: "<< std::endl
469 << filename << std::endl;
470 imageMat = cv::imread(filename, 0);
471 if(imageMat.data == NULL){
472 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
480 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
490 vpCTRACE <<
"Load " << filename << std::endl;
495 vpCTRACE <<
"Write " << filename << std::endl;
496 if(!cv::imwrite(filename, imageMat)){
497 vpCTRACE<<
"Cannot write image: "<< std::endl << filename << std::endl;
500 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
510 vpCTRACE <<
"Load " << filename << std::endl;
516 vpCTRACE <<
"Write " << filename << std::endl;
517 if(!cv::imwrite(filename, imageMat)){
518 vpCTRACE<<
"Cannot write image: "<< std::endl << filename << std::endl;
521 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
523 std::cout <<
"Conversion c++ interface : " << t3 - t2 <<
" ms" << std::endl;
535 vpCTRACE <<
"Load " << filename << std::endl;
540 for(
int i=0; i<1000;i++){
545 std::cout<<
"Time for 1000 split (ms): "<< endtime - begintime <<std::endl;
549 vpCTRACE <<
"Write " << filename << std::endl;
551 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
555 vpCTRACE <<
"Write " << filename << std::endl;
557 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
565 for(
int i=0; i<1000; i++){
570 std::cout<<
"Time for 1000 merge (ms): "<< endtime - begintime <<std::endl;
581 unsigned char *hue =
new unsigned char[size];
582 unsigned char *saturation =
new unsigned char[size];
583 unsigned char *value =
new unsigned char[size];
597 double *hue2 =
new double[size];
598 double *saturation2 =
new double[size];
599 double *value2 =
new double[size];
602 unsigned char *rgba =
new unsigned char[size*4];
610 if(saturation2 != NULL) {
611 delete[] saturation2;
625 for(
unsigned int i = 0; i < Ic.
getHeight(); i++) {
626 for(
unsigned int j = 0; j < Ic.
getWidth(); j++) {
627 if(Ic[i][j].R != I_HSV2RGBa[i][j].R ||
628 Ic[i][j].G != I_HSV2RGBa[i][j].G ||
629 Ic[i][j].B != I_HSV2RGBa[i][j].B) {
630 std::cerr <<
"Ic[i][j].R=" <<
static_cast<unsigned>(Ic[i][j].R)
631 <<
" ; I_HSV2RGBa[i][j].R=" << static_cast<unsigned>(I_HSV2RGBa[i][j].R) << std::endl;
632 std::cerr <<
"Ic[i][j].G=" <<
static_cast<unsigned>(Ic[i][j].G)
633 <<
" ; I_HSV2RGBa[i][j].G=" << static_cast<unsigned>(I_HSV2RGBa[i][j].G) << std::endl;
634 std::cerr <<
"Ic[i][j].B=" <<
static_cast<unsigned>(Ic[i][j].B)
635 <<
" ; I_HSV2RGBa[i][j].B=" << static_cast<unsigned>(I_HSV2RGBa[i][j].B) << std::endl;
644 unsigned char *rgba2 =
new unsigned char[size*4];
645 memset(rgba2, 127, size*4);
658 if(I_copyData.getSize() > 0) {
659 I_copyData[0][0].R = 10;
665 std::cout <<
"Catch an exception: " << e << std::endl;
static void write(const vpImage< unsigned char > &I, const char *filename)
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class that defines a RGB 32 bits structure.
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 RGBaToHSV(const unsigned char *rgba, double *hue, double *saturation, double *value, const unsigned int size)
VISP_EXPORT double measureTimeMs()
static void HSVToRGBa(const double *hue, const double *saturation, const double *value, unsigned char *rgba, const unsigned int size)
unsigned int getHeight() const
static void read(vpImage< unsigned char > &I, const char *filename)