Commits

Dan Strother committed 4b3799a

added scaling and cropping

  • Participants
  • Parent commits e36e2df

Comments (0)

Files changed (1)

File stereo/sim/dlsc_stereobm_models_program.cpp

     bool use_readmemh;
     int width;
     int height;
+    bool scale;
     
     po::options_description desc("Allowed options");
     desc.add_options()
         ("readmemh",        po::value<bool>(&use_readmemh)->default_value(false),       "Use Verilog $readmemh format for output")
         ("width",           po::value<int>(&width)->default_value(-1),                  "Width of output image")
         ("height",          po::value<int>(&height)->default_value(-1),                 "Height of output image")
+        ("scale",           po::value<bool>(&scale)->default_value(false),              "Scale input images")
     ;
     
     po::variables_map vm;
         return 1;
     }
 
+    if(il.size() != ir.size()) {
+        std::cerr << "left and right images must have save size" << std::endl;
+        return 1;
+    }
+
     if(width <=0) width  = il.cols;
     if(height<=0) height = il.rows;
 
+    if(scale && (il.cols != width || il.rows != height)) {
+        cv::Mat ils = il.clone();
+        cv::Mat irs = ir.clone();
+
+        int w,h;
+        float src_aspect = 1.0*il.cols/il.rows;
+        float aspect = 1.0*width/height;
+
+        if( src_aspect > aspect ) {
+            h = height;
+            w = (int)(height*src_aspect);
+        } else {
+            w = width;
+            h = (int)(width/src_aspect);
+        }
+
+        cv::resize(ils,il,cv::Size(w,h));
+        cv::resize(irs,ir,cv::Size(w,h));
+    }
+
+    if(il.cols < width || il.rows < height) {
+        std::cerr << "source image size must be >= output size (try --scale)" << std::endl;
+        return 1;
+    }
+
+    if(il.cols != width || il.rows != height) {
+        // take central crop
+        int x   = (il.cols/2) - (width/2);
+        int dx  = x + width;
+        int y   = (il.rows/2) - (height/2);
+        int dy  = y + height;
+        il      = il(cv::Range(y,dy),cv::Range(x,dx));
+        ir      = ir(cv::Range(y,dy),cv::Range(x,dx));
+        assert(il.cols == width && il.rows == height);
+    }
+
     cv::Mat ilf = il.clone();
     cv::Mat irf = ir.clone();