lines1 = cv2.computeCorrespondEpilines(pts_2.reshape(-1,1,2), 2,F) lines1 = lines1.reshape(-1,3) img5,img6 = drawlines(img1,img2,lines1,pts_1[:10],pts_2[:10]) # Find epilines corresponding to points in left image (first image) and # drawing its lines on right image lines2 = cv2.computeCorrespondEpilines(pts_1.reshape(-1,1,2), 1,F) lines2 = lines2.reshape(-1,3) img3,img4 = drawlines(img2,img1,lines2,pts_2[:10],pts_1[:10]) cv2.imwrite('task2_epi_right.jpg',img5) cv2.imwrite('task2_epi_left.jpg',img3) #Task 1.4 stereoMatcher = cv2.StereoBM_create(numDisparities=80, blockSize=15) imgL=cv2.cvtColor(img1,cv2.COLOR_RGB2GRAY) imgR=cv2.cvtColor(img2,cv2.COLOR_RGB2GRAY) disparity_img = stereoMatcher.compute(imgL,imgR) largest_num = disparity_img[0][0] for row_idx, row in enumerate(disparity_img): for col_idx, num in enumerate(row): if num > largest_num: largest_num = num large_val = largest_num row = disparity_img.shape[0] col = disparity_img.shape[1]
import numpy as np import cv2 import time import tkinter as tk import StereoImg #feed2,cap2 = L #feed1,cap1 = R cap1 = cv2.VideoCapture(0) cap2 = cv2.VideoCapture(1) stereo = cv2.StereoBM_create(numDisparities=128, blockSize=5) #stereo = StereoImg.Stereo(16,6 * 16,16,3,61,1,10,100,32,False) ''' window_size = 3 min_disp = 16 num_disp = 112-min_disp stereo = cv2.StereoSGBM_create(minDisparity = min_disp, numDisparities = num_disp, blockSize = 16, P1 = 8*1*window_size**2, P2 = 32*1*window_size**2, disp12MaxDiff = 1, uniquenessRatio = 10, speckleWindowSize = 100, speckleRange = 32 ) ''' ''' #create sliders master_ui = tk.Tk() #minDisparity w1 = tk.Scale(master_ui, from_=0, to=100, length = 350, label = 'minDisparity', orient=tk.HORIZONTAL, command=stereo.updateMinDisparity)
import numpy as np import cv2 from matplotlib import pyplot as plt imgL = cv2.imread('tsukuba_l.png', 0) #print(type(imgL)) #<class 'numpy.ndarray'> imgR = cv2.imread('tsukuba_r.png', 0) #stereo = cv2.createStereoBM(numDisparities=16, blockSize=15) # SO Says - StereoBM_create -- https://stackoverflow.com/questions/54991771/attributeerror-module-cv2-has-no-attribute-createstereobm stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15) disparity = stereo.compute(imgL, imgR) plt.imshow(disparity, 'gray') plt.show()
prefilterSize = cv2.getTrackbarPos('3','image') + 5 prefilterCap = cv2.getTrackbarPos('4','image') + 1 textureThreshold = cv2.getTrackbarPos('5','image') speckleWindowSize = cv2.getTrackbarPos('6','image') speckleRange = cv2.getTrackbarPos('7','image') uniquenessRatio = cv2.getTrackbarPos('8','image') if (blockSize % 2) == 0: blockSize = blockSize +1 if (prefilterSize % 2) == 0: prefilterSize = prefilterSize +1 time = 0 for i in range(10): start = timer() stereobm = cv2.StereoBM_create(numDisparities,blockSize) stereobm.setPreFilterSize(prefilterSize) stereobm.setPreFilterType(cv2.STEREO_BM_PREFILTER_NORMALIZED_RESPONSE) stereobm.setPreFilterCap(prefilterCap) stereobm.setTextureThreshold(textureThreshold) stereobm.setSpeckleWindowSize(speckleWindowSize) stereobm.setSpeckleRange(speckleRange) stereobm.setUniquenessRatio(uniquenessRatio) realLeft = cv2.remap(imageLeft0, xMapLeft, yMapLeft, cv2.INTER_LINEAR) realRight = cv2.remap(imageRight0, xMapRight, yMapRight, cv2.INTER_LINEAR) realLeft = realLeft[:(HEIGHT-10), :] realRight = realRight[10:, :] imageLeft = cv2.cvtColor(realLeft, cv2.COLOR_BGR2GRAY)
right = cv2.imread('C:/Users/harshak/Documents/GitHub/U3TL0002_001_5d.jpg') # Increase the resolution # The distortion in the left and right edges prevents a good calibration, so # discard the edges CROP_WIDTH = 960 def cropHorizontal(image): return image[:, int((CAMERA_WIDTH-CROP_WIDTH)/2): int(CROP_WIDTH+(CAMERA_WIDTH-CROP_WIDTH)/2)] # TODO: Why these values in particular? # TODO: Try applying brightness/contrast/gamma adjustments to the images stereoMatcher = cv2.StereoBM_create() stereoMatcher.setMinDisparity(4) stereoMatcher.setNumDisparities(128) stereoMatcher.setBlockSize(21) stereoMatcher.setROI1(leftROI) stereoMatcher.setROI2(rightROI) stereoMatcher.setSpeckleRange(16) stereoMatcher.setSpeckleWindowSize(45) # Grab both frames first, then retrieve to minimize latency between cameras leftHeight, leftWidth = left.shape[:2] rightHeight, rightWidth = right.shape[:2]
pts2 = pts2[mask.ravel() == 1] print("fundamental matrix:") print(F) ##question 3 # Find epilines corresponding to points in right image (second image) and # drawing its lines on left image lines1 = cv2.computeCorrespondEpilines(pts2.reshape(-1, 1, 2), 2, F) lines1 = lines1.reshape(-1, 3) img5, img6 = drawlines(img1, img2, lines1, pts1, pts2, seed=seed) # Find epilines corresponding to points in left image (first image) and # drawing its lines on right image lines2 = cv2.computeCorrespondEpilines(pts1.reshape(-1, 1, 2), 1, F) lines2 = lines2.reshape(-1, 3) img3, img4 = drawlines(img2, img1, lines2, pts2, pts1, seed=seed) cv2.imwrite('../task2_img/task2_epi_left.jpg', img5) cv2.imwrite('../task2_img/task2_epi_right.jpg', img3) ## question 4 stereo = cv2.StereoBM_create(numDisparities=64, blockSize=25) disparity = stereo.compute(img1_gray, img2_gray) norm_image = cv2.normalize(disparity, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) cv2.imwrite('../task2_img/task2_disparity.jpg', (norm_image * 255).astype(np.uint8))
# Find epilines corresponding to points in right image (second image) and # drawing its lines on left image lines1 = cv2.computeCorrespondEpilines(pts2.reshape(-1, 1, 2), 2, F) lines1 = lines1.reshape(-1, 3) img5, img6 = drawlines(img1, img2, lines1, pts1, pts2) # Find epilines corresponding to points in left image (first image) and # drawing its lines on right image lines2 = cv2.computeCorrespondEpilines(pts1.reshape(-1, 1, 2), 1, F) lines2 = lines2.reshape(-1, 3) img3, img4 = drawlines(img2, img1, lines2, pts2, pts1) f, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 9)) f.tight_layout() ax1.imshow(img5) ax1.set_title('Left Image', fontsize=30) ax2.imshow(img3) ax2.set_title('Right Image', fontsize=30) plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.) plt.suptitle('Epilines') plt.show() img1 = cv2.imread('left.png', 0) #queryimage # left image img2 = cv2.imread('right.png', 0) #trainimage # right image stereo = cv2.StereoBM_create(32, 7) disparity = stereo.compute(img1, img2) plt.imshow(disparity, 'gray') plt.suptitle('Disparity Map') plt.show()
rightMapY = calibration["rightMapY"] Q = calibration["disparityToDepthMap"] CAMERA_WIDTH = 640 CAMERA_HEIGHT = 480 left = cv2.VideoCapture(0) right = cv2.VideoCapture(1) left.set(cv2.CAP_PROP_FRAME_WIDTH, CAMERA_WIDTH) left.set(cv2.CAP_PROP_FRAME_HEIGHT, CAMERA_HEIGHT) right.set(cv2.CAP_PROP_FRAME_WIDTH, CAMERA_WIDTH) right.set(cv2.CAP_PROP_FRAME_HEIGHT, CAMERA_HEIGHT) window_size = 3 left_matcher = cv2.StereoBM_create(16, 15) right_matcher = cv2.ximgproc.createRightMatcher(left_matcher) # FILTER Parameters lmbda = 80000 sigma = 1.2 visual_multiplier = 1.0 wls_filter = cv2.ximgproc.createDisparityWLSFilter(matcher_left=left_matcher) wls_filter.setLambda(lmbda) wls_filter.setSigmaColor(sigma) while (True): if not left.grab() or not right.grab(): print("No more frames") break
from __future__ import print_function import sys import numpy as np import cv2 imgL = cv2.pyrDown(cv2.imread('aloes/aloeL.jpg')) imgR = cv2.pyrDown(cv2.imread('aloes/aloeR.jpg')) min_disp = 16 num_disp = 128 - min_disp window_size = 11 stereoBM = cv2.StereoBM_create(numDisparities=num_disp, blockSize=window_size) stereoBM.setMinDisparity(min_disp) stereoBM.setNumDisparities(num_disp) stereoBM.setBlockSize(window_size) stereoBM.setUniquenessRatio(1) stereoBM.setSpeckleRange(32) stereoBM.setSpeckleWindowSize(100) stereoSGBM = cv2.StereoSGBM_create(numDisparities=num_disp, blockSize=window_size) stereoSGBM.setMinDisparity(min_disp) stereoSGBM.setNumDisparities(num_disp) stereoSGBM.setBlockSize(window_size) #stereoSGBM.setDisp12MaxDiff(0) #stereoSGBM.setUniquenessRatio(1) stereoSGBM.setSpeckleRange(32) stereoSGBM.setSpeckleWindowSize(100) grayL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY) grayR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)
f.write((ply_header % dict(vert_num=len(verts))).encode('utf-8')) numpy.savetxt(f, verts, fmt='%f %f %f %d %d %d ') left = cv2.imread("/Users/michaelwu/Desktop/projects/japan/data/left.png", cv2.IMREAD_GRAYSCALE) right = cv2.imread("/Users/michaelwu/Desktop/projects/japan/data/right.png", cv2.IMREAD_GRAYSCALE) h, w = left.shape[:2] fx = 0.8 * w # guess for focal length # lense focal length baseline = 200 #30 # distance in mm between the two cameras disparities = 32 #128 # num of disparities to consider block = 17 #31 # block size to match units = 5 # depth units, adjusted for the output to fit in one byte sbm = cv2.StereoBM_create(numDisparities=disparities, blockSize=block) # calculate disparities disparity = sbm.compute(left, right) valid_pixels = disparity > 0 # calculate depth data depth = numpy.zeros(shape=left.shape).astype("uint8") depth[valid_pixels] = (fx * baseline) / (units * disparity[valid_pixels]) # visualize depth data depth = cv2.equalizeHist(depth) colorized_depth = numpy.zeros((left.shape[0], left.shape[1], 3), dtype="uint8") temp = cv2.applyColorMap(depth, cv2.COLORMAP_JET) colorized_depth[valid_pixels] = temp[valid_pixels] # plt.imshow(depth)
def displayDisparity(self, left, right): cv.namedWindow('Disparity', cv.WINDOW_NORMAL) # create trackbars cv.createTrackbar('10 * alpha', 'Disparity', 10 * self.alpha, 10, cu.nullFunction) cv.createTrackbar('Ratio', 'Disparity', self.ratio, 5, cu.nullFunction) cv.createTrackbar('numDisparities / 16', 'Disparity', 1, 20, cu.nullFunction) cv.createTrackbar('(blockSize - 5) / 2', 'Disparity', 8, 20, cu.nullFunction) cv.createTrackbar('uniquenessRatio', 'Disparity', 15, 50, cu.nullFunction) self.computeCorrectionMaps(self.alpha, self.ratio) while (True): alpha = cv.getTrackbarPos('10 * alpha', 'Disparity') / 10 ratio = cv.getTrackbarPos('Ratio', 'Disparity') numDisparities = 16 * cv.getTrackbarPos('numDisparities / 16', 'Disparity') blockSize = 2 * cv.getTrackbarPos('(blockSize - 5) / 2', 'Disparity') + 5 uniquenessRatio = cv.getTrackbarPos('uniquenessRatio', 'Disparity') if ratio == 0: ratio = 1 if (alpha != self.alpha or ratio != self.ratio): self.alpha = alpha self.ratio = ratio self.computeCorrectionMaps(self.alpha, self.ratio) rectLeft, rectRight = self.rectify(left, right) # Construct the stereo object here (StereoBM_create) stereo = cv.StereoBM_create(numDisparities, blockSize) stereo.setMinDisparity(1) stereo.setUniquenessRatio(uniquenessRatio) # Compute the disparity here disparity = stereo.compute(left, right) minVal, maxVal, minLoc, maxLoc = cv.minMaxLoc(disparity) display = cv.convertScaleAbs(disparity, alpha=(255.0 / maxVal - minVal)) display = cv.cvtColor(display, cv.COLOR_GRAY2BGR) display = cv.applyColorMap(display, cv.COLORMAP_JET) mask = np.copy(disparity) mask[np.where(disparity <= [stereo.getNumDisparities() - 16])] = [ 0 ] mask[np.where(disparity > [stereo.getNumDisparities() - 16])] = [1] mask = np.uint8(mask) display = cv.bitwise_and(display, display, mask=mask) cv.imshow('Disparity', display) key = cv.waitKey(1) if key == ord('\x1b') or key == ord('q'): break cv.destroyAllWindows()
import numpy as np import cv2 from matplotlib import pyplot as plt CAMERA_LEFT = 1 CAMERA_RIGHT = 2 capL = cv2.VideoCapture(CAMERA_LEFT) capR = cv2.VideoCapture(CAMERA_RIGHT) while (True): # Capture frame-by-frame ret, frameL = capL.read() ret, frameR = capR.read() #stereo = cv2.createStereoBM(numDisparities=16, blockSize=15) stereo = cv2.StereoBM_create(numDisparities=16 * 15) grayL = cv2.cvtColor(frameL, cv2.COLOR_BGR2GRAY) grayR = cv2.cvtColor(frameR, cv2.COLOR_BGR2GRAY) disparity = stereo.compute(grayL, grayR) #disparity = (disparity - 127) * 255 #disparity = (disparity + 127) # Display frame cv2.imshow('disparity', disparity) #cv2.imshow('disparity', frameL) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture
import cv2 import os import numpy as np import matplotlib.pyplot as plt if __name__ == "__main__": left_path = os.path.join('left.jpg') right_path = os.path.join('right.jpg') image_left = cv2.imread(left_path) image_right = cv2.imread(right_path) gray_left = cv2.cvtColor(image_left, cv2.COLOR_BGR2GRAY) gray_right = cv2.cvtColor(image_right, cv2.COLOR_BGR2GRAY) matcher = cv2.StereoBM_create(numDisparities=32, blockSize=21) disparity = cv2.StereoBM.compute(matcher, gray_left, gray_right, disparity=None) cv2.imwrite(os.path.join('disparity.bmp'), disparity, params=None) # cv2.imshow('disparity', disparity)
colors = [] for i in range(0, 10): colors.append(tuple(np.random.randint(0, 255, 3).tolist())) img1_lines = cv2.computeCorrespondEpilines(selected_point1.reshape(-1, 1, 2), 2, fundamentalmat) img1_lines = img1_lines.reshape(-1, 3) img1_lines1 = drawlines(m1_clr, m2_clr, img1_lines, selected_point1, selected_point2, colors) img2_lines = cv2.computeCorrespondEpilines(selected_point2.reshape(-1, 1, 2), 2, fundamentalmat) img2_lines = img1_lines.reshape(-1, 3) img2_lines1 = drawlines(m2_clr, m1_clr, img2_lines, selected_point2, selected_point1, colors) stereo = cv2.StereoBM_create(96, blockSize=17) stereo.setMinDisparity(16) stereo.setDisp12MaxDiff(0) stereo.setUniquenessRatio(10) stereo.setSpeckleRange(32) stereo.setSpeckleWindowSize(100) disparity_map = stereo.compute(image1_bw, image2_bw).astype(np.float32) / 16.0 disp_map = (disparity_map - 16) / 96 # printing out all the output plt.imsave('output/task2/task2_disparity.jpg', disp_map, cmap=plt.cm.gray) cv2.imwrite('output/task2/task2_epi_right.jpg', img2_lines1) cv2.imwrite('output/task2/task2_epi_left.jpg', img1_lines1) cv2.imwrite("output/task2/merged.jpg", np.hstack([img2_lines1, img1_lines1]))
import numpy as np import matplotlib.pyplot as plt def rms(imageA, imageB): err = np.sum((imageA.astype("float") - imageB.astype("float"))**2) err /= float(imageA.shape[0] * imageA.shape[1]) return err left_img = cv2.imread('aloes/aloeL.png') right_img = cv2.imread('aloes/aloeR.png') groundtruth = cv2.imread('aloes/') # First attempt stereo_bm = cv2.StereoBM_create(16, 7) dispmap_bm = stereo_bm.compute(cv2.cvtColor(left_img, cv2.COLOR_BGR2GRAY), cv2.cvtColor(right_img, cv2.COLOR_BGR2GRAY)) stereo_sgbm = cv2.StereoSGBM_create(0, 16) dispmap_sgbm = stereo_sgbm.compute(left_img, right_img) plt.figure(figsize=(12, 10)) plt.subplot(221) plt.title('left') plt.imshow(left_img[:, :, [2, 1, 0]]) plt.subplot(222) plt.title('right') plt.imshow(right_img[:, :, [2, 1, 0]]) plt.subplot(223) plt.title('BM')
# Implementing calibration data print('Read calibration data and rectifying stereo pair...') calibration = StereoCalibration(input_folder=in_folder) # Initialize interface windows cv2.namedWindow("Image") cv2.moveWindow("Image", 50,100) #cv2.namedWindow("left") #cv2.moveWindow("left", 450,100) #cv2.namedWindow("right") #cv2.moveWindow("right", 850,100) disparity = np.zeros((img_width, img_height), np.uint8) sbm = cv2.StereoBM_create(numDisparities=0, blockSize=21) def stereo_depth_map(rectified_pair): dmLeft = rectified_pair[0] dmRight = rectified_pair[1] disparity = sbm.compute(dmLeft, dmRight) local_max = disparity.max() local_min = disparity.min() disparity_grayscale = (disparity-local_min)*(65535.0/(local_max-local_min)) disparity_fixtype = cv2.convertScaleAbs(disparity_grayscale, alpha=(255.0/65535.0)) disparity_color = cv2.applyColorMap(disparity_fixtype, cv2.COLORMAP_JET) #print('Disparity color size:', disparity_color.shape, '\nDisparity fixtype:', disparity_fixtype.shape, # '\nDisparity grayscae:', disparity_grayscale.shape, '\n') cv2.imshow("Image", disparity_color) print(disparity_color.shape)
def generate3D(rectR,rectL,Q_matrix): CV_MATCHER_BM = 1 CV_MATCHER_SGBM = 0 #define matching parameters #SETTINGS FOR PHOBOS NUCLEAR algorithm = CV_MATCHER_BM window_size = 21 block_size = 15 min_disp = -69 num_disp = 16*10 uniqness_ratio = 15 speckle_window_size = 500 speckle_range = 5 #Convert source image to unsigned 8 bit integer Numpy array arrL = np.uint8(rectL) arrR = np.uint8(rectR) #generate disparity using stereo matching algorithms if algorithm == CV_MATCHER_BM: stereo = cv2.StereoBM_create(numDisparities=num_disp, blockSize=block_size) stereo.setMinDisparity(min_disp) stereo.setSpeckleWindowSize(speckle_window_size) stereo.setSpeckleRange(speckle_range) stereo.setUniquenessRatio(uniqness_ratio) elif algorithm == CV_MATCHER_SGBM: stereo = cv2.StereoSGBM_create(minDisparity = min_disp, numDisparities = num_disp, blockSize = block_size, P1 = 8*3*window_size**2, P2 = 32*3*window_size**2, disp12MaxDiff = 1, uniquenessRatio = uniqness_ratio, speckleWindowSize = speckle_window_size, speckleRange = speckle_range ) disp = stereo.compute(arrL, arrR).astype(np.float32) / 16.0 #reproject disparity to 3D points = cv2.reprojectImageTo3D(disp, Q_matrix) disp = (disp-min_disp)/num_disp #normalise disparity imask = disp > disp.min() disp_thresh = np.zeros_like(disp, np.uint8) disp_thresh[imask] = disp[imask] disp_norm = np.zeros_like(disp, np.uint8) cv2.normalize(disp, disp_norm, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U) #format colour image from left camera for mapping to point cloud h, w = arrL.shape[:2] colors = cv2.cvtColor(arrL, cv2.COLOR_BGR2RGB) mask = disp > disp.min() out_points = points[mask] out_colors = colors[mask] #saves the points of a point map as variables return out_points
def display(): curDir = os.getcwd() objpoints = [] imgpoints1 = [] imgpoints2 = [] ret1, objpoints, imgpoints1 = calibrate(curDir + "/chessboardsA") if ret1: ret2, objpoints, imgpoints2 = calibrate(curDir + "/chessboardsB") if ret2: vc1, vc2, rval1, rval2, frame1, frame2 = init_video() gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) ret1, mtx1, dist1, rvecs1, tvecs1 = cv2.calibrateCamera( objpoints, imgpoints1, gray1.shape[::-1], None, None) ret2, mtx2, dist2, rvecs2, tvecs2 = cv2.calibrateCamera( objpoints, imgpoints2, gray2.shape[::-1], None, None) #v1 calib h1, w1 = frame1.shape[:2] h2, w2 = frame2.shape[:2] newcam1mtx, roi1 = cv2.getOptimalNewCameraMatrix( mtx1, dist1, (w1, h1), 1, (w1, h1)) newcam2mtx, roi2 = cv2.getOptimalNewCameraMatrix( mtx2, dist2, (w2, h2), 1, (w2, h2)) #v2 calib # termination criteria criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) (_, _, _, _, _, rotationMatrix, translationVector, _, _) = cv2.stereoCalibrate(objpoints, imgpoints1, imgpoints2, mtx1, dist1, mtx2, dist2, frame1.shape[:2], None, None, None, None, cv2.CALIB_FIX_INTRINSIC, criteria) (leftRectification, rightRectification, leftProjection, rightProjection, dispartityToDepthMap, leftROI, rightROI) = cv2.stereoRectify(mtx1, dist1, mtx2, dist2, frame1.shape[:2], rotationMatrix, translationVector, None, None, None, None, None, cv2.CALIB_ZERO_DISPARITY, -1) leftMapX, leftMapY = cv2.initUndistortRectifyMap( mtx1, dist1, leftRectification, leftProjection, frame1.shape[:2], cv2.CV_32FC1) rightMapX, rightMapY = cv2.initUndistortRectifyMap( mtx2, dist2, rightRectification, rightProjection, frame2.shape[:2], cv2.CV_32FC1) stereoMatcher = cv2.StereoBM_create() stereo = cv2.StereoBM_create() #stereoMatcher.setNumDisparities(128) #stereoMatcher.setBlockSize(21) #stereoMatcher.setSpeckleRange(16) #stereoMatcher.setSpeckleWindowSize(45) num = 4 while rval1 and rval2: #stereoMatcher.setMinDisparity(num) rval1, frame1 = vc1.read() rval2, frame2 = vc2.read() #v1 calib cam1v1 = cv2.undistort(frame1, mtx1, dist1, None, newcam1mtx) cam2v1 = cv2.undistort(frame2, mtx2, dist2, None, newcam2mtx) x1, y1, w1, h1 = roi1 cam1v1 = cam1v1[y1:y1 + h1, x1:x1 + w1] x2, y2, w2, h2 = roi2 cam2v1 = cam2v1[y2:y2 + h2, x2:x2 + w2] cv2.imshow("cam1Calibrated v1", cam1v1) cv2.imshow("cam2Calibrated v1", cam2v1) #v2 calib fixedLeft = cv2.remap(frame1, leftMapX, leftMapY, cv2.INTER_LINEAR) fixedRight = cv2.remap(frame2, rightMapX, rightMapY, cv2.INTER_LINEAR) grayLeft = cv2.cvtColor(fixedLeft, cv2.COLOR_BGR2GRAY) grayRight = cv2.cvtColor(fixedRight, cv2.COLOR_BGR2GRAY) depthv2 = stereoMatcher.compute(grayLeft, grayRight) cv2.imshow("depth v2", depthv2 / 2048) cv2.imshow("cam1Calibrated v2", fixedLeft) cv2.imshow("cam2Calibrated v2", fixedRight) key = cv2.waitKey(20) if key == 27: # exit on ESC break elif key == ord('w'): num += 1 print(num) elif key == ord('s'): num -= 1 print(num)
import numpy as np import cv2 as cv #from matplotlib import pyplot as plt imgL = cv.imread('imgL3.jpg', 0) imgR = cv.imread('imgR3.jpg', 0) #stereo = cv.StereoBM_create(numDisparities=384, blockSize=5) #disparity = stereo.compute(imgL,imgR) cv.namedWindow('image', cv.WINDOW_NORMAL) cv.resizeWindow('image', 600, 600) #cv.namedWindow('ref',cv.WINDOW_NORMAL) #cv.resizeWindow('ref', 600,600) flag_break = False while True: x = 1 while x < 100: stereo = cv.StereoBM_create(numDisparities=(x * 16), blockSize=5) disparity = stereo.compute(imgL, imgR) #cv.putText(disparity, "%.2f" % (x * 16), #(disparity.shape[1] - 500, disparity.shape[0] - 50), cv.FONT_HERSHEY_SIMPLEX, #20.0, (0, 255, 0), 10) #print (disparity.shape[0]) #print (disparity.shape[1]) cv.imshow('image', disparity) #cv.imshow('ref',imgL) #time.sleep(1) #cv.destroyAllWindows() x = x + 1 if cv.waitKey(200) & 0xFF == ord('q'): flag_break = True break x = 1
# img2_rectified = cv2.remap(frame2, camera_configs.right_map1, camera_configs.right_map2, cv2.INTER_LINEAR) # 将图片置为灰度图,为StereoBM作准备 imgL = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) imgR = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) # 两个trackbar用来调节不同的参数查看效果 num = cv2.getTrackbarPos("num", "depth") blockSize = cv2.getTrackbarPos("blockSize", "depth") if blockSize % 2 == 0: blockSize += 1 if blockSize < 5: blockSize = 5 # 根据Block Maching方法生成差异图(opencv里也提供了SGBM/Semi-Global Block Matching算法,有兴趣可以试试) stereo = cv2.StereoBM_create(numDisparities=16 * num, blockSize=21) disparity = stereo.compute(imgL, imgR) disp = cv2.normalize(disparity, disparity, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U) # 将图片扩展至3d空间中,其z方向的值则为当前的距离 threeD = cv2.reprojectImageTo3D( disparity.astype(np.float32) / 16., camera_configs.Q) cv2.imshow("left", imgL) cv2.imshow("right", imgR)
while (True): if not left.grab() or not right.grab(): print("No more frames") break _, fixedLeft = left.retrieve() _, fixedRight = right.retrieve() #fixedLeft = cv2.remap(leftFrame, leftMapX, leftMapY, cv2.INTER_LINEAR) #fixedRight = cv2.remap(rightFrame, rightMapX, rightMapY, cv2.INTER_LINEAR) grayLeft = cv2.cvtColor(fixedLeft, cv2.COLOR_BGR2GRAY) grayRight = cv2.cvtColor(fixedRight, cv2.COLOR_BGR2GRAY) left_matcher = cv2.StereoBM_create(int(numDisparities[0]), int(blockSize[0])) left_matcher.setNumDisparities(int(numDisparities[0])) left_matcher.setBlockSize(int(blockSize[0])) left_matcher.setUniquenessRatio(int(uniquessRatio[0])) left_matcher.setSpeckleRange(int(speckleRange[0])) left_matcher.setSpeckleWindowSize(int(speckleWindow[0])) right_matcher = cv2.ximgproc.createRightMatcher(left_matcher) wls_filter = cv2.ximgproc.createDisparityWLSFilter( matcher_left=left_matcher) wls_filter.setLambda(lmbda) wls_filter.setSigmaColor(sigma) displ = left_matcher.compute(grayLeft, grayRight) #.astype(np.float32)/16 displ = np.int16(displ) dispr = right_matcher.compute(grayRight,
def calculateDisparity(framePair): isSGBM=1 if(isSGBM): window_size=3 stereo = cv2.StereoSGBM_create(minDisparity =2, numDisparities =64, preFilterCap=63, blockSize = 3, uniquenessRatio =15, speckleWindowSize =180, speckleRange = 2, disp12MaxDiff = 1, #fullDP = True, P1 =64*3*window_size**2, P2 = 80*3*window_size**2, mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY ) stereoR=cv2.ximgproc.createRightMatcher(stereo) # Create another stereo for right this time #stereo = cv2.StereoSGBM_create(minDisparity=0, numDisparities=64, #blockSize=2*1+1) else: print("steroBM") stereo = cv2.StereoBM_create(numDisparities=16,blockSize=9) #print stereo # Compu te disparity. lmbda = 8000 sigma = 1.0 visual_multiplier = 1.0 wls_filter = cv2.ximgproc.createDisparityWLSFilter(matcher_left=stereo) wls_filter.setLambda(lmbda) wls_filter.setSigmaColor(sigma) disparity = stereo.compute(gray_l, gray_r) disp= stereo.compute(gray_l,gray_r)#.astype(np.float32)/ 16 # disp= ((disp.astype(np.float32)/ 16)-min_disp)/num_disp dispL= disp dispR= stereoR.compute(gray_r,gray_l) dispL= np.int16(dispL) dispR= np.int16(dispR) # Using the WLS filter filteredImg= wls_filter.filter(dispL,gray_l,None,dispR) filteredImg = cv2.normalize(src=filteredImg, dst=filteredImg, beta=0, alpha=255, norm_type=cv2.NORM_MINMAX); filteredImg = np.uint8(filteredImg) disparity =filteredImg np.set_printoptions(threshold=np.inf,linewidth=np.inf) #@print disparity #cv2.waitKey(0) #print("xxxxxxxxxxxxx",disparity.min(), disparity.max()) return disparity if 1: # post process for plot. # Convert to plot value. disparity = disparity/16 #Scale to (0~255) int16 #print("xxxxxxxxxxxxx",disparity.min(), disparity.max()) #disparity = disparity.astype(np.float16)/disparity.max().astype(np.float16)*255.0 disparity = disparity.astype(np.float16)/55.0*255.0 #print(disparity.min(), disparity.max()) disparity = disparity.astype(np.uint8) #print(disparity.min(), disparity.max()) #disparity = cv2.medianBlur(disparity, 5) return disparit
fourcc, 30.0, (1426,418)) elif Data_Num == 4: out = cv2.VideoWriter("Video_"+SaveName+".avi",\ fourcc, 30.0, (1426,418)) elif Data_Num == 5: out = cv2.VideoWriter("Video_"+SaveName+".avi",\ fourcc, 30.0, (1432,421)) elif Data_Num == 6: out = cv2.VideoWriter("Video_"+SaveName+".avi",\ fourcc, 30.0, (1426,418)) np.set_printoptions(suppress=True) # Setting parameters based on disparity type selected if disparity_type == "BM": left_matcher = cv2.StereoBM_create(\ numDisparities=16*10, blockSize=15) elif disparity_type == "BM+WLS": left_matcher = cv2.StereoBM_create(\ numDisparities=16*10, blockSize=15) right_matcher = cv2.ximgproc.createRightMatcher(left_matcher) wls_filter = cv2.ximgproc.createDisparityWLSFilter(\ matcher_left=left_matcher) wls_filter.setLambda(8000) wls_filter.setSigmaColor(0.8) else: print("Incorrect Disparity Method Selected") sys.exit() i = 0 k = 0
def scan(): # opening and downscaling left image l = cv.imread('left2.jpg',0) x = len(l[0]) y = len(l) k = 16 l = cv.resize(l, (x//k, y//k)) # opening and downscaling right image r = cv.imread('right2.jpg',0) r = cv.resize(r, (x//k, y//k)) # computing dispartity map stereo = cv.StereoBM_create(numDisparities=16, blockSize=15) disparity = stereo.compute(l,r) # visualization of disparity map plt.imshow(disparity,'gray') plt.show() # stereocam parameters w = 4.5 #mm (matrix width) h = 3.4 #mm (matrix height) fn = 5.6 #mm (focal length) T = 10 #mm (spacing) # middle point xmid = x/(2*k) #pixel ymid = y/(2*k) #pixel # pixel to mm factor Kpm = w/x # initializing point cloud xc = np.ndarray(0, np.int16) yc = np.ndarray(0, np.int16) zc = np.ndarray(0) xraw = np.ndarray(0, np.int16) yraw = np.ndarray(0, np.int16) zraw = np.ndarray(0) Krare = 10 # computing point cloud for j in range(y//k): for i in range(x//k): d = (disparity[j][i])*Kpm #mm if d > 0: dx = (i-xmid)*Kpm #mm dy = (j-ymid)*Kpm #mm Z = T*fn/d #mm if Z < 1000: xx = math.trunc(20*dx*Z/fn) yy = math.trunc(20*dy*Z/fn) zz = math.trunc(Z) xc = np.append(xc, xx//Krare) #cm yc = np.append(yc, zz//Krare) #cm zc = np.append(zc, -yy/Krare) #cm return xc,yc,zc
""" Disparity map """ import cv2 import matplotlib.pyplot as plt """ Load dataset """ fpath = 'd:/github/ComputerVision/hw4/' img_L = cv2.imread(fpath + 'im0.png', cv2.IMREAD_GRAYSCALE) img_R = cv2.imread(fpath + 'im1.png', cv2.IMREAD_GRAYSCALE) """ Create disparity map """ nd = 64 # number of Disparities bs = 15 # block size stereo = cv2.StereoBM_create(numDisparities=nd, blockSize=bs) disparity = stereo.compute(img_L, img_R) plt.figure(figsize=(10, 5), dpi=150) plt.subplot(1, 3, 1), plt.imshow(img_L, 'gray') plt.subplot(1, 3, 2), plt.imshow(img_R, 'gray') plt.subplot(1, 3, 3), plt.imshow(disparity) plt.show()
def stero_rectification(data, calib_files_left, calib_files_right, image_size): rectify_scale = 0 # 0=full crop, 1=no crop R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify( data["cameraMatrix1"], data["distCoeffs1"], data["cameraMatrix2"], data["distCoeffs2"], image_size, data["R"], data["T"], alpha=rectify_scale) #(240, 320) # compute undistortion and rectification; output two maps for remap purpose left_maps = cv2.initUndistortRectifyMap(data["cameraMatrix1"], data["distCoeffs1"], R1, P1, image_size, cv2.CV_16SC2) #cv2.CV_16SC2 right_maps = cv2.initUndistortRectifyMap(data["cameraMatrix2"], data["distCoeffs2"], R2, P2, image_size, m1type=cv2.CV_16SC2) #CV_32FC1 # print(left_maps[0]) # print(left_maps[1]) calib_files = zip(calib_files_left, calib_files_right) # calib_files = ('./calibration/CaptureL.JPG', './calibration/CaptureR.JPG') # dstmap1_l, dstmap2_l = cv2.convertMaps(map1=left_maps[0], map2=left_maps[1], dstmap1type=cv2.CV_16SC2) # dstmap1_r, dstmap2_r = cv2.convertMaps(map1=right_maps[0], map2=right_maps[1], dstmap1type=cv2.CV_16SC2) for left_path, right_path in calib_files: # left_path = './calibration/CaptureL.JPG' # right_path = './calibration/CaptureR.JPG' # print(left_path) # print(right_path) left_img = cv2.imread(left_path) right_img = cv2.imread(right_path) print(left_img.shape) print(right_img.shape) # cv2.imshow("rectifed left chess one", left_img) # cv2.waitKey(0) # cv2.imshow("recified right chess two", left_img[1]) # left_img = left_img.astype(np.float16) # right_img = right_img.astype(np.float16) # left_img.convertTo(left_img, cv2.CV_32FC1) left_img_remap = cv2.remap(left_img, left_maps[0], left_maps[1], cv2.INTER_LANCZOS4) right_img_remap = cv2.remap(right_img, right_maps[0], right_maps[1], cv2.INTER_LANCZOS4) #cv2.INTER_LANCZOS4 # print("get line") # get_epipolar_line(left_img_remap, right_img_remap) cv2.imshow("rectifed left chess", left_img_remap) cv2.imshow("recified right chess", right_img_remap) cv2.waitKey(0) # input1 = input("Enter") imgL = cv2.cvtColor(left_img_remap, cv2.COLOR_BGR2GRAY) imgR = cv2.cvtColor(right_img_remap, cv2.COLOR_BGR2GRAY) stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15) # cv2.imshow('L', imgL) # cv2.imshow('R', imgR) # cv2.waitKey(0) # cv2.destroyAllWindows() disparity = stereo.compute(imgL, imgR) print(disparity) plt.imshow(disparity, 'gray') plt.show() cv2.destroyAllWindows() return left_img_remap, right_img_remap
cv2.resizeWindow('disp', 600, 600) cv2.createTrackbar('numDisparities', 'disp', 1, 17, nothing) cv2.createTrackbar('blockSize', 'disp', 5, 50, nothing) cv2.createTrackbar('preFilterType', 'disp', 1, 1, nothing) cv2.createTrackbar('preFilterSize', 'disp', 2, 25, nothing) cv2.createTrackbar('preFilterCap', 'disp', 5, 62, nothing) cv2.createTrackbar('textureThreshold', 'disp', 10, 100, nothing) cv2.createTrackbar('uniquenessRatio', 'disp', 15, 100, nothing) cv2.createTrackbar('speckleRange', 'disp', 0, 100, nothing) cv2.createTrackbar('speckleWindowSize', 'disp', 3, 25, nothing) cv2.createTrackbar('disp12MaxDiff', 'disp', 5, 25, nothing) cv2.createTrackbar('minDisparity', 'disp', 5, 25, nothing) # Creating an object of StereoBM algorithm stereo = cv2.StereoBM_create() while True: # Capturing and storing left and right camera images retL, imgL = CamL.read() retR, imgR = CamR.read() # Proceed only if the frames have been captured if retL and retR: imgR_gray = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY) imgL_gray = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY) # Applying stereo image rectification on the left image Left_nice = cv2.remap(imgL_gray, Left_Stereo_Map_x, Left_Stereo_Map_y, cv2.INTER_LANCZOS4, cv2.BORDER_CONSTANT, 0)
import numpy as np import sys import cv2 as cv from matplotlib import pyplot as plt np.set_printoptions(threshold=sys.maxsize) imgL = cv.imread('left1.png', 0) imgR = cv.imread('right1.png', 0) stereo = cv.StereoBM_create(numDisparities=32, blockSize=9) disparity = stereo.compute(imgL, imgR) #disparity = cv.normalize(stereo.compute(imgL, imgR),stereo.compute(imgL, imgR), alpha=0, beta=255, \norm_type=cv.NORM_MINMAX, dtype=cv.CV_8U) #plt.imshow(disparity,'gray') #print(disparity) plt.subplot(121), plt.imshow(disparity) plt.subplot(122), plt.imshow(imgL) #plt.imshow(imgL,'gray') plt.show() cv.imshow('ssss', disparity) cv.imshow('ddd', imgL) cv.waitKey(0) #cv.imshow('gray1',imgL) #cv.Waitkey(0) #cv.DestroyAllWindows()
import numpy as np import cv2 from matplotlib import pyplot as plt imgL = cv2.imread('../data/left.png', 0) imgR = cv2.imread('../data/right.png', 0) # SAD window size should be between 5..255 block_size = 15 num_disp = 16 # 必须取16的整数倍 uniquenessRatio = 10 stereo = cv2.StereoBM_create(numDisparities=num_disp, blockSize=block_size) stereo.setUniquenessRatio(uniquenessRatio) # disparity = stereo.compute(imgL,imgR) disparity = stereo.compute(imgL, imgR).astype(np.float32) / 16.0 # np.savetxt("../data/disparity_image_bm.txt", disparity, fmt='%3.2f', delimiter=' ', newline='\n') plt.imshow(disparity, 'gray') plt.axis('off') # 去掉坐标轴 plt.savefig('...') plt.show() disparity = cv2.imread('', cv2.IMREAD_GRAYSCALE) disparity = cv2.applyColorMap(disparity, cv2.COLORMAP_JET) # 转化为伪彩色图
ndisparities 0-512 steps of 16 //32+1 SADWindowSize 5-255 odd //125+1 ''' dispar = [ 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256, 272, 288, 304, 320, 336, 352, 368, 384, 400, 416, 432, 448, 464, 480, 496, 512 ] windowSize = [ 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255 ] imageL = cv2.imread('l.png') imageR = cv2.imread('r.png') imageL = cv2.cvtColor(imageL, cv2.COLOR_BGR2GRAY) imageR = cv2.cvtColor(imageR, cv2.COLOR_BGR2GRAY) for x in dispar: for y in windowSize: # stereo = cv2.StereoBM_create(cv2.STEREO_BM_BASIC_PRESET,ndisparities=x, SADWindowSize=y) stereo = cv2.StereoBM_create(numDisparities=x, blockSize=y) disparity = stereo.compute(imageL, imageR, cv2.CV_32F) cv2.imwrite('inv_d' + str(x) + '_w' + str(y) + '.png', disparity) print 'x=' + str(x) + ' y=' + str(y)