# Calibration , disparity map

**URL:** <https://forum.opencv.org/t/calibration-disparity-map/12054>\
**Category:** Python\
**Tags:** calib3d\
**Created:** [February 20, 2023, 2:22am UTC](https://forum.opencv.org/t/calibration-disparity-map/12054 "2023-02-20T02:22:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mostafa\_Mohamed](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mostafa_mohamed/32/7218_2.png) [@Mostafa\_Mohamed](https://forum.opencv.org/u/Mostafa_Mohamed)\
**Post date:** [February 20, 2023, 2:22am UTC](https://forum.opencv.org/t/calibration-disparity-map/12054/1 "2023-02-20T02:22:43Z")

</div>

hello , i have been trying for long time to make 2 script , 1- stereo calibration , 2- generate disparty map , anyway when i genereate ply file when i measure it in meashlab it gives me wrong numbers for exampe my chessboard is 2.5 mm square , i get in meshlab 86.9 something like that , so i thought maybe the ply i generate dont do mm maybe pixels , then i make it in mm it gave me wrong size too , so i used a scale factor but when i fix a scale factor at a distance lets say 10 cm away scanned object , then use the same object but at 11 cm away , it does not give me the real size anymore , i am so tired so kindly someone help me please

---

<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:** [February 20, 2023, 10:20am UTC](https://forum.opencv.org/t/calibration-disparity-map/12054/2 "2023-02-20T10:20:21Z")

</div>

“disparity” is a screen-space measure, in pixels.

to get 3D points, you have to `reprojectImageTo3D()`

[https://docs.opencv.org/4.x/d9/d0c/group\_\_calib3d.html#ga1bc1152bd57d63bc524204f21fde6e02](https://docs.opencv.org/4.x/d9/d0c/group__calib3d.html#ga1bc1152bd57d63bc524204f21fde6e02)

---

<div class="post-metadata">

**Author:** ![Mostafa\_Mohamed](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mostafa_mohamed/32/7218_2.png) [@Mostafa\_Mohamed](https://forum.opencv.org/u/Mostafa_Mohamed)\
**Post date:** [February 20, 2023, 12:54pm UTC](https://forum.opencv.org/t/calibration-disparity-map/12054/3 "2023-02-20T12:54:55Z")

</div>

the steps i used in disparty

1. Load the left and right images from the specified paths using `cv2.imread()`, `np.load` for calibration files
2. Undistort the loaded images using `cv2.undistort()` and the intrinsic camera matrices and distortion coefficients.
3. Convert the undistorted images to grayscale using `cv2.cvtColor()`.
4. Create a window to display the disparity map using `cv2.namedWindow()`.
5. Create a `StereoSGBM` object with default parameters using `cv2.StereoSGBM_create()`.
6. Compute the disparity map using `StereoSGBM.compute()` with the grayscale left and right images as inputs.
7. Normalize the disparity map to an 8-bit image using `cv2.normalize()`.
8. Display the disparity map using `cv2.imshow()`.
9. Define a 4x4 projection matrix `Q1` for the reprojected 3D points.
10. Use `cv2.reprojectImageTo3D()` to create a 3D point cloud from the disparity map and projection matrix.
11. Load the left image again using `cv2.imread()` to extract its colors.
12. Reshape the 3D point cloud and color arrays and concatenate them to create a single `vertices` array.
13. Remove any points with `NaN` values and save the remaining points and colors as a PLY file using a custom `create_output()` function.

---

<div class="post-metadata">

**Author:** ![Mostafa\_Mohamed](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mostafa_mohamed/32/7218_2.png) [@Mostafa\_Mohamed](https://forum.opencv.org/u/Mostafa_Mohamed)\
**Post date:** [February 20, 2023, 12:57pm UTC](https://forum.opencv.org/t/calibration-disparity-map/12054/4 "2023-02-20T12:57:38Z")

</div>

i can share my script if you want to take a look on it

---

<div class="post-metadata">

**Author:** ![Mostafa\_Mohamed](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mostafa_mohamed/32/7218_2.png) [@Mostafa\_Mohamed](https://forum.opencv.org/u/Mostafa_Mohamed)\
**Post date:** [February 20, 2023, 1:33pm UTC](https://forum.opencv.org/t/calibration-disparity-map/12054/5 "2023-02-20T13:33:59Z")

</div>

the steps for calibration

1-Import necessary libraries: OpenCV and NumPy  
2-Set up some parameters for the chessboard used for calibration, including its size, the size of each square on the board, and the desired frame size for the images.  
3-Initialize empty lists for storing image points and object points for both the left and right cameras.  
4-Create an array of object points that represents the 3D coordinates of the corners of the chessboard in the real world.  
5-Use the cv2.findChessboardCorners() function to detect the corners of the chessboard in each image.  
6-If the corners are detected in an image, refine their positions using cv2.cornerSubPix().  
7-Draw the detected corners onto the image using cv2.drawChessboardCorners().  
8-Concatenate the left and right images with detected corners side-by-side, and display the result using cv2.imshow().  
9-Repeat steps 5-8 for each image in the dataset.  
10-Use cv2.calibrateCamera() to calibrate the left and right cameras separately using the image and object points.  
11-Use cv2.stereoCalibrate() to calibrate the stereo camera system using the object and image points from both the left and right cameras, as well as the calibration parameters from the individual cameras.  
12-Save the calibration parameters for both the left and right cameras, as well as the rotation and translation matrices for the stereo camera system, to separate .npy files.

---

<div class="post-metadata">

**Author:** ![Mostafa\_Mohamed](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mostafa_mohamed/32/7218_2.png) [@Mostafa\_Mohamed](https://forum.opencv.org/u/Mostafa_Mohamed)\
**Post date:** [February 20, 2023, 1:39pm UTC](https://forum.opencv.org/t/calibration-disparity-map/12054/6 "2023-02-20T13:39:14Z")

</div>

my camera rig set up , they are micro cameras away from each other like 10 mm , and they are parraler to each other in X and Y Axis , they are not angled maybe this cause the problem so i need to angel one of them ?
