OpenCV的基础操作可以看看这个:http://wiki.opencv.org.cn/index.php/%E9%A6%96%E9%A1%B5
可以看看这篇博客
http://blog.csdn.net/taily_duan/article/details/52165458
测试图片可以从这里找
https://www.raspberrypi.org/blog/real-time-depth-perception-with-the-compute-module/
以下两行代码似乎因为版本更替原因都无效了
deparsity = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET,ndisparities=16, SADWindowSize=15)
deparsity = cv2.createStereoBM(numDisparities=16, blockSize=25)
测试代码如下:
# -*- coding: UTF-8 -*-
import numpy as np
import cv2
from matplotlib import pyplot as plt #import the library
left_img = cv2.imread('Tsukuba_L.png',0) #read the left_image(grey)
left_img_rgb = cv2.imread('Tsukuba_L.png',cv2.IMREAD_COLOR) #read the left_image(colorful)
right_img = cv2.imread('Tsukuba_R.png',0) #read the right_image(grey)
right_img_rgb = cv2.imread('Tsukuba_R.png',cv2.IMREAD_COLOR) #read the right_image(colorful)
'''
sp = left_img_rgb.shape #get the size of the image
print("width ; %d\nheight : %d\nchannels : %d\n" % (sp[0],sp[1],sp[2]))
'''
matching=cv2.StereoBM_create(numDisparities=16, blockSize=15) #set the parameters of the stereo
disparity = matching.compute(left_img,right_img) #make the depth map
cv2.imshow("left_img",left_img_rgb) #show the left_image(colorful)
cv2.imshow("right_img",right_img_rgb) #show the right_image(colorful)
plt.imshow(disparity,'gray') #show the depth map(grey)
plt.show()
参考上述博客:
可利用三角形相似和等比定理计算:
图片见onenote