Skip to content

Instantly share code, notes, and snippets.

@abda53
Created December 27, 2018 20:29
Show Gist options
  • Save abda53/ec8b182278354496feb492c9ef7d8e7e to your computer and use it in GitHub Desktop.
Save abda53/ec8b182278354496feb492c9ef7d8e7e to your computer and use it in GitHub Desktop.
import numpy
fx = 942.8 # lense focal length
baseline = 54.8 # distance in mm between the two cameras
disparities = 64 # num of disparities to consider
block = 15 # block size to match
units = 0.001 # depth units
for i in xrange(block, left.shape[0] - block - 1):
for j in xrange(block + disparities, left.shape[1] - block - 1):
ssd = numpy.empty([disparities, 1])
# calc SSD at all possible disparities
l = left[(i - block):(i + block), (j - block):(j + block)]
for d in xrange(0, disparities):
r = right[(i - block):(i + block), (j - d - block):(j - d + block)]
ssd[d] = numpy.sum((l[:,:]-r[:,:])**2)
# select the best match
disparity[i, j] = numpy.argmin(ssd)
# Convert disparity to depth
depth = np.zeros(shape=left.shape).astype(float)
depth[disparity > 0] = (fx * baseline) / (units * disparity[disparity > 0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment