Skip to content

Commit

Permalink
add preFilterCap param for bm tunning
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzebin committed Apr 9, 2017
1 parent 7b06bc8 commit fcdddb5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
35 changes: 27 additions & 8 deletions capture_stereo_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,35 @@ def capture_stereo_images(left_cam, right_cam, width, height, image_pair_num, ou
cap_right.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

count = 0
flags = (cv2.CALIB_CB_FAST_CHECK)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
rows = 6
cols = 8
while count < image_pair_num:
ret1, img1 = cap_left.read()
ret2, img2 = cap_right.read()
# Wait until ret1 and ret2 are true since camera setup needs some time.
if ret1 and ret2:
cv2.imshow("stereo cameras", np.hstack((img1, img2)))

if cv2.waitKey(1) & 0xFF == ord('q'):
break
img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
found_left, corners_left = cv2.findChessboardCorners(img1_gray, (rows, cols), flags)
found_right, corners_right = cv2.findChessboardCorners(img2_gray, (rows, cols), flags)
if found_left and found_right:
# cv2.cornerSubPix(img1_gray, corners_left, (11, 11), (-1, -1), criteria)
# cv2.cornerSubPix(img2_gray, corners_right, (11, 11), (-1, -1), criteria)

img1_gray = cv2.cvtColor(img1_gray, cv2.COLOR_GRAY2BGR)
img2_gray = cv2.cvtColor(img2_gray, cv2.COLOR_GRAY2BGR)

cv2.drawChessboardCorners(img1_gray, (rows, cols), corners_left, found_left)
cv2.drawChessboardCorners(img2_gray, (rows, cols), corners_right, found_right)


# cv2.imshow("stereo cameras", np.hstack((img1, img2)))
cv2.imshow("stereo cameras", np.hstack((img1_gray, img2_gray)))

# When space key is pressed, save the image of left and right respectively
if cv2.waitKey(1) & 0xFF == ord(' '):
if ret1 and ret2 and cv2.waitKey(1) & 0xFF == ord(' '):
save_images(img1, img2, count)
count = count + 1

Expand All @@ -73,11 +90,13 @@ def print_prompt(output_dir):
print 'Images will be saved in %s' % output_dir

if __name__ == "__main__":
output_dir = './images/'
output_dir = './images_320x240/'
left_cam = 1
right_cam = 2
width = 480
height = 320
width = 320
height = 240
# width = 800
# height = 600
image_pair_num = 50

print_prompt(output_dir)
Expand Down
14 changes: 11 additions & 3 deletions depth_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self):
self.disp12_max_diff = 1
self.p1 = 8 * 3 * self.block_size**2
self.p2 = 32 * 3 * self.block_size**2
self.prefilter_cap = 0

def __str__(self):
return '\n'.join('%s: %s' % item for item in vars(self).items())
Expand All @@ -51,7 +52,8 @@ def save(self, file_name):
pickle.dump(self, open(file_name, "wb"))
print ('blockmathcer result saved into %s' % file_name)

bm = pickle.load(open('blockMatcher.pkl', 'rb'))
#bm = pickle.load(open('blockMatcher.pkl', 'rb'))
bm = BlockMatcher()

stereo = cv2.StereoSGBM_create(minDisparity = bm.min_disparity,
numDisparities = bm.num_disparities,
Expand All @@ -61,7 +63,8 @@ def save(self, file_name):
speckleRange = bm.speckle_range,
disp12MaxDiff = bm.disp12_max_diff,
P1 = bm.p1,
P2 = bm.p2
P2 = bm.p2,
preFilterCap = bm.prefilter_cap
)

def on_min_disparity(position):
Expand Down Expand Up @@ -119,6 +122,11 @@ def on_speckle_range(position):
stereo.setSpeckleRange(position)
bm.speckle_range = position

def on_prefilter_cap(position):
print ('on_prefilter_cap %s' % position)
stereo.setPreFilterCap(position)
bm.prefilter_cap = position

# window_size = 3
# min_disp = 16
# num_disp = 112-min_disp
Expand All @@ -142,7 +150,7 @@ def on_speckle_range(position):
cv2.createTrackbar("uniquenessRatio", "disparity", bm.uniqueness_ratio, 100, on_uniqueness_ratio)
#cv2.createTrackbar("speckleWindowSize", "disparity", bm.speckle_window_size, 1000, on_speckle_window_size)
#cv2.createTrackbar("speckleRange", "disparity", bm.speckle_range, 1000, on_speckle_range)

cv2.createTrackbar("preFilterCap", "disparity", bm.prefilter_cap, 100, on_prefilter_cap)
# while True:
# disparity = stereo.compute(imgL,imgR)
# # Normalize the image for representation
Expand Down
12 changes: 6 additions & 6 deletions show_cams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import cv2
import numpy as np

left_cam_id = 2
right_cam_id = 1
width = 680
height = 480
left_cam_id = 1
right_cam_id = 2
width = 480
height = 320

def show_camera(cam_id, width, height):
"""
Expand Down Expand Up @@ -57,5 +57,5 @@ def show_cameras(left, right, width, height):
cv2.destroyAllWindows()

if __name__ == "__main__":
# show_cameras(left_cam_id, right_cam_id, width, height)
show_camera(0, width, height)
show_cameras(left_cam_id, right_cam_id, width, height)
# show_camera(0, width, height)

0 comments on commit fcdddb5

Please sign in to comment.