Stereo with openCV

548 views Asked by At

I try to find the distance between camera and an object,so I use two different cameras (stereo). I use the program in opencv samples .

#include "opencv2/core/core.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/contrib/contrib.hpp"
#include <stdio.h>
#include <string.h>
#include<iostream>

using namespace cv;

const char *windowDisparity = "Disparity";



int main(  )
{
Size S(640,360);
Mat threshold2,threshold1;
  Mat imgLeft = imread( "lift.jpg", CV_LOAD_IMAGE_GRAYSCALE );
  Mat imgRight = imread("right.jpg", CV_LOAD_IMAGE_GRAYSCALE );
  resize(imgLeft,imgLeft,S);

  Mat imgDisparity16S = Mat( imgLeft.rows, imgLeft.cols, CV_16S );
  Mat imgDisparity8U = Mat( imgRight.rows, imgRight.cols, CV_8UC1 );

  int ndisparities = 16*5;
  int SADWindowSize = 21;

  StereoBM sbm( StereoBM::BASIC_PRESET,ndisparities,SADWindowSize );

  sbm( imgLeft, imgRight, imgDisparity16S, CV_16S );

  double minVal; double maxVal;

  minMaxLoc( imgDisparity16S, &minVal, &maxVal );

  printf("Min disp: %f Max value: %f \n", minVal, maxVal);

  imgDisparity16S.convertTo( imgDisparity8U, CV_8UC1, 255/(maxVal - minVal));

  namedWindow( windowDisparity, WINDOW_NORMAL );
  imshow( windowDisparity, imgDisparity8U );
  imshow( "left", imgLeft );
  imshow( "right", imgRight );

  imwrite("SBM_sample.png", imgDisparity16S);

  waitKey(0);

  return 0;
}

my problem is that I can't find the depth Z between object and camera

1

There are 1 answers

0
morynicz On

Maybe try to convert disparity to 3D points using cv::reprojectImageTo3D() or cv::perspectiveTransform() for sparse set of points.