下面的我的python代码旨在获得准确度很高的实时视差图。这是我的代码:
import cv2
import numpy as np
import sys
from matplotlib import pyplot as plt
cap = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(2)
# Check if camera opened successfully
if (cap.isOpened()== False and cap2.isOpened()== False):
print("Error opening video stream or file")
# Read until video is completed
while(cap.isOpened() and cap2.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
ret2, frame2 = cap2.read()
if ret == True and ret2 == True:
# Display the resulting frame
cv2.imshow('Frame',frame)
cv2.imshow('Frame2',frame2)
framegray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame2gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray1', framegray)
cv2.imshow('Gray2', frame2gray)
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
disparity = stereo.compute(framegray, frame2gray)
#minVal, maxVal, randval, dontcare = cv2.minMaxLoc(disparity)
#disparity.convertTo(dispweird, CV_8UC1, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));
#disparitywithcolor = cv2.applyColorMap(dispweird, cv2.COLORMAP_JET)
plt.imshow(disparity,'disparity')
plt.show()
delay(1000)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
这是我的命令行输出:
跟踪(最近一次通话最近一次):在plt.imshow中的文件“ cameratest.py”,第31行(视差,“视差”),文件“ C:\ Users \ Adithya Kumar \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ pyplot.py“,第3205行,在imshow ** kwargs中)文件“ C:\ Users \ Adithya Kumar \ AppData \ Local \程序\ Python \ Python36-32 \ lib \ site-packages \ matplotlib__init __。py“,行1855,在内部返回函数(ax,* args,** kwargs)文件“ C:\ Users \ Adithya Kumar \ AppData \ Local \程序\ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ axes_axes.py”,第5485行,在imshow resample = resample,** kwargs)中,文件“ C:\ Users \ Adithya Kumar \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ image.py“,第824行,在 init ** kwargs中文件“ C:\ Users \ Adithya Kumar \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ image.py”,第228行,位于 init cm.ScalarMappable中。初始化((自身,规范,cmap)文件“ C:\ Users \ Adithya Kumar \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ cm.py” ,在 init 中的第203行,self.cmap = get_cmap(cmap)文件“ C:\ Users \ Adithya Kumar \ AppData \ Local \ Programs \ Python \ Python36-32 \ li b \ site-packages \ matplotlib \ cm.py“,第168行,位于get_cmap%(名称,','。join(sorted(cmap_d))))中)ValueError:无法识别色图差异。可能的值包括:Accent,Accent_r,Blues,Blues_r,BrBG,BrBG_r,BuGn,BuGn_r,BuPu,BuPu_r,CMRmap,CMRmap_r,Dark2,Dark2_r,GnBu,GnBu_r,Greens,Greens_r,Greys,Greys_r,Or Oranges_r,PRGn,PRGn_r,成对,成对_r,Pastel1,Pastel1_r,Pastel2,Pastel2_r,PiYG,PiYG_r,PuBu,PuBuGn,PuBuGn_r,PuBu_r,PuOr,PuOr_r,PuRd,PuRd_r,Purs,Purs,Purs,Purs,Purs RdPu,RdPu_r,RdYlBu,RdYlBu_r,RdYlGn,RdYlGn_r,Reds,Reds_r,Set1,Set1_r,Set2,Set2_r,Set3,Set3_r,Spectral,Spectral_r,Wistia,Wistia_r,YlGn,YlGn,YlGn,YlGnBu,YlGnBu,YlGnBu YlOrRd_r,afmhot,afmhot_r,秋季,autumn_r,二进制,binary_r,骨骼,bone_r,brg,brg_r,bwr,bwr_r,cividis,cividis_r,cool,cool_r,coolwarm,coolwarm_r,铜,copper_r_,cubehelix,cubichelix gist_earth,gist_earth_r,gist_gray,gist_gray_r,gist_heat,gist_heat_r,gist_ncar,gist_ncar_r,gist_rainbow,gist_rainbow_r,gist_stern,gist_stern_r,gist_yarg,gist_yarg_ r,gnuplot,gnuplot2,gnuplot2_r,gnuplot_r,灰色,gray_r,热,hot_r,hsv,hsv_r,地狱,地狱,喷射,喷射_r,岩浆,岩浆_r,nipy_spectral,nipy_spectral_r,大洋洲,ocean_r,粉红色棱镜,棱镜_r,彩虹,彩虹_r,地震,地震_r,弹簧,弹簧_r,夏季,夏季_r,tab10,tab10_r,tab20,tab20_r,tab20b,tab20b_r,tab20c,tab20c_r,地形,terrain_r,viridis,viridis_r,冬季,winter_r
您可以从注释掉的代码中看到,我试图将视差图输出从stereoBM转换为图像,以便为它着色,但是由于某些原因,这是行不通的。如果您有解决此问题的方法,请告诉我。