def disp(self): pass imgL = cv2.imread('Yeuna9x.png', 0) imgR = cv2.imread('SuXT483.png', 0) stereo = cv2.StereoBM(1, 16, 15) disparity = stereo.compute(imgL, imgR)
def stereo_disparity(Number_of_Disparities, Window_Size): stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, ndisparities=Number_of_Disparities, SADWindowSize=Window_Size) ##http://docs.opencv.org/2.4.1/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#stereobm-stereobm disparity = stereo.compute(imgL, imgR) return disparity
def __init__(self, params=default_params): self.params = params # Initilize stereo block matching self.bm = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, 128, 9) self.process = self.compute
def doStereo(imgL, imgR, params): """ Parameters tuned for q50 car images. Parameters: minDisparity - Minimum possible disparity value. Normally, it is zero but sometimes rectification algorithms can shift images, so this parameter needs to be adjusted accordingly. numDisparities - Maximum disparity minus minimum disparity. The value is always greater than zero. In the current implementation, this parameter must be divisible by 16. SADWindowSize - Matched block size. It must be an odd number >=1. Normally, it should be somewhere in the 3..11 range. P1 - The first parameter controlling the disparity smoothness. See below. P2 - The second parameter controlling the disparity smoothness. The larger the values are, the smoother the disparity is. P1 is the penalty on the disparity change by plus or minus 1 between neighbor pixels. P2 is the penalty on the disparity change by more than 1 between neighbor pixels. The algorithm requires P2 > P1. See stereo_match.cpp sample where some reasonably good P1 and P2 values are shown (like 8*number_of_image_channels*SADWindowSize*SADWindowSize and 32*number_of_image_channels*SADWindowSize*SADWindowSize, respectively). disp12MaxDiff - Maximum allowed difference (in integer pixel units) in the left-right disparity check. Set it to a non-positive value to disable the check. preFilterCap - Truncation value for the prefiltered image pixels. The algorithm first computes x-derivative at each pixel and clips its value by [-preFilterCap, preFilterCap] interval. The result values are passed to the Birchfield-Tomasi pixel cost function. uniquenessRatio - Margin in percentage by which the best (minimum) computed cost function value should "win" the second best value to consider the found match correct. Normally, a value within the 5-15 range is good enough. speckleWindowSize - Maximum size of smooth disparity regions to consider their noise speckles and invalidate. Set it to 0 to disable speckle filtering. Otherwise, set it somewhere in the 50-200 range. speckleRange - Maximum disparity variation within each connected component. If you do speckle filtering, set the parameter to a positive value, it will be implicitly multiplied by 16. Normally, 1 or 2 is good enough. fullDP - Set it to true to run the full-scale two-pass dynamic programming algorithm. It will consume O(W*H*numDisparities) bytes, which is large for 640x480 stereo and huge for HD-size pictures. By default, it is set to false. """ imsize = (1280, 960) (R1, R2, P1, P2, Q, size1, size2, map1x, map1y, map2x, map2y) = computeStereoRectify(params) imgRectL = cv2.remap(imgL, map1x, map1y, interpolation=cv.CV_INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) imgRectR = cv2.remap(imgR, map2x, map2y, interpolation=cv.CV_INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) """ window_size = 1 min_disp = 0 num_disp = 64 stereo = cv2.StereoSGBM(minDisparity = min_disp, numDisparities = num_disp, SADWindowSize = window_size, uniquenessRatio = 30, speckleWindowSize = 80, speckleRange = 1, disp12MaxDiff = 1, P1 = 8*3*window_size**2, P2 = 128*3*window_size**2, fullDP = True ) """ imgRectL = cv2.cvtColor(imgRectL, cv2.COLOR_RGB2GRAY) imgRectR = cv2.cvtColor(imgRectR, cv2.COLOR_RGB2GRAY) stereo = cv2.StereoBM(preset=cv.CV_STEREO_BM_NARROW, SADWindowSize=35) print 'computing stereo...' disp = stereo.compute(imgRectL, imgRectR).astype(np.float32) / 16.0 return (disp, Q, R1, R2)
def __init__(self, is_sgbm=False): # fixed parameters for current camera # StereoSGBM([minDisparity, numDisparities, SADWindowSize[, P1[, P2[, disp12MaxDiff[, preFilterCap[, uniquenessRatio[, speckleWindowSize[, speckleRange[, fullDP]]]]]]]]]) -> <StereoSGBM object> if is_sgbm: self.bm = cv2.StereoSGBM(0, 128, 15, 200, 400, 0, 9, 15, 100, 4, False) else: self.bm = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, 128, 45) #preset, ndisp, SADWindowSize self.is_sgbm = is_sgbm
def stereobm(config, left_image, right_image): bm = cv2.StereoBM(config.stereobm.preset_id, config.stereobm.ndisparity, config.stereobm.sad_window_size) left_image = cv2.cvtColor(left_image, cv2.COLOR_RGB2GRAY) right_image = cv2.cvtColor(right_image, cv2.COLOR_RGB2GRAY) disparity = bm.compute(left_image, right_image, disptype=cv2.CV_16S) disparity *= 255 / (disparity.min() - disparity.max()) disparity = disparity.astype(numpy.uint8) return disparity
def cl_op7_hist_time(BS,D,H,W,M=4): times = [] left,right, out =create_arrays(H,W) for i in xrange(M): #t=hist_op_1_time(img_buf,bins_buf, N) start=time.time() stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET,ndisparities=D, SADWindowSize=BS) ##http://docs.opencv.org/2.4.1/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#stereobm-stereobm #out = stereo.compute(left,right) t=time.time()-start times.append(t) #print 'opencl time: ', np.average(times) return np.average(times)
def get_disparity(imL, imR): import numpy as np import cv2 from matplotlib import pyplot as plt # imgL = cv2.imread('Yeuna9x.png',0) # imgR = cv2.imread('SuXT483.png',0) stereo = cv2.StereoBM(1, 16, 15) disparity = stereo.compute(imgL, imgR) plt.imshow(disparity,'gray') plt.show() return disparity
def pureBlockMatch(self, SADWindowSize=5): stereo = cv2.StereoBM(preset=cv2.cv.CV_STEREO_BM_BASIC, ndisparities=self.ndisparities, SADWindowSize=SADWindowSize) """ presetspecifies the whole set of algorithm parameters, one of: BASIC_PRESET - parameters suitable for general cameras FISH_EYE_PRESET - parameters suitable for wide-angle cameras NARROW_PRESET - parameters suitable for narrow-angle cameras """ disparity = stereo.compute(self.grayL, self.grayR) return disparity
def compute_depth(self, imgR, imgL, ndisparities, SADWindowSize): # Convert to gray scale imgR_ = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY) imgL_ = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY) # Compute stereo disparity stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, ndisparities, SADWindowSize) D = stereo.compute(imgL_, imgR_).astype(np.float) depth_map = ( D - np.min(D) ) / ( np.max(D) - np.min(D) ) * 255 for ch in xrange(3): imgL[:, :, ch] = depth_map.astype(np.uint8) return imgL
def getStereoPairPointCloud(imgL, imgR, calibration_data='../calibration_data/camera_left2' ): stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, ndisparities=16) disparity = stereo.compute(imgL, imgR) cF = open(calibration_data, 'rb') dL = pickle.load(cF) fx = dL['K'][0] fy = dL['K'][4] cx = dL['K'][2] cy = dL['K'][5] Tx = dL['P'][3] Ty = dL['P'][7] T = -Tx / fx Q = np.zeros((4, 4)) Q[0, 0] = 1 Q[0, 3] = -cx Q[1, 3] = -cy Q[1, 1] = 1 Q[2, 3] = np.sqrt(fx**2 + fy**2) #Q[3,3] = 1 Q[3, 2] = -1 / T dispindices = np.where(disparity > 0) N = len(dispindices[0]) sparseline = np.zeros((N, 3)) for i in range(0, N): sparseline[i, :] = np.array((dispindices[0][i], dispindices[1][i], disparity[dispindices[0][i], dispindices[1][i]])) sparseline[:, 2] = np.mean(sparseline[:, 2]) print sparseline[:, 2] print np.mean(sparseline[:, 2]) plt.figure() plt.imshow(disparity, cmap='gray') plt.show() out = cv2.perspectiveTransform(sparseline[None, :, :], Q) return -np.squeeze(out)
def __init__(self): self.right_image = None self.left_image = None self.rcounter = 0 self.lcounter = 0 self.info = {'l': None, 'r': None} self.bridge = cv_bridge.CvBridge() self.right_patches = None self.left_patches = None #========SUBSCRIBERS========# # image subscribers rospy.init_node('image_saver') rospy.Subscriber("/endoscope/left/image_rect_color", Image, self.left_image_callback, queue_size=1) rospy.Subscriber("/endoscope/right/image_rect_color", Image, self.right_image_callback, queue_size=1) # info subscribers rospy.Subscriber("/endoscope/left/camera_info", CameraInfo, self.left_info_callback) rospy.Subscriber("/endoscope/right/camera_info", CameraInfo, self.right_info_callback) rospy.spin() print("After rospy.spin() shut down") print("\tleft_patches.shape = {}".format(self.left_patches.shape)) print("\tright_patches.shape = {}".format(self.right_patches.shape)) print("right_image.shape = {}, left_image.shape = {}".format( self.right_image.shape, self.left_image.shape)) #======= SAVE IMAGES and MEASURE DISPARITY =======# cv2.imwrite("color_left.jpg", self.left_image) cv2.imwrite("color_right.jpg", self.right_image) stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, ndisparities=16, SADWindowSize=15) img1 = cv2.cvtColor(self.left_image, cv2.COLOR_BGR2GRAY) img2 = cv2.cvtColor(self.right_image, cv2.COLOR_BGR2GRAY) disparity = stereo.compute(img1, img2) cv2.imwrite("disparity.jpg", disparity)
def main(): # 画像取得 gray_l = cv2.imread("left.png", 0) gray_r = cv2.imread("right.png", 0) # BM法でステレオ対応点探索 stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, ndisparities=32, SADWindowSize=21) disp = stereo.compute(gray_l, gray_r) # 視差を計算 # 視差データを8bitデータに変換(imshowで表示させるため) disp = cv2.normalize(disp, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U) cv2.imshow("disp", disp) # 視差画像の表示 cv2.waitKey(0) cv2.destroyAllWindows()
def __init__(self, bm_type='bm'): self.bm_type = bm_type unitDisparity = 15 numberOfDisaprities = unitDisparity * 16 if bm_type == 'bm': bm = cv2.StereoBM().create() # bm.setROI1(validPixROI1) # bm.setROI2(validPixROI2) bm.setBlockSize(21) bm.setPreFilterCap(13) bm.setMinDisparity(0) bm.setNumDisparities(numberOfDisaprities) bm.setTextureThreshold(20) bm.setUniquenessRatio(10) bm.setSpeckleWindowSize(19) bm.setSpeckleRange(32) bm.setDisp12MaxDiff(5) self.bm = bm elif bm_type == 'sgbm': bm = cv2.StereoSGBM().create() SADWindowSize = 11 mindisparity = 0 ndisparities = 64 bm.setMinDisparity(0) bm.setNumDisparities(64) bm.setBlockSize(SADWindowSize) channel = 1 P1 = 8 * channel * SADWindowSize * SADWindowSize P2 = 32 * channel * SADWindowSize * SADWindowSize bm.setP1(P1) bm.setP2(P2) bm.setPreFilterCap(15) bm.setUniquenessRatio(10) bm.setSpeckleRange(2) bm.setSpeckleWindowSize(100) bm.setDisp12MaxDiff(cv2.STEREO_SGBM_MODE_SGBM) self.bm = bm else: raise AssertionError('bm_type must be select in sgbm and bm')
def __init__(self, trajectoire, depth_max=10., height_max=2., resolution=(1000, 1000), cell_size=0.1, origin=(512, 512), downsample=True, dense_decim=4, visu_scale=5): ''' Constructeur de la carte d'élévation : depth_max : profondeur maximum utilisé dans la carte height_max : hauteur maximum des imformation ajouté à la carte resolution : taille (x,y) en pixels de la carte cell_size : taille en mètres de la cellule (pixel), la taille réele cartographié est alors cell_size*resolution origin : position de l'origine dans la carte (position en pixel) downsample : défini si la disparite est calculé sur une image a plus basse résolution dense_decim : decimation temporel des carte stereo inséré ''' self.height_max = height_max self.height_min = 0.2 self.traj = trajectoire self.depth_max = depth_max self.origin = origin self.carte = np.zeros(resolution) self.votes = np.zeros(resolution) self.cell_size = cell_size self.current_time = -1 self.ref_plane = None self.downsample = downsample self.decimation_dense = dense_decim if self.downsample: disp_lvl = 32 else: disp_lvl = 64 self.stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, disp_lvl, 9) #self.stereo = cv2.StereoSGBM(0,disp_lvl, 7, P1 = 10, P2 = 500, speckleWindowSize = 51, speckleRange= 1 ) self.visu_scale = visu_scale
def callback(self, image_1, image_2): try: cv_image_1 = self.bridge.imgmsg_to_cv2(image_1, "bgr8") cv_image_2 = self.bridge.imgmsg_to_cv2(image_2, "bgr8") except CvBridgeError as e: print(e) cv2.imshow('left_hand_camera 1', cv_image_1) cv2.imshow('right_hand_camera 2', cv_image_2) cv2.waitKey(1) stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, ndisparities=16, SADWindowSize=15) gray_1 = cv2.cvtColor(cv_image_1, cv2.COLOR_BGR2GRAY) gray_2 = cv2.cvtColor(cv_image_2, cv2.COLOR_BGR2GRAY) disparity = stereo.compute(gray_1, gray_2) plt.imshow(disparity, 'gray') plt.show()
def testStereoAlgo(I0, I1, winSize, numDisp): ''' Fonction calculant et affichant la carte de disparité avec 4 algorighmes: BM : algorithme de bloque matching stereo simple SGBM1 : algorithme sgbm avec une faible régularisaion SGBM2 : algorithme sgbm avec une régularisation moyenne SGMB3 : algorithme sgbm avec une forte régularisation winSize = taille de la fenêtre de corrélation numDisp = nombre d'hypothèse de disparités ''' print(numDisp) bm = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, numDisp, winSize) disp1 = bm.compute(I0, I1).astype(np.float32) / 16. sgbm = cv2.StereoSGBM(0, numDisp, winSize, P1=0, P2=5) disp2 = sgbm.compute(I0, I1).astype(np.float32) / 16. sgbm2 = cv2.StereoSGBM(0, numDisp, winSize, P1=10, P2=500, speckleWindowSize=51, speckleRange=1) disp3 = sgbm2.compute(I0, I1).astype(np.float32) / 16. sgbm3 = cv2.StereoSGBM(0, numDisp, winSize, P1=5000, P2=10000, speckleWindowSize=51, speckleRange=1) disp4 = sgbm3.compute(I0, I1).astype(np.float32) / 16. figure() disp = np.concatenate( [np.concatenate([disp1, disp3]), np.concatenate([disp2, disp4])], axis=1) imshow(disp, vmin=0, vmax=numDisp) colorbar() title('resultat [[BM ,SGBM1],[SGBM 2, SGBM3]]')
def estimateDisparity(image_left,image_right,method='SGBM'): if method=='SGBM': import cv2 disparityrange=[0,15] ndisparities=int(np.ceil(disparityrange[1]-disparityrange[0]) ) ndisparities=16*int(np.ceil(float(ndisparities)/16)) minDisparity=int(np.floor(disparityrange[0])) #stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET,ndisparities=ndisparities, SADWindowSize=15) #stereo = cv2.StereoSGBM(minDisparity=mindisparity,numDisparities=ndisparities, SADWindowSize=15,P1=10000,P2=0) stereo = cv2.StereoSGBM(minDisparity=minDisparity,numDisparities=ndisparities, SADWindowSize=3,P1=30,P2=30) right_disparity = stereo.compute(image_left[:,:,0],image_right[:,:,0])# does not work in color... right_disparity=right_disparity/16. elif method=='BM': stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET,ndisparities=ndisparities, SADWindowSize=5) right_disparity = stereo.compute(image_left[:,:,0],image_right[:,:,0],disptype=cv2.CV_32F)# does not work in color... return right_disparity
def find_depth(img1, img2): """ Goal:+ calculated disparity map From disparity map estimate depth of image objects Inputs: img1 -> file path to left image img2 -> file path to right image Returns: estimated depth map """ left = cv2.imread(img1, 0) right = cv2.imread(img2, 0) stereo = cv2.StereoBM(preset = 0, ndisparities=208, SADWindowSize = 5)#minDisparity=0, numDisparities=192, SADWindowSize=7) disparity = stereo.compute(left,right) cv2.imshow('right',right) cv2.imshow('disparity', disparity) cv2.waitKey(0)
def main(): (map1x, map1y, map2x, map2y) = calibration() cam = cv2.VideoCapture(0) # левая cam0 = cv2.VideoCapture(2) # правая n = 0 while (1): ret_val, frame = cam.read() ret_val0, frame0 = cam0.read() img_l = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) img_r = cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY) height, width, depth = frame.shape imgU1 = np.zeros((height, width, 3), np.uint8) imgU2 = np.zeros((height, width, 3), np.uint8) imgU1 = cv2.remap(img_l, map1x, map1y, cv2.INTER_LINEAR, imgU1, cv2.BORDER_CONSTANT, 0) imgU2 = cv2.remap(img_r, map2x, map2y, cv2.INTER_LINEAR, imgU2, cv2.BORDER_CONSTANT, 0) stereo = cv2.StereoBM(1, 0, 11) disparity = stereo.compute(imgU2, imgU1, cv2.CV_32F) # cv2.imshow('disparity', disparity) cv2.imshow('right_U', imgU1) cv2.imshow('left_U', imgU2) cv2.imshow('frame', frame) cv2.imshow('frame0', frame0) plt.imshow(disparity, 'gray') plt.show() k = cv2.waitKey(30) & 0xff # рендринг материала для калибровки if k == 10: n = saveImg(n, frame, frame0) if k == 27: break
def compute_disparity(self, left_image, right_image, ndisparities=16, SADWindowSize=25): """Compute the disparity image, given the left and right image.""" stereo = cv2.StereoBM( cv2.STEREO_BM_BASIC_PRESET, ndisparities=ndisparities, SADWindowSize=SADWindowSize) self._left_image = left_image # Convert the given images to grayscale gray_left = cv2.cvtColor(left_image, cv2.COLOR_BGR2GRAY) self._right_image = right_image gray_right = cv2.cvtColor(right_image, cv2.COLOR_BGR2GRAY) # Compute stereo disparity image disparity_map = ( stereo.compute(gray_left, gray_right).astype(np.float32) - 32 ) / 25.0 self._disparity_map = disparity_map
try: cap = cv2.VideoCapture(1) logging.debug("SUCCESFULLY ACTIVATE WEBCAM") except: logging.error("ERROR CANNOT ACTIVATE WEBCAM") exit(0) with open("calibration.json", "r") as fp: logging.debug("READ CALIBRATION JSON") calibration = json.load(fp) mtx = np.array(calibration["mtx"]) dist = np.array(calibration["dist"]) newcameramtx = np.array(calibration["newcameramtx"]) logging.debug("WIDTH = %s", str(cap.get(cv2.CAP_PROP_FRAME_WIDTH))) logging.debug("HEIGHT = %s", str(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) fps = cap.get(cv2.CAP_PROP_FPS) delta_time = 100 / fps logging.debug("START VIDEO CAPTURE WITH %d MILLISECONDS INTERVAL", int(delta_time)) stereo = cv2.StereoBM() logging.debug("START START CAPTURING") wait = -1 while cap.isOpened() and wait == -1: frame = read_video(cap, mtx, dist, newcameramtx, stereo) wait = cv2.waitKey(int(delta_time)) logging.debug("END CAPTURING")
import cv2 from matplotlib import pyplot as plt imgL = cv2.imread('left_1.ppm',0) imgR = cv2.imread('right_1.ppm',0) # stereo = cv2.createStereoBM(numDisparities=16, blockSize=15) stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET,ndisparities=16, SADWindowSize=15) disparity = stereo.compute(imgL,imgR) plt.imshow(disparity,'gray') plt.show()
dest='right_path', default='right_image_rect/', help='Right camera subpath') args = parser.parse_args() root_folder = args.root_folder[0] out_folder = join(root_folder, 'depth/') left_files = glob.glob(join(root_folder, args.left_path, '*.jpg')) left_files.sort() #[rename(f,join(root_folder, args.left_path,"frame{:03}.jpg".format(i))) for f,i in zip(left_files, range(len(left_files)))] #right_files = glob.glob(join(root_folder, args.left_path, '*.jpg')) #right_files.sort() #[rename(f,join(root_folder, args.right_path,"frame{:03}.jpg".format(i))) for f,i in zip(right_files, range(len(right_files)))] #print left_files if not isdir(out_folder): makedirs(out_folder) stereo = cv2.StereoBM(1, 16, 15) for left_path in left_files: right_path = join(root_folder, args.right_path, basename(left_path)) depth_path = join(out_folder, basename(left_path)) print right_path imgL = cv2.imread(left_path, cv2.CV_LOAD_IMAGE_GRAYSCALE) imgR = cv2.imread(right_path, cv2.CV_LOAD_IMAGE_GRAYSCALE) print imgL.shape print imgR.shape depth_map = stereo.compute(imgL, imgR) cv2.imwrite(depth_path, depth_map)
#Adds the trackbar to disparity frame ndi = cv2.getTrackbarPos(dr, disp) sws = cv2.getTrackbarPos(bs, disp) if sws % 2 == 0: sws += 1 if sws < startWD: sws = 5 if not ndi % 16 == 0: ndi = (ndi / 16) * 16 #Determine the stereo block matching algorithm for the variables given stereo = cv2.StereoBM(0, ndi, sws) #Prints out the value of the variables print "DisparityRange:", ndi print "BlockSize:", sws #Computes the disparity from the two video feeds disparity = stereo.compute(gray_left, gray_right) #Disparity needs to be converted to match the format of the camera video feeds cvuint8 = cv2.convertScaleAbs(disparity) #Displays the disparity map cv2.imshow('disparity', cvuint8) f = 0.4 #focal length
capL = cv2.VideoCapture(int(sys.argv[1])) capR = cv2.VideoCapture(int(sys.argv[2])) imgL = np.zeros((480, 640, 3), np.uint8) imgR = np.zeros((480, 640, 3), np.uint8) while True: capL.read(imgL) capR.read(imgR) imgGrayL = cv2.GaussianBlur( cv2.equalizeHist( cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)), (5, 5), 0) imgGrayR = cv2.GaussianBlur( cv2.equalizeHist( cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)), (5, 5), 0) cv2.imshow("image left gray", imgGrayL) cv2.imshow("image right gray", imgGrayR) k = cv2.waitKey(33) if k == ord('d'): stereo = cv2.StereoBM( cv2.STEREO_BM_BASIC_PRESET, ndisparities=int(sys.argv[3]), SADWindowSize=int(sys.argv[4])) disparity = stereo.compute( imgGrayL, imgGrayR) plt.imshow(disparity, "gray") plt.show() else: if k == ord('q'): break
import cv2 from matplotlib import pyplot as plt imgL = cv2.imread('imgL_0.png') imgR = cv2.imread('imgR_0.png') cv2.namedWindow('imgL', cv2.WINDOW_NORMAL) cv2.namedWindow('imgR', cv2.WINDOW_NORMAL) cv2.namedWindow('disparity', cv2.WINDOW_NORMAL) while (cv2.waitKey(1) & 0xFF != ord('q')): grayImgL = cv2.imread('imgL_0.png', 0) grayImgR = cv2.imread('imgR_0.png', 0) # disparity settings numDisparites = 16 SADwindowsize = 21 stereo = cv2.StereoBM(0, ndisparities=numDisparites, SADWindowSize=SADwindowsize) cv2.imshow("Left image", imgL) cv2.imshow("Right image", imgR) k = cv2.waitKey(0) #timing in ms, keyboard binding function if k == 27: #wait for esc key interrupt cv2.destroyAllWindows() #destroy window and escape from program disparity = stereo.compute(grayImgL, grayImgR) plt.imshow(disparity, 'gray') plt.show()
#left = np.uint8(left) #right = np.uint8(right) 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 * 3 * window_size**2, P2=32 * 3 * window_size**2, disp12MaxDiff=1, uniquenessRatio=10, speckleWindowSize=100, speckleRange=32) stereo = cv2.StereoBM(numDisparities=16, blockSize=15) print('computing disparity...') dis = stereo.compute(left, right).astype(np.float32) / 16.0 #disparity_image = np.uint8(right) disparity_image = np.uint8(dis) print('frame {} computed'.format(counter)) #display.display([left, disparity_image]) #cv2.imwrite('/home/jetson/Videos/disparity/testimage{}.png'.format(counter), disparity_image) # out.write(disparity_image) # print(disparity) #plt.imshow(dis, 'gray') # plt.show() else: _, left = left_cam.read() counter += 1
def _replace_bm(self): """Replace ``_block_matcher`` with current values.""" self._block_matcher = cv2.StereoBM(preset=self._bm_preset, ndisparities=self._search_range, SADWindowSize=self._window_size)
date = '2011_09_29' drive = '0071' # Optionally, specify the frame range to load frame_range = 0 # Load the data dataset = pykitti.raw(basedir, date, drive, frame_range) # Load image data # dataset.load_gray(format='cv2') # Loads images as uint8 grayscale dataset.load_rgb(format='cv2') # Loads images as uint8 with BGR ordering # Do some stereo processing stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET, numDisparities=64, blockSize=15) disp_gray = stereo.compute(dataset.gray[0].left, dataset.gray[0].right) disp_rgb = stereo.compute( cv2.cvtColor(dataset.rgb[0].left, cv2.COLOR_BGR2GRAY), cv2.cvtColor(dataset.rgb[0].right, cv2.COLOR_BGR2GRAY)) # Display some data f, ax = plt.subplots(2, 2, figsize=(15, 5)) ax[0, 0].imshow(dataset.gray[0].left, cmap='gray') ax[0, 0].set_title('Left Gray Image (cam0)') ax[0, 1].imshow(disp_gray, cmap='viridis') ax[0, 1].set_title('Gray Stereo Disparity') ax[1, 0].imshow(cv2.cvtColor(dataset.rgb[0].left, cv2.COLOR_BGR2RGB))