OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sat, 04 Apr 2020 11:09:11 -0500Finding depth of an object using 2 camerashttp://answers.opencv.org/question/228501/finding-depth-of-an-object-using-2-cameras/Hi,
I am new to stereo imaging and learning to find depth of an object.
I have 2 cameras kept separately looking at a cardboard surface.
**Given** :
- 8 points marked on the cardboard surface.
- Captured one image each from both the camera.
- Identified (x,y) coordinates of all 8 points in both the images.
**Problem** : Find the depth of each point i.e. distance of each point from the cameras.
*I tried solving it using following approach but I got weird result* :
1. Noted down 8 common points from both the left and right images captured from 2 different cameras.
2. Determined Fundamental Matrix between both the images using 8 points.
3. The fundamental matrix F relates the points on the image plane of one camera in image coordinates (pixels) to the points on the image plane of the other camera in image coordinates
- Opencv Function : cv::findFundamentalMat()
- Input to the function : 8 common points from both the image
- Output = Fundamental matrix of 3x3
4. Performed stereo rectification
- It reprojects the image planes of our two cameras so that they reside in the exact same plane, with image rows perfectly aligned into a frontal parallel configuration.
- Opencv Function : cv::stereoRectifyUncalibrated()
- Input to the function : 8 common Points from the images and fundamental matrix
- Output = Rectification matrices H1 and H2 for both the images.
5. Determined depth of a point
- Trying to find the depth of a point which is aprox. 39 feet (468 inches) away from the camera.
- Formula to find depth is Z = (f * T) / (xl – xr)
- Z is depth, f is focal lenth, T is distance between camera, xl and xr are x coordinate of a point on left image and right image respectively.
- Following are the values taken for the variables :
- f = From the determined camera intrinsic of the camera, I got fx and fy. So I found out f = sqrt(fx*fx + fy*fy)
- T = 2 cameras are kept apart 36 feet i.e. 432 inches. So, I gave T = 432
- xl and xr are x values of the point from left and right images which are perspective transformed using rectification matrices H1 and H2.
- But I got very weird result.
You can look at the screenshot of my experimentation and result.
![image description](/upfiles/158601511677758.png)
So could someone tell me the approach I am taking is right or wrong ?cvsolverSat, 04 Apr 2020 11:09:11 -0500http://answers.opencv.org/question/228501/StereoBM CPU and StereoBM GPU versions on OpenCV give very different output.http://answers.opencv.org/question/185481/stereobm-cpu-and-stereobm-gpu-versions-on-opencv-give-very-different-output/I find a huge difference between the outputs of the CPU and GPU versions of StereoBM.
I have compiled a GPU version of OpenCV 3.2 on my machine.
I am attaching the outputs of both CPU and GPU, along with the GPU code.
StereoBM GPU
![StereoBM GPU](/upfiles/15196502069787625.png)
StereoBM CPU
![StereoBM CPU](/upfiles/15196501846851267.png)
Here is the code:
#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/cudaarithm.hpp"
#include <iostream>
#include <stdio.h>
#include <limits.h>
using namespace cv;
int main()
{
cv::Mat img1, img2;
img1 = imread( "~/left.png", cv::IMREAD_GRAYSCALE);
img2 = imread( "~/right.png", cv::IMREAD_GRAYSCALE);
cuda::GpuMat d_left, d_right;
Ptr<cuda::StereoBM> bm;
bm = cuda::createStereoBM(64, 11); //numDisparities=64, windowSize=11
Mat disp(img1.size(), CV_8U);
cuda::GpuMat d_disp(img1.size(), CV_8U);
cuda::GpuMat d_disp_color(img1.size(), CV_8U);
d_left.upload(img1);
d_right.upload(img2);
bm->compute(d_right, d_left, d_disp);
cuda::drawColorDisp(d_disp, d_disp_color, 64);
d_disp.download(disp);
imwrite("~/disparity.png", disp);
d_disp_color.download(disp);
imwrite("~/disparity_color.png", disp);
return 0;
}
The Output of StereoBM CPU is obtained with the same parameters as StereoBM GPU.
In addition to that, StereoBM CPU has following parameters:
> preFilterSize = 9,
> preFilterCap = 31,
> wsize = 11,
> numDisparities = 64,
> minDisparity = 0,
> uniquenessRatio = 0,
> textureThreshold = 0,
> speckleWindowSize = 400,
> speckleRange = 4,
> disp12MaxDiff = 0.sap89750@gmail.comMon, 26 Feb 2018 07:14:47 -0600http://answers.opencv.org/question/185481/