dosegment.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <cstdlib>
00003 #include <cstdio>
00004 #include <vector>
00005 #ifdef USE_TBB
00006 #include <tbb/tbb.h>
00007 #endif // USE_TBB
00008 #include "pyra/tpimageutil.h"
00009 
00010 #include "fgbgsegment.h"
00011 #include "timercpu.h"
00012 
00013 #ifdef USE_OPENCV
00014 #include <opencv2/opencv.hpp>
00015 #include <opencv2/highgui/highgui.hpp>
00016 #endif // USE_OPENCV
00017 
00023 int main(int argc, char *argv[])
00024 {
00025   if (argc<6) {
00026     std::cerr << "Usage: " << argv[0] << " <image format> <disparity format> <first index> <last index> <disparity range>" << std::endl;
00027     std::cout << "   Ex:" << argv[0] <<" ../demo/clim%04d.pgm ../demo/dimg%04d.pgm 1 9 64" << std::endl;
00028     return 1;
00029   }
00030   // Read arguments
00031   char *image_format = argv[1];
00032   char *disp_format = argv[2];
00033   int fstidx = atoi(argv[3]);
00034   int lstidx = atoi(argv[4]);
00035   int drange = atoi(argv[5]);
00036   if (lstidx<fstidx) {
00037     int tmpidx = fstidx; 
00038     fstidx = lstidx; 
00039     lstidx = tmpidx;  
00040   }
00041   int w = 640;
00042   int h = 480;
00043   /* Initialise Segmenter with width and height of images, disparity range and 
00044    * gradient cost
00045    */
00046   FgBgSegment fgbgsegment(w, h, drange, 30.0);
00047 #ifdef USE_CUDA
00048   fgbgsegment.UseGPU(false);
00049 #else 
00050   fgbgsegment.UseGPU(false);
00051 #endif //USE_CUDA
00052   Image<uint8_t> image(3*w, h);
00053   Image<float> disparities(w, h);
00054   for (int i=fstidx;i<=lstidx;i++) {
00055     char name[80];
00056     sprintf(name, image_format, i); //%%%%
00057     /* Interfaces to read in images and convert them to 16 bit aligned images. */
00058 #ifdef USE_OPENCV
00059     cv::Mat tmp_img = cv::imread(name, 1);
00060     if(tmp_img.empty()) {
00061       std::cout << "Image " << name << " does not exist " << std::endl;
00062       exit(-1);
00063     }
00064     cv::cvtColor(tmp_img, tmp_img, CV_BGR2RGB);
00065     image.SetDataAlign(tmp_img.ptr<uchar>(0), 3*w, h);
00066 #else
00067     if(!image.LoadRGB(name)){
00068       std::cout << "Image " << name << " does not exist " << std::endl;
00069       exit(-1);
00070     }
00071 #endif //USE_OPENCV
00072     sprintf(name, disp_format, i); //%%%%
00073 #ifdef USE_OPENCV
00074     cv::Mat tmp_disp = cv::imread(name, 0);
00075     if(tmp_disp.empty()) {
00076       std::cout << "Image  " << name << " does not exist " << std::endl;
00077       exit(-1);
00078     }
00079     cv::Mat tmp_disp_fl;
00080     tmp_disp.convertTo(tmp_disp_fl,CV_32FC1);
00081     disparities.SetDataAlign(tmp_disp_fl.ptr<float>(0), w, h);
00082 #else
00083     if(!disparities.Load(name)){
00084       std::cout << "Image " << name << " does not exist " << std::endl;
00085       exit(-1);
00086     }
00087 #endif  //USE_OPENCV
00088     TimerCPU timer(2800);
00089     /* Segmentation is executed on RGB image and disparity for 10 iterations.
00090      * If third argument is true, segmentation is initialised.
00091      */
00092     fgbgsegment.Execute(image, disparities, (i==fstidx), 10);
00093     if (i==fstidx)
00094        /* Default KTH: Initial Foreground in image center due to gaze shifts and fixation mechanism */
00095       fgbgsegment.SetNewForeground(w/2, h/2, disparities, drange);
00096     
00097     // Helper function to monitor segmentation and store intermediate results.
00098     std::cout << "Loop time: " << timer.read() << " ms" << std::endl;
00099     Image<uint8_t> segm(w, h);
00100     fgbgsegment.MakeSegmentImage(segm);
00101     sprintf(name, "segm%04dx.pgm", i);
00102     segm.Store(name, true, false); 
00103     fgbgsegment.MakeBorderImage(image);
00104     sprintf(name, "segm%04dx.ppm", i);
00105     image.StoreRGB(name);  
00106     Image<uint8_t> mask(w, h);
00107     fgbgsegment.MakeMaskImage(mask, 255, 1);
00108     sprintf(name, "mask%04dx.pgm", i);
00109     mask.Store(name);
00110     //if (i==lstidx) i = fstidx + 1;
00111   }
00112   return 0;
00113 }
00114 


active_realtime_segmentation
Author(s): Mårten Björkman. Maintained by Jeannette Bohg
autogenerated on Fri Jan 3 2014 12:02:50