# Buggy remapped image after stereo rectify

**URL:** <https://forum.opencv.org/t/buggy-remapped-image-after-stereo-rectify/4594>\
**Category:** Uncategorized\
**Tags:** calib3d\
**Created:** [August 4, 2021, 12:30pm UTC](https://forum.opencv.org/t/buggy-remapped-image-after-stereo-rectify/4594 "2021-08-04T12:30:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiapei100](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/jiapei100/32/2661_2.png) [@jiapei100](https://forum.opencv.org/u/jiapei100)\
**Post date:** [August 4, 2021, 12:30pm UTC](https://forum.opencv.org/t/buggy-remapped-image-after-stereo-rectify/4594/1 "2021-08-04T12:30:41Z")

</div>

1. Image effects

- Original Left Image:  

- Original Right Image  

- Remapped Nice Image After Stereo Rectification  

1. Code

- My code for stereo calibration is located at [calibrate.py](https://github.com/spmallick/learnopencv/blob/master/stereo-camera/calibrate.py)

- My code for demonstrating the depth image is located at  
[disparity2depth\_calib.py](https://github.com/spmallick/learnopencv/blob/master/Depth-Perception-Using-Stereo-Camera/python/disparity2depth_calib.py)

So, did anybody successfully obtain the remapped image after stereo rectify?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 4, 2021, 4:43pm UTC](https://forum.opencv.org/t/buggy-remapped-image-after-stereo-rectify/4594/2 "2021-08-04T16:43:34Z")

</div>

welcome.

I see darkness. do you really expect to be able to work with that data?

---

<div class="post-metadata">

**Author:** ![Charles\_R](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/charles_r/32/2757_2.png) [@Charles\_R](https://forum.opencv.org/u/Charles_R)\
**Post date:** [August 5, 2021, 3:32pm UTC](https://forum.opencv.org/t/buggy-remapped-image-after-stereo-rectify/4594/3 "2021-08-05T15:32:58Z")

</div>

I am working on a similar project and have not yet obtained the results I’m looking for. You can at least get rid of the black space by cropping the image. You might also try playing around with the free scaling parameter when rectifying.

From [https://docs.opencv.org/3.4/d9/d0c/group\_\_calib3d.html:](https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html:) “Free scaling parameter. If it is -1 or absent, the function performs the default scaling. Otherwise, the parameter should be between 0 and 1. alpha=0 means that the rectified images are zoomed and shifted so that only valid pixels are visible (no black areas after rectification). alpha=1 means that the rectified image is decimated and shifted so that all the pixels from the original images from the cameras are retained in the rectified images (no source image pixels are lost). Any intermediate value yields an intermediate result between those two extreme cases.”

---

<div class="post-metadata">

**Author:** ![Charles\_R](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/charles_r/32/2757_2.png) [@Charles\_R](https://forum.opencv.org/u/Charles_R)\
**Post date:** [August 6, 2021, 12:48am UTC](https://forum.opencv.org/t/buggy-remapped-image-after-stereo-rectify/4594/4 "2021-08-06T00:48:08Z")

</div>

I found a solution that works for my project. Perhaps it will work for yours as well. I changed my free scaling parameter from 1 to 0.5 in the matrix optimization and it almost entirely eliminated the problem.

In your code, try changing line 58 from:  
new\_mtxR, roiR= cv2.getOptimalNewCameraMatrix(mtxR,distR,(wR,hR),1,(wR,hR))  
to:  
new\_mtxR, roiR= cv2.getOptimalNewCameraMatrix(mtxR,distR,(wR,hR),0.5,(wR,hR))

See if it helps your issue. BTW, it would still be a good idea to crop the output using the ROI.

---

<div class="post-metadata">

**Author:** ![jiapei100](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/jiapei100/32/2661_2.png) [@jiapei100](https://forum.opencv.org/u/jiapei100)\
**Post date:** [August 7, 2021, 7:29am UTC](https://forum.opencv.org/t/buggy-remapped-image-after-stereo-rectify/4594/5 "2021-08-07T07:29:28Z")

</div>

> [@Charles\_R](#):
>
> getOptimalNewCameraMatrix

I tried **0.5** and **0.2** , still problematic.

Just wonder if `cv2.getOptimalNewCameraMatrix` is able to handle gray images (nChannels=1, rather than 3) ?

I’m quite sure my `imgL_gray` is correct. Therefore, the **ONLY** factor causing `Left_nice` buggy is due to **Left\_Stereo\_Map\_x** and **Left\_Stereo\_Map\_y**.

```python
		Left_nice= cv2.remap(imgL_gray,
							Left_Stereo_Map_x,
							Left_Stereo_Map_y,
							cv2.INTER_LANCZOS4,
							cv2.BORDER_CONSTANT,
							0)

```

So, `params_py.xml` in file [calibrate.py](https://github.com/spmallick/learnopencv/blob/master/stereo-camera/calibrate.py) is **EXACTLY the same as** `stereo_rectify_maps.xml` in file [disparity2depth\_calib.py](https://github.com/spmallick/learnopencv/blob/master/Depth-Perception-Using-Stereo-Camera/python/disparity2depth_calib.py), right?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 7, 2021, 7:34am UTC](https://forum.opencv.org/t/buggy-remapped-image-after-stereo-rectify/4594/6 "2021-08-07T07:34:19Z")

</div>

> [@jiapei100](#):
>
> Just wonder if `cv2.getOptimalNewCameraMatrix` is able to handle gray images (nChannels=1, rather than 3) ?

why are you wondering ? [getOptimalNewCameraMatrix](https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga7a6c4e032c97f03ba747966e6ad862b1) is not dealing with image channels at all

---

<div class="post-metadata">

**Author:** ![jiapei100](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/jiapei100/32/2661_2.png) [@jiapei100](https://forum.opencv.org/u/jiapei100)\
**Post date:** [August 7, 2021, 7:51am UTC](https://forum.opencv.org/t/buggy-remapped-image-after-stereo-rectify/4594/7 "2021-08-07T07:51:03Z")

</div>

Thank you @berak . But, why my `Left_nice` is **NOT nice at all**?

---

<div class="post-metadata">

**Author:** ![Charles\_R](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/charles_r/32/2757_2.png) [@Charles\_R](https://forum.opencv.org/u/Charles_R)\
**Post date:** [September 15, 2021, 1:06am UTC](https://forum.opencv.org/t/buggy-remapped-image-after-stereo-rectify/4594/8 "2021-09-15T01:06:09Z")

</div>

Just a thought: there are separate free scaling parameters for both the calibration and for the stereo rectification. Did you try reducing the value on both?
