注释注释注释

This commit is contained in:
家才 王 2019-05-07 18:29:36 +08:00
parent 8831170ba8
commit d2f300ec7c

View File

@ -5,6 +5,7 @@ import cv2
import os import os
import sys import sys
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
from scipy import stats
''' '''
@ -19,10 +20,15 @@ TODO:
# 打开视频文件 # 打开视频文件
def openVid(): def openVid():
fileName = input("video path:") fileName = input("video path: ./vid/")
fileName = "./vid/" + fileName
while not os.path.isfile(fileName): while not os.path.isfile(fileName):
if os.path.isfile(fileName + ".mkv"):
fileName = fileName + ".mkv"
break
print("file doesn't exist!") print("file doesn't exist!")
fileName = input("video path:") fileName = input("video path: ./vid/")
fileName = "./vid/" + fileName
cap = cv2.VideoCapture(fileName) cap = cv2.VideoCapture(fileName)
if cap.isOpened(): if cap.isOpened():
return cap return cap
@ -49,7 +55,7 @@ def getFrameRate(cap):
# 给出左右画面,计算景深 # 给出左右画面,计算景深
def getDepthMap(imgL, imgR): def getDepthMap(imgL, imgR):
stereo = cv2.StereoSGBM_create(numDisparities=64, blockSize=3) stereo = cv2.StereoSGBM_create(numDisparities=32, blockSize=3)
return stereo.compute(imgL, imgR) return stereo.compute(imgL, imgR)
@ -76,7 +82,7 @@ if __name__ == "__main__":
isDemo = int(input("is Demo(0/1)?")) isDemo = int(input("is Demo(0/1)?"))
frameRate = getFrameRate(cap) frameRate = getFrameRate(cap)
frameCount = getFrameCount(cap) frameCount = getFrameCount(cap)
framesCalculated = 0
isSuccess, img = cap.read() isSuccess, img = cap.read()
if not isSuccess: if not isSuccess:
print("video read error.") print("video read error.")
@ -87,8 +93,8 @@ if __name__ == "__main__":
imgR = np.split(img, 2, 1)[1] imgR = np.split(img, 2, 1)[1]
prvs = imgR # 上一帧的右画面,用于运动矢量计算 prvs = imgR # 上一帧的右画面,用于运动矢量计算
# 每秒取10帧进行计算 # 每秒取4帧进行计算
for frameID in range(round(cap.get(cv2.CAP_PROP_POS_FRAMES)), round(frameCount), round(frameRate/10)): for frameID in range(round(cap.get(cv2.CAP_PROP_POS_FRAMES)), round(frameCount), round(frameRate/4)):
cap.set(cv2.CAP_PROP_POS_FRAMES, frameID) cap.set(cv2.CAP_PROP_POS_FRAMES, frameID)
isSuccess, img = cap.read() isSuccess, img = cap.read()
if not isSuccess: if not isSuccess:
@ -105,10 +111,17 @@ if __name__ == "__main__":
# 计算深度图 # 计算深度图
disparity = getDepthMap(imgL, imgR) disparity = getDepthMap(imgL, imgR)
framesCalculated += 1
# 显示计算结果 # 显示计算结果
print("time: ", round(frameID/frameRate, 2)) print("time: ", round(frameID/frameRate, 2))
print("AVG depth: ", round(np.mean(disparity), 2)) print("AVG depth: ", round(np.mean(disparity), 2)) # 景深的平均值,偏大则意味着负视差,可能不适
print("AVG motion: ", round(np.mean(hsv[..., 2]), 2)) print("AVG motion: ", round(np.mean(hsv[..., 2]), 2)) # 运动矢量大小的平均值,可判断画面大致上是否稳定
print("Mode depth: ", stats.mode(disparity.reshape(-1))[0][0]) # 景深的众数,由于景深基本不连续,众数意义不大
print("Mode motion: ", stats.mode(hsv[..., 2].reshape(-1))[0][0]) # 运动矢量大小的众数一般为0若较大说明画面中存在较大面积的快速运动可能不适
print("STD depth: ", round(np.std(disparity),2)) # 景深的标准差若偏大说明景深范围较大可能不适但同时也是3D感更强的特征
print("STD motion: ", round(np.std(hsv[...,2]),2)) # 运动矢量大小的标准差,若偏大说明各部分运动比较不一致,可能需要结合运动矢量的方向作进一步判断,若存在较复杂的运动形式,则可能不适
print() print()
# 当为demo模式时显示当前帧画面、运动矢量图和景深图 # 当为demo模式时显示当前帧画面、运动矢量图和景深图
@ -121,12 +134,16 @@ if __name__ == "__main__":
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) # hsv转为rgb用于显示 bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) # hsv转为rgb用于显示
cv2.namedWindow("MotionVector", cv2.WINDOW_NORMAL) cv2.namedWindow("MotionVector", cv2.WINDOW_NORMAL)
cv2.imshow("MotionVector", bgr) cv2.imshow("MotionVector", bgr)
# cv2.waitKey(1)
# 显示当前帧的景深图 # 显示当前帧的景深图
plt.title("DepthMap") plt.title("DepthMap")
plt.imshow(disparity) plt.imshow(disparity)
# 运动矢量的直方图,方便查看数值
# plt.title("MotionVector")
# plt.imshow(hsv[...,2])
# plt.show()
plt.pause(0.2) plt.pause(0.2)
input("press to continue")
prvs = next # 当前帧覆盖上一帧,继续计算 prvs = next # 当前帧覆盖上一帧,继续计算
print("success") print("success")