# Stereo Depth Perception gone wrong

**URL:** https://forum.opencv.org/t/stereo-depth-perception-gone-wrong/2457
**Category:** C++
**Tags:** calib3d
**Created:** [March 29, 2021, 2:26pm UTC](https://forum.opencv.org/t/stereo-depth-perception-gone-wrong/2457 "2021-03-29T14:26:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![alpi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/alpi/32/1326_2.png) [@alpi](https://forum.opencv.org/u/alpi)
#### Post date: [March 29, 2021, 2:26pm UTC](https://forum.opencv.org/t/stereo-depth-perception-gone-wrong/2457/1 "2021-03-29T14:26:00Z")

</div>

I’ve build a system with 2 cameras (not webcams, something with a good mounting plate) I have them mounted on a 3d printed plate to keep them parallel in yaw and on a single plane in roll. Having a plate with known dimensions gives me the exact distance between cameras. The goal is to get a distance (x,y,z components) to a known object in a scene. I calibrated both cameras separately using chessboard pattern to get a camera matrix.  
Here is the overview of the steps I take:

1. grab a frame from both cameras and remove distortions
2. run yolo on a left image to get a bounding box of an object in a scene
3. create a pattern using prev calculated bounding box and run pattern match on a right image
4. triangulate using a single pixel(box center in the left image and matched area center in the right) with the following formula  
Z\_component = distance\_between\_cams\_mm \* horizontal\_focal\_distance\_from\_camera\_matrx /  
(offset\_from\_the\_optical\_center\_in\_left\_image - offset\_from\_the\_optical\_center\_in\_right\_image)  
**Question: horizontal\_focal\_distance\_from\_camera\_matrx \<— which cam should I use left/right??? they have the same lenses but focal distances from cal are slightly different**
5. Now that we have a distance between an object plane and a camera plane we can calculate horizontal and vertical distance components relative to the left camera:  
// X = offset\_from\_center\_x \* Z\_component / focal\_x  
// Y = offset\_from\_center\_y \* Z\_component / focal\_y

After all this work accuracy isn’t there, +/- 30 cm when an object is 2 meters away and I’m looking to get distance calculation on objects 8-10 metes away. Things need to improve.  
**Now the questions:**  
**1. How accurate do these systems get from your experience?**  
**2. Is there anything obvious that I might be doing wrong? Any steps that I’m missing? I found a stereo calibrate function in the opencv api, docs say it translates an image from left to right camera, not sure why would I need to do it.**

I know this might be a vouge question, but I have no one else to ask these questions besides this forum.

PS\>Please don’t point me to the Disparity Calculation tutorial on the opencv, it shows a trivial sample image and does not go into details on how to tune a disparity map. Plus I don’t need to get the depths of everything just a single object in the scene.

---

<div class="post-metadata">

### Author: ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)
#### Post date: [March 29, 2021, 3:46pm UTC](https://forum.opencv.org/t/stereo-depth-perception-gone-wrong/2457/2 "2021-03-29T15:46:16Z")

</div>

Hi,  
A book : [Multiple View Geometry in Computer Vision](https://www.cambridge.org/core/books/multiple-view-geometry-in-computer-vision/0B6F289C78B2B23F596CAA76D3D43F7A)
