# Opencv depth estimation

**URL:** https://discourse.processing.org/t/opencv-depth-estimation/16705
**Category:** Libraries
**Created:** [December 29, 2019, 2:32pm UTC](https://discourse.processing.org/t/opencv-depth-estimation/16705 "2019-12-29T14:32:35Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![costachemadalin](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@costachemadalin](https://discourse.processing.org/u/costachemadalin)
#### Post date: [December 29, 2019, 2:32pm UTC](https://discourse.processing.org/t/opencv-depth-estimation/16705/1 "2019-12-29T14:32:35Z")

</div>

I am trying to get depth from 2 rectified images using openCV ,i took this code from here  
[opencv-depth](https://github.com/atduskgreg/opencv-processing/tree/master/examples/DepthFromStereo) . I want to understand how the program works ,but i couldn’t find any documentation.

```auto
import gab.opencv.*;
import org.opencv.core.Mat;
import org.opencv.calib3d.StereoBM;
import org.opencv.core.CvType;
import org.opencv.calib3d.StereoSGBM;

OpenCV ocvL, ocvR;
PImage imgL, imgR, depth1, depth2;

void setup() {

  imgL = loadImage("scene_l.jpg");
  imgR = loadImage("scene_r.jpg");
  ocvL = new OpenCV(this, imgL);

  ocvR = new OpenCV(this, imgR);

  size(768, 576);
  
  ocvL.gray();
  ocvR.gray();
  Mat left = ocvL.getGray();
  Mat right = ocvR.getGray();

  Mat disparity = OpenCV.imitate(left);

  StereoSGBM stereo = new StereoSGBM(0, 32, 3, 128, 256, 20, 16, 1, 100, 20, true);
  stereo.compute(left, right, disparity );

  Mat depthMat = OpenCV.imitate(left);
  disparity.convertTo(depthMat, depthMat.type());

  depth1 = createImage(depthMat.width(), depthMat.height(), RGB);
  ocvL.toPImage(depthMat, depth1);

  StereoBM stereo2 = new StereoBM();
  stereo2.compute(left, right, disparity );
  disparity.convertTo(depthMat, depthMat.type());

  depth2 = createImage(depthMat.width(), depthMat.height(), RGB);
  ocvL.toPImage(depthMat, depth2);
}

void draw() {
  image(imgL, 0, 0);
  image(imgR, imgL.width, 0);

  image(depth1, 0, imgL.height);
  image(depth2, imgL.width, imgL.height);

  fill(255, 0, 0);
  text("left", 10, 20);
  text("right", 10 + imgL.width, 20);
  text("stereo SGBM", 10, imgL.height + 20);
  text("stereo BM", 10 + imgL.width, imgL.height+ 20);
}

```

For this set of images it works very good  
[image\_l](https://ibb.co/KwptZjP)  
[image\_r](https://ibb.co/1L92YzB)  
[depth](https://ibb.co/TwmFZss)

If i run the code with another images i have a bad output  
[image\_l](https://ibb.co/48hkXQM)  
[image\_r](https://ibb.co/Hzvy2wf)  
[depth](https://ibb.co/gSf7865)

---

<div class="post-metadata">

### Author: ![musiciantriescoding](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/musiciantriescoding/32/7662_2.png) [@musiciantriescoding](https://discourse.processing.org/u/musiciantriescoding)
#### Post date: [January 1, 2020, 11:44am UTC](https://discourse.processing.org/t/opencv-depth-estimation/16705/2 "2020-01-01T11:44:28Z")

</div>

I was mucking around with openCV and had this issue as well, not just with this example. But generally, there are resources around if you look for openCV’s own documentation. Have you taken a look here: [https://docs.opencv.org/master/dd/d53/tutorial\_py\_depthmap.html](https://docs.opencv.org/master/dd/d53/tutorial_py_depthmap.html)

From my own experiences, it takes a lot of messing around with the values that openCV uses to compute. I think in this case, StereoSGBM would need to be messed with. Take a look here: [https://docs.opencv.org/3.4/d2/d85/classcv\_1\_1StereoSGBM.html](https://docs.opencv.org/3.4/d2/d85/classcv_1_1StereoSGBM.html)

Other than that, I’m not sure!
