44 #include <visp/vpConfig.h>
45 #include <visp/vpImage.h>
46 #include <visp/vpImageIo.h>
47 #include <visp/vpImageConvert.h>
48 #include <visp/vpParseArgv.h>
49 #include <visp/vpIoTools.h>
50 #include <visp/vpDebug.h>
51 #include <visp/vpTime.h>
62 #define GETOPTARGS "i:o:h"
64 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user);
65 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user);
78 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user)
81 Test image conversions.\n\
84 %s [-i <input image path>] [-o <output image path>]\n\
90 -i <input image path> %s\n\
91 Set image input path.\n\
92 From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
93 and \"ViSP-images/Klimt/Klimt.ppm\" images.\n\
94 Setting the VISP_INPUT_IMAGE_PATH environment\n\
95 variable produces the same behaviour than using\n\
98 -o <output image path> %s\n\
99 Set image output path.\n\
100 From this directory, creates the \"%s\"\n\
101 subdirectory depending on the username, where \n\
102 Klimt_grey.pgm and Klimt_color.ppm output images\n\
106 Print the help.\n\n",
107 ipath.c_str(), opath.c_str(), user.c_str());
110 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
125 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user)
132 case 'i': ipath = optarg_;
break;
133 case 'o': opath = optarg_;
break;
134 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;
561 std::cout <<
"Catch an exception: " << e << std::endl;
static void write(const vpImage< unsigned char > &I, const char *filename)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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 double measureTimeMs()
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static void read(vpImage< unsigned char > &I, const char *filename)