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;
165 char *ptenv = getenv(
"VISP_INPUT_IMAGE_PATH");
170 if (! env_ipath.empty())
175 opt_opath =
"C:/temp";
184 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
189 if (!opt_ipath.empty())
191 if (!opt_opath.empty())
204 usage(argv[0], NULL, ipath, opt_opath, username);
205 std::cerr << std::endl
206 <<
"ERROR:" << std::endl;
207 std::cerr <<
" Cannot create " << opath << std::endl;
208 std::cerr <<
" Check your -o " << opt_opath <<
" option " << std::endl;
215 if (opt_ipath.empty()) {
216 if (ipath != env_ipath) {
217 std::cout << std::endl
218 <<
"WARNING: " << std::endl;
219 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
220 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
221 <<
" we skip the environment variable." << std::endl;
226 if (opt_ipath.empty() && env_ipath.empty()){
227 usage(argv[0], NULL, ipath, opt_opath, username);
228 std::cerr << std::endl
229 <<
"ERROR:" << std::endl;
230 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
232 <<
" environment variable to specify the location of the " << std::endl
233 <<
" image path where test images are located." << std::endl << std::endl;
245 vpTRACE(
"Convert a grey image (.pgm) to a color image (.ppm)");
248 vpCTRACE <<
"Load " << filename << std::endl;
253 vpCTRACE <<
"Write " << filename << std::endl;
257 vpTRACE(
"Convert a color image (.ppm) to a grey image (.pgm)");
260 vpCTRACE <<
"Load " << filename << std::endl;
265 vpCTRACE <<
"Write " << filename << std::endl;
269 unsigned char y=187, u=10, v=30;
270 unsigned char r, g, b;
274 vpTRACE(
"y(%d) u(%d) v(%d) = r(%d) g(%d) b(%d)", y, u, v, r, g, b);
276 #ifdef VISP_HAVE_OPENCV
281 IplImage* image = NULL;
286 vpCTRACE <<
"Reading the color image with opencv: "<< std::endl
287 << filename << std::endl;
288 if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
289 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
297 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
303 vpCTRACE <<
"Reading the greyscale image with opencv: "<< std::endl
304 << filename << std::endl;
305 if(image!=NULL) cvReleaseImage( &image );
306 if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
307 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
315 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
324 vpCTRACE <<
"Reading the color image with opencv: "<< std::endl
325 << filename << std::endl;
326 if(image!=NULL) cvReleaseImage( &image );
327 if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR)) == NULL) {
328 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
336 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
342 vpCTRACE <<
"Reading the greyscale image with opencv: "<< std::endl
343 << filename << std::endl;
344 if(image!=NULL) cvReleaseImage( &image );
345 if((image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE)) == NULL) {
346 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
355 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
365 vpCTRACE <<
"Load " << filename << std::endl;
370 vpCTRACE <<
"Write " << filename << std::endl;
371 if((cvSaveImage(filename.c_str(), image)) == 0) {
372 vpCTRACE<<
"Cannot write image: "<< std::endl << filename << std::endl;
373 if(image!=NULL) cvReleaseImage( &image );
376 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
386 vpCTRACE <<
"Load " << filename << std::endl;
392 vpCTRACE <<
"Write " << filename << std::endl;
393 if((cvSaveImage(filename.c_str(), image)) == 0) {
394 vpCTRACE<<
"Cannot write image: "<< std::endl << filename << std::endl;
395 if(image!=NULL) cvReleaseImage( &image );
398 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
400 if(image!=NULL) cvReleaseImage( &image );
402 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;
498 if(image!=NULL) cvReleaseImage( &image );
501 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
511 vpCTRACE <<
"Load " << filename << std::endl;
517 vpCTRACE <<
"Write " << filename << std::endl;
518 if(!cv::imwrite(filename, imageMat)){
519 vpCTRACE<<
"Cannot write image: "<< std::endl << filename << std::endl;
520 if(image!=NULL) cvReleaseImage( &image );
523 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
525 std::cout <<
"Conversion c++ interface : " << t3 - t2 <<
" ms" << std::endl;
537 vpCTRACE <<
"Load " << filename << std::endl;
542 for(
int i=0; i<1000;i++){
547 std::cout<<
"Time for 1000 split (ms): "<< endtime - begintime <<std::endl;
551 vpCTRACE <<
"Write " << filename << std::endl;
553 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
557 vpCTRACE <<
"Write " << filename << std::endl;
559 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
563 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)