# Stereo rectify question

**URL:** <https://forum.opencv.org/t/stereo-rectify-question/2889>\
**Category:** Uncategorized\
**Tags:** calib3d, stereo\
**Created:** [April 21, 2021, 7:07am UTC](https://forum.opencv.org/t/stereo-rectify-question/2889 "2021-04-21T07:07:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nistarlwc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nistarlwc/32/1874_2.png) [@nistarlwc](https://forum.opencv.org/u/nistarlwc)\
**Post date:** [April 21, 2021, 7:07am UTC](https://forum.opencv.org/t/stereo-rectify-question/2889/1 "2021-04-21T07:07:20Z")

</div>

When use Matlab calibrate and rectify, img size change from 2592 x 1944 to 8805 x 2188.  
But do not know how to realize this.  
I think that the parameter of “stereoRectify” function, but how to do?  
Can self-adaptive image size?

---

<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:** [April 21, 2021, 7:40am UTC](https://forum.opencv.org/t/stereo-rectify-question/2889/2 "2021-04-21T07:40:29Z")

</div>

sorry, but this is the opencv site, we cannot help with matlab problems

---

<div class="post-metadata">

**Author:** ![nistarlwc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nistarlwc/32/1874_2.png) [@nistarlwc](https://forum.opencv.org/u/nistarlwc)\
**Post date:** [May 26, 2021, 8:04am UTC](https://forum.opencv.org/t/stereo-rectify-question/2889/3 "2021-05-26T08:04:01Z")

</div>

Sorry, the previous description is not clear, this is not a problem with Matlab.

Image original size is 2592x1944.  
After rectifying, its size should be 8805 x 2188, it is result of Matlab.

result correct of left image:

 ![垂直相机1_rectify](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/ca4f901aa1dfc81d15fff68039121662bb25ff16.jpeg)

Below is OpenCV code：

```python
image_size = (1944, 2592)
R1, R2, P1, P2, Q, roi_left, roi_right = cv2.stereoRectify(K1, D1, K2, D2, image_size, R, T, 
                flags=cv2.CALIB_ZERO_DISPARITY, alpha=-1)
leftMapX, leftMapY = cv2.initUndistortRectifyMap(K1, D1, R1, P1, image_size, cv2.CV_32FC1)
left_rectified = cv2.remap(left, leftMapX, leftMapY, cv2.INTER_LINEAR, cv2.BORDER_TRANSPARENT) 
rightMapX, rightMapY = cv2.initUndistortRectifyMap(K2, D2, R2, P2, image_size, cv2.CV_32FC1)
right_rectified = cv2.remap(right, rightMapX, rightMapY, cv2.INTER_LINEAR, cv2.BORDER_TRANSPARENT) 

```

The results are errors.

In cv2.stereoRectify, there is a parameter “newImageSize”.  
If set newImageSize = (8805, 2188). the results are correct.  
But roi\_left, roi\_right are erros.

Now, I would know:  
How to calculate the newImageSize for self-adaptive image size?  
How to calculate the effective ROI of the rectified image? For the next step， stereo matching.

---

<div class="post-metadata">

**Author:** ![nistarlwc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nistarlwc/32/1874_2.png) [@nistarlwc](https://forum.opencv.org/u/nistarlwc)\
**Post date:** [May 26, 2021, 8:04am UTC](https://forum.opencv.org/t/stereo-rectify-question/2889/4 "2021-05-26T08:04:42Z")

</div>

result error of left image:

 ![垂直相机1_矫正](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/a581ec66e5b3ed15ff41dc291753cd9b7b557fcf.jpeg)

---

<div class="post-metadata">

**Author:** ![nistarlwc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nistarlwc/32/1874_2.png) [@nistarlwc](https://forum.opencv.org/u/nistarlwc)\
**Post date:** [May 26, 2021, 8:08am UTC](https://forum.opencv.org/t/stereo-rectify-question/2889/5 "2021-05-26T08:08:03Z")

</div>

set newImageSize = (8805, 2188). the results are correct:

 ![垂直相机1_矫正](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/6e9145eee046524bfce17ef6b27daeecf3205e5e.jpeg)

---

<div class="post-metadata">

**Author:** ![wind\_Z](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/wind_z/32/10186_2.png) [@wind\_Z](https://forum.opencv.org/u/wind_Z)\
**Post date:** [April 12, 2024, 2:48am UTC](https://forum.opencv.org/t/stereo-rectify-question/2889/6 "2024-04-12T02:48:23Z")

</div>

hello, i have met the same problems. Do you have any ideas on this issue? I found that the P1,P2 are different with the cv2.stereoRectify in python and rectifyStereoImages in matlab. I used the same binocular camera parameters and it was very confusing for me.
