python学习之双目图像得到景深图(及遇到的问题)

双目视觉测距主要是利用了目标点在左右两幅图像上成像的横向坐标存在的视差,视差与目标点到成像平面的距离Z存在着反比关系:Z=fT/d

首先下载一些先关的软件包(有就不用安装了):

pip install --upgrade setuptools 
pip install numpy Matplotlib 
pip install opencv-python
pip install --user opencv-contrib-python

python学习之双目图像得到景深图(及遇到的问题)_第1张图片
需要说明的是;采用pip安装对网速的是有要求的,有时会导致安装失败,有时也可能是其他原因导致失败;解决方法之一就是继续重新安装,直到成功!
python学习之双目图像得到景深图(及遇到的问题)_第2张图片
在进行一个简单测试的代码是,出现问题:

raceback (most recent call last):
  File "G:\WangLuoRuanJian\Myapp\shiyan\chuankoult\theone\leftjiaoz.py", line 22, in 
    disparity = stereo.compute(img_L, img_R)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\calib3d\src\stereobm.cpp:1110: error: (-209:Sizes of input arguments do not match) All the images must have the same size in function 'cv::StereoBMImpl::compute'

python学习之双目图像得到景深图(及遇到的问题)_第3张图片
改变文件的读取路径的方式;将‘\’变为’/'就不会出错了!
测试结果:
python学习之双目图像得到景深图(及遇到的问题)_第4张图片
测试代码:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
u'''
Created on 2019年4月22日

@author: wuluo
'''
__author__ = 'wuluo'
__version__ = '1.0.0'
__company__ = u'重庆交大'
__updated__ = '2019-04-26'

import cv2
import numpy as np
from matplotlib import pyplot as plt

img_L = cv2.imread('G:\2018and2019two\qianrushi\one.jpg', 0)
img_R = cv2.imread('G:/2018and2019two/qianrushi/two.jpg', 0)

# OpenCV 3.0的函数为StereoBM_create
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
disparity = stereo.compute(img_L, img_R)

plt.subplot(121), plt.imshow(img_L, 'gray'), plt.title(
    'img_left'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(disparity, 'gray'), plt.title(
    'disparity'), plt.xticks([]), plt.yticks([])
plt.show()

if __name__ == "__main__":
    pass

从网址:http://vision.middlebury.edu/stereo/data/ 中任意选择两组双目图像(各left-right两张图片);

双目景深图代码:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
u'''
Created on 2019年4月22日

@author: wuluo
'''
__author__ = 'wuluo'
__version__ = '1.0.0'
__company__ = u'重庆交大'
__updated__ = '2019-04-26'

# 原始jpg已经畸变矫正
import numpy as np
import cv2
IMAGE_WIDTH = 800
IMAGE_HEIGHT = 600
capL = cv2.VideoCapture(2)
capR = cv2.VideoCapture(0)
imgL = np.zeros((IMAGE_WIDTH, IMAGE_HEIGHT, 3), np.uint8)
imgR = np.zeros((IMAGE_WIDTH, IMAGE_HEIGHT, 3), np.uint8)

stereo = None
opencv_measure_version = int(cv2.__version__.split('.')[0])
windowSize = 5
minDisp = 10
numDisp = 250 - minDisp

stereo = cv2.StereoSGBM_create(
    minDisparity=minDisp,
    numDisparities=numDisp,
    blockSize=16,
    P1=8 * 3 * windowSize**2,
    P2=32 * 3 * windowSize**2,
    disp12MaxDiff=1,
    uniquenessRatio=10,
    speckleWindowSize=100,
    speckleRange=32
)
capL.set(cv2.CAP_PROP_FRAME_WIDTH,  IMAGE_WIDTH)
capL.set(cv2.CAP_PROP_FRAME_HEIGHT, IMAGE_HEIGHT)
capR.set(cv2.CAP_PROP_FRAME_WIDTH,  IMAGE_WIDTH)
capR.set(cv2.CAP_PROP_FRAME_HEIGHT, IMAGE_HEIGHT)
imgL = cv2.imread('G:/2018and2019two/qianrushi/one.jpg')
imgR = cv2.imread('G:/2018and2019two/qianrushi/two.jpg')

# create gray images
imgGrayL = cv2.cvtColor(imgL, cv2.COLOR_BGR2GRAY)
imgGrayR = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)
# calculate histogram
imtGrayL = cv2.equalizeHist(imgGrayL)
imtGrayR = cv2.equalizeHist(imgGrayR)

# through gausiann filter
imgGrayL = cv2.GaussianBlur(imgGrayL, (5, 5), 0)
imgGrayR = cv2.GaussianBlur(imgGrayR, (5, 5), 0
cv2.imshow("image left", imgGrayL)
cv2.imshow("image right", imgGrayR)

# calculate disparity
disparity = stereo.compute(imgGrayL, imgGrayR).astype(np.float32) / 16
disparity = (disparity - minDisp) / numDisp
cv2.imshow("disparity", disparity)
cv2.waitKey(0)

if __name__ == "__main__":
    pass

运行的结果图:
python学习之双目图像得到景深图(及遇到的问题)_第5张图片

你可能感兴趣的:(wuluo,python3)