diff --git a/StereoVidComfort.py b/StereoVidComfort.py index 8e9dee3..cbdebe5 100644 --- a/StereoVidComfort.py +++ b/StereoVidComfort.py @@ -5,6 +5,7 @@ import cv2 import os import sys from matplotlib import pyplot as plt +from scipy import stats ''' @@ -19,10 +20,15 @@ TODO: # 打开视频文件 def openVid(): - fileName = input("video path:") + fileName = input("video path: ./vid/") + fileName = "./vid/" + fileName while not os.path.isfile(fileName): + if os.path.isfile(fileName + ".mkv"): + fileName = fileName + ".mkv" + break print("file doesn't exist!") - fileName = input("video path:") + fileName = input("video path: ./vid/") + fileName = "./vid/" + fileName cap = cv2.VideoCapture(fileName) if cap.isOpened(): return cap @@ -49,7 +55,7 @@ def getFrameRate(cap): # 给出左右画面,计算景深 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) @@ -76,7 +82,7 @@ if __name__ == "__main__": isDemo = int(input("is Demo(0/1)?")) frameRate = getFrameRate(cap) frameCount = getFrameCount(cap) - + framesCalculated = 0 isSuccess, img = cap.read() if not isSuccess: print("video read error.") @@ -87,8 +93,8 @@ if __name__ == "__main__": imgR = np.split(img, 2, 1)[1] prvs = imgR # 上一帧的右画面,用于运动矢量计算 - # 每秒取10帧进行计算 - for frameID in range(round(cap.get(cv2.CAP_PROP_POS_FRAMES)), round(frameCount), round(frameRate/10)): + # 每秒取4帧进行计算 + 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) isSuccess, img = cap.read() if not isSuccess: @@ -105,10 +111,17 @@ if __name__ == "__main__": # 计算深度图 disparity = getDepthMap(imgL, imgR) + framesCalculated += 1 + # 显示计算结果 print("time: ", round(frameID/frameRate, 2)) - print("AVG depth: ", round(np.mean(disparity), 2)) - print("AVG motion: ", round(np.mean(hsv[..., 2]), 2)) + print("AVG depth: ", round(np.mean(disparity), 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() # 当为demo模式时显示当前帧画面、运动矢量图和景深图 @@ -121,12 +134,16 @@ if __name__ == "__main__": bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) # hsv转为rgb用于显示 cv2.namedWindow("MotionVector", cv2.WINDOW_NORMAL) cv2.imshow("MotionVector", bgr) - + # cv2.waitKey(1) # 显示当前帧的景深图 plt.title("DepthMap") plt.imshow(disparity) + # 运动矢量的直方图,方便查看数值 + # plt.title("MotionVector") + # plt.imshow(hsv[...,2]) + # plt.show() plt.pause(0.2) - + input("press to continue") prvs = next # 当前帧覆盖上一帧,继续计算 print("success")