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
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 FgBgSegment fgbgsegment(w, h, drange, 50.0);
00044 #ifdef USE_CUDA
00045 fgbgsegment.UseGPU(false);
00046 #else
00047 fgbgsegment.UseGPU(false);
00048 #endif //USE_CUDA
00049 Image<uint8_t> image(3*w, h);
00050 Image<float> disparities(w, h);
00051 for (int i=fstidx;i<=lstidx;i++) {
00052 char name[80];
00053 sprintf(name, image_format, i);
00054 #ifdef USE_OPENCV
00055 cv::Mat tmp_img = cv::imread(name, 1);
00056 if(tmp_img.empty()) {
00057 std::cout << "Image " << name << " does not exist " << std::endl;
00058 exit(-1);
00059 }
00060 cv::cvtColor(tmp_img, tmp_img, CV_BGR2RGB);
00061 image.SetDataAlign(tmp_img.ptr<uchar>(0), 3*w, h);
00062 #else
00063 if(!image.LoadRGB(name)){
00064 std::cout << "Image " << name << " does not exist " << std::endl;
00065 exit(-1);
00066 }
00067 #endif //USE_OPENCV
00068 sprintf(name, disp_format, i);
00069 #ifdef USE_OPENCV
00070 cv::Mat tmp_disp = cv::imread(name, 0);
00071 if(tmp_disp.empty()) {
00072 std::cout << "Image " << name << " does not exist " << std::endl;
00073 exit(-1);
00074 }
00075 cv::Mat tmp_disp_fl;
00076 tmp_disp.convertTo(tmp_disp_fl,CV_32FC1);
00077 disparities.SetDataAlign(tmp_disp_fl.ptr<float>(0), w, h);
00078 #else
00079 if(!disparities.Load(name)){
00080 std::cout << "Image " << name << " does not exist " << std::endl;
00081 exit(-1);
00082 }
00083 #endif //USE_OPENCV
00084 TimerCPU timer(2800);
00085 fgbgsegment.Execute(image, disparities, (i==fstidx), 10);
00086 if (i==fstidx)
00087 fgbgsegment.SetNewForeground(w/2, h/2, disparities, drange);
00088
00089 std::cout << "Loop time: " << timer.read() << " ms" << std::endl;
00090 Image<uint8_t> segm(w, h);
00091 fgbgsegment.MakeSegmentImage(segm);
00092 sprintf(name, "segm%04dx.pgm", i);
00093 segm.Store(name, true, false);
00094 fgbgsegment.MakeBorderImage(image);
00095 sprintf(name, "segm%04dx.ppm", i);
00096 image.StoreRGB(name);
00097 Image<uint8_t> mask(w, h);
00098 fgbgsegment.MakeMaskImage(mask, 255, 1);
00099 sprintf(name, "mask%04dx.pgm", i);
00100 mask.Store(name);
00101
00102 }
00103 return 0;
00104 }
00105