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"
75 void usage(
const char *name,
const char *badparam, std::string ipath,
76 std::string opath, std::string user)
79 Test image conversions.\n\
82 %s [-i <input image path>] [-o <output image path>]\n\
88 -i <input image path> %s\n\
89 Set image input path.\n\
90 From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
91 and \"ViSP-images/Klimt/Klimt.ppm\" images.\n\
92 Setting the VISP_INPUT_IMAGE_PATH environment\n\
93 variable produces the same behaviour than using\n\
96 -o <output image path> %s\n\
97 Set image output path.\n\
98 From this directory, creates the \"%s\"\n\
99 subdirectory depending on the username, where \n\
100 Klimt_grey.pgm and Klimt_color.ppm output images\n\
104 Print the help.\n\n",
105 ipath.c_str(), opath.c_str(), user.c_str());
108 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
123 bool getOptions(
int argc,
const char **argv,
124 std::string &ipath, std::string &opath, std::string user)
131 case 'i': ipath = optarg;
break;
132 case 'o': opath = optarg;
break;
133 case 'h': usage(argv[0], NULL, ipath, opath, user);
return false;
break;
136 usage(argv[0], optarg, ipath, opath, user);
return false;
break;
140 if ((c == 1) || (c == -1)) {
142 usage(argv[0], NULL, ipath, opath, user);
143 std::cerr <<
"ERROR: " << std::endl;
144 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
152 main(
int argc,
const char ** argv)
155 std::string env_ipath;
156 std::string opt_ipath;
157 std::string opt_opath;
160 std::string filename;
161 std::string username;
164 char *ptenv = getenv(
"VISP_INPUT_IMAGE_PATH");
169 if (! env_ipath.empty())
174 opt_opath =
"C:/temp";
183 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
188 if (!opt_ipath.empty())
190 if (!opt_opath.empty())
203 usage(argv[0], NULL, ipath, opt_opath, username);
204 std::cerr << std::endl
205 <<
"ERROR:" << std::endl;
206 std::cerr <<
" Cannot create " << opath << std::endl;
207 std::cerr <<
" Check your -o " << opt_opath <<
" option " << std::endl;
214 if (opt_ipath.empty()) {
215 if (ipath != env_ipath) {
216 std::cout << std::endl
217 <<
"WARNING: " << std::endl;
218 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
219 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
220 <<
" we skip the environment variable." << std::endl;
225 if (opt_ipath.empty() && env_ipath.empty()){
226 usage(argv[0], NULL, ipath, opt_opath, username);
227 std::cerr << std::endl
228 <<
"ERROR:" << std::endl;
229 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
231 <<
" environment variable to specify the location of the " << std::endl
232 <<
" image path where test images are located." << std::endl << std::endl;
244 vpTRACE(
"Convert a grey image (.pgm) to a color image (.ppm)");
247 vpCTRACE <<
"Load " << filename << std::endl;
252 vpCTRACE <<
"Write " << filename << std::endl;
256 vpTRACE(
"Convert a color image (.ppm) to a grey image (.pgm)");
259 vpCTRACE <<
"Load " << filename << std::endl;
264 vpCTRACE <<
"Write " << filename << std::endl;
268 unsigned char y=187, u=10, v=30;
269 unsigned char r, g, b;
273 vpTRACE(
"y(%d) u(%d) v(%d) = r(%d) g(%d) b(%d)", y, u, v, r, g, b);
275 #ifdef VISP_HAVE_OPENCV
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;
407 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
414 vpCTRACE <<
"Reading the color image with c++ interface of opencv: "<< std::endl
415 << filename << std::endl;
416 imageMat = cv::imread(filename, 1);
417 if(imageMat.data == NULL){
418 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
425 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
430 vpCTRACE <<
"Reading the greyscale image with opencv: "<< std::endl
431 << filename << std::endl;
432 imageMat = cv::imread(filename, 0);
433 if(imageMat.data == NULL) {
434 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
441 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
450 vpCTRACE <<
"Reading the color image with opencv: "<< std::endl
451 << filename << std::endl;
452 imageMat = cv::imread(filename, 1);
453 if(imageMat.data == NULL){
454 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
461 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
467 vpCTRACE <<
"Reading the greyscale image with opencv: "<< std::endl
468 << filename << std::endl;
469 imageMat = cv::imread(filename, 0);
470 if(imageMat.data == NULL){
471 vpCTRACE<<
"Cannot read image: "<< std::endl << filename << std::endl;
479 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
489 vpCTRACE <<
"Load " << filename << std::endl;
494 vpCTRACE <<
"Write " << filename << std::endl;
495 if(!cv::imwrite(filename, imageMat)){
496 vpCTRACE<<
"Cannot write image: "<< std::endl << filename << std::endl;
497 if(image!=NULL) cvReleaseImage( &image );
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;
519 if(image!=NULL) cvReleaseImage( &image );
522 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
524 std::cout <<
"Conversion c++ interface : " << t3 - t2 <<
" ms" << std::endl;
536 vpCTRACE <<
"Load " << filename << std::endl;
541 for(
int i=0; i<1000;i++){
546 std::cout<<
"Time for 1000 split (ms): "<< endtime - begintime <<std::endl;
550 vpCTRACE <<
"Write " << filename << std::endl;
552 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;
556 vpCTRACE <<
"Write " << filename << std::endl;
558 vpCTRACE<<
"Convert result in "<<std::endl<< filename << std::endl;