Challenge

Project 1: Stereo Based Scene Segmentation

Week 5 challenge,project 1 uploaded under week 5

import numpy as np
import cv2
import glob
import sys
# import calibration_store
# import load_coefficients, save_stereo_coefficients

pathL = "./New folder/"
pathR = "./New folder1/"

# Termination criteria for refining the detected corners
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((9*6,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)

img_ptsL = []
img_ptsR = []
obj_pts = []

for i in range(1,2):
imgL = cv2.imread(pathL+"img%d.jpg"%i)
imgR = cv2.imread(pathR+"img%d.jpg"%i)
imgL_gray = cv2.imread(pathL+"img%d.jpg"%i,0)
imgR_gray = cv2.imread(pathR+"img%d.jpg"%i,0)

outputL = imgL.copy()
outputR = imgR.copy()

retR, cornersR = cv2.findChessboardCorners(outputR,(9,6),None)
retL, cornersL = cv2.findChessboardCorners(outputL,(9,6),None)

if retR and retL:
obj_pts.append(objp)
cv2.cornerSubPix(imgR_gray,cornersR,(11,11),(-1,-1),criteria)
cv2.cornerSubPix(imgL_gray,cornersL,(11,11),(-1,-1),criteria)
cv2.drawChessboardCorners(outputR,(9,6),cornersR,retR)
cv2.drawChessboardCorners(outputL,(9,6),cornersL,retL)
cv2.imshow('cornersR',outputR)
cv2.imshow('cornersL',outputL)
cv2.waitKey(0)

img_ptsL.append(cornersL)
img_ptsR.append(cornersR)
#Calibrating left camera
retL, mtxL, distL, rvecsL, tvecsL = cv2.calibrateCamera(obj_pts,img_ptsL,imgL_gray.shape[::-1],None,None)
hL,wL= imgL_gray.shape[:2]
print("MAtrix",mtxL)
new_mtxL, roiL= cv2.getOptimalNewCameraMatrix(mtxL,distL,(wL,hL),1,(wL,hL))

# Calibrating right camera
retR, mtxR, distR, rvecsR, tvecsR = cv2.calibrateCamera(obj_pts,img_ptsR,imgR_gray.shape[::-1],None,None)
hR,wR= imgR_gray.shape[:2]
print("Matrix",mtxR)
new_mtxR, roiR= cv2.getOptimalNewCameraMatrix(mtxR,distR,(wR,hR),1,(wR,hR))

flags = 0
flags |= cv2.CALIB_FIX_INTRINSIC
# Here we fix the intrinsic camara matrixes so that only Rot, Trns, Emat and Fmat are calculated.
# Hence intrinsic parameters are the same

criteria_stereo= (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# This step is performed to transformation between the two cameras and calculate Essential and Fundamenatl matrix
retS, new_mtxL, distL, new_mtxR, distR, Rot, Trns, Emat, Fmat = cv2.stereoCalibrate(obj_pts, img_ptsL, img_ptsR, new_mtxL, distL, new_mtxR, distR, imgL_gray.shape[::-1], criteria_stereo, flags)
print("Stereo MatrixL",new_mtxL)
print("Stereo MatrixL",new_mtxR)

#To find the disparity to depth matrix Q
rectify_scale= 1
rect_l, rect_r, proj_mat_l, proj_mat_r, Q, roiL, roiR= cv2.stereoRectify(new_mtxL, distL, new_mtxR, distR, imgL_gray.shape[::-1], Rot, Trns, rectify_scale,(0,0))
print("Q-Matrix",Q)

# import required libraries
import numpy as np
import cv2
from matplotlib import pyplot as plt

# read two input images as grayscale images
imgL = cv2.imread('im1.png',0)
imgR = cv2.imread('im2.png',0)

# Initiate and StereoBM object
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=5)
# compute the disparity map
disparity = stereo.compute(imgL,imgR)
plt.imshow(disparity,'gray')
plt.show()
disparity.shape
cv2.imwrite('disp.jpg',disparity)

#To find the depth using disparity and Q matrix
depth = cv2.reprojectImageTo3D(disparity, Q)
plt.imshow(depth)
cv2.imwrite('dp.png',depth)

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.