函数包装,加注释
This commit is contained in:
parent
efc813073e
commit
3159ddd478
@ -17,6 +17,9 @@ TODO:
|
|||||||
...
|
...
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
# 打开视频文件
|
||||||
|
|
||||||
|
|
||||||
def openVid():
|
def openVid():
|
||||||
fileName = input("video path:")
|
fileName = input("video path:")
|
||||||
while not os.path.isfile(fileName):
|
while not os.path.isfile(fileName):
|
||||||
@ -28,9 +31,9 @@ def openVid():
|
|||||||
else:
|
else:
|
||||||
print("cannot open video.")
|
print("cannot open video.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 获取视频总帧数
|
||||||
def getFrameCount(cap):
|
def getFrameCount(cap):
|
||||||
if cap.isOpened():
|
if cap.isOpened():
|
||||||
return cap.get(cv2.CAP_PROP_FRAME_COUNT)
|
return cap.get(cv2.CAP_PROP_FRAME_COUNT)
|
||||||
@ -38,6 +41,8 @@ def getFrameCount(cap):
|
|||||||
print("cannot open video.")
|
print("cannot open video.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
# 获取帧速率
|
||||||
|
|
||||||
|
|
||||||
def getFrameRate(cap):
|
def getFrameRate(cap):
|
||||||
if cap.isOpened():
|
if cap.isOpened():
|
||||||
@ -46,6 +51,31 @@ def getFrameRate(cap):
|
|||||||
print("cannot open video.")
|
print("cannot open video.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
# 给出左右画面,计算景深
|
||||||
|
|
||||||
|
|
||||||
|
def getDepthMap(imgL, imgR):
|
||||||
|
stereo = cv2.StereoSGBM_create(numDisparities=64, blockSize=3)
|
||||||
|
return stereo.compute(imgL, imgR)
|
||||||
|
|
||||||
|
|
||||||
|
# 给出前后两帧,计算帧间运动矢量
|
||||||
|
def getMotionVector(prvs, next):
|
||||||
|
hsv = np.zeros_like(imgR) # 将运动矢量按hsv显示,以色调h表示运动方向,以明度v表示运动位移
|
||||||
|
hsv[..., 1] = 255 # 饱和度置为最高
|
||||||
|
|
||||||
|
# 转为灰度以计算光流
|
||||||
|
prvs = cv2.cvtColor(prvs, cv2.COLOR_BGR2GRAY)
|
||||||
|
next = cv2.cvtColor(next, cv2.COLOR_BGR2GRAY)
|
||||||
|
|
||||||
|
flow = cv2.calcOpticalFlowFarneback(
|
||||||
|
prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0) # 计算两帧间的光流,即运动矢量的直角坐标表示
|
||||||
|
mag, ang = cv2.cartToPolar(
|
||||||
|
flow[..., 0], flow[..., 1]) # 运动矢量的直角坐标表示转换为极坐标表示
|
||||||
|
hsv[..., 0] = ang*180/np.pi/2 # 角度对应色调
|
||||||
|
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) # 位移量对应明度
|
||||||
|
return hsv
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
cap = openVid()
|
cap = openVid()
|
||||||
@ -58,63 +88,55 @@ if __name__ == "__main__":
|
|||||||
print("video read error.")
|
print("video read error.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
#分割左右画面
|
# 分割左右画面
|
||||||
imgL = np.split(img, 2, 1)[0]
|
imgL = np.split(img, 2, 1)[0]
|
||||||
imgR = np.split(img, 2, 1)[1]
|
imgR = np.split(img, 2, 1)[1]
|
||||||
prvs = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY) #前一帧的右画面灰度,用于运动矢量计算
|
prvs = imgR # 上一帧的右画面,用于运动矢量计算
|
||||||
hsv = np.zeros_like(imgR) #将运动矢量按hsv显示,以色调h表示运动方向,以明度v表示运动位移
|
|
||||||
hsv[..., 1] = 255 #饱和度置为最高
|
|
||||||
|
|
||||||
#每秒取4帧进行计算
|
# 每秒取10帧进行计算
|
||||||
for frameID in range(round(cap.get(cv2.CAP_PROP_POS_FRAMES)), round(frameCount), round(frameRate/4)):
|
for frameID in range(round(cap.get(cv2.CAP_PROP_POS_FRAMES)), round(frameCount), round(frameRate/10)):
|
||||||
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:
|
||||||
print("video read error.")
|
print("video read error.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
#分割左右画面
|
# 分割左右画面
|
||||||
imgL = np.split(img, 2, 1)[0]
|
imgL = np.split(img, 2, 1)[0]
|
||||||
imgR = np.split(img, 2, 1)[1]
|
imgR = np.split(img, 2, 1)[1]
|
||||||
|
|
||||||
next = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY) #当前帧的右画面灰度,用于运动矢量计算
|
next = imgR # 当前帧的右画面,用于运动矢量计算
|
||||||
flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0) #计算两帧间的光流
|
hsv = getMotionVector(prvs, next)
|
||||||
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) #运动矢量的直角坐标表示转换为极坐标表示
|
|
||||||
hsv[..., 0] = ang*180/np.pi/2 #角度对应色调
|
|
||||||
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) #位移量对应明度
|
|
||||||
|
|
||||||
#计算深度图
|
# 计算深度图
|
||||||
stereo = cv2.StereoSGBM_create(numDisparities=64, blockSize=3)
|
disparity = getDepthMap(imgL, imgR)
|
||||||
disparity = stereo.compute(imgL, imgR)
|
|
||||||
print("time: ", round(frameID/frameRate,2))
|
# 显示计算结果
|
||||||
print("AVG depth: ",round(np.mean(disparity),2))
|
print("time: ", round(frameID/frameRate, 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()
|
print()
|
||||||
|
|
||||||
#当为demo模式时显示当前帧画面、运动矢量图和景深图
|
# 当为demo模式时显示当前帧画面、运动矢量图和景深图
|
||||||
if isDemo:
|
if isDemo:
|
||||||
#显示当前帧
|
# 显示当前帧
|
||||||
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
|
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
|
||||||
cv2.imshow('img', img)
|
cv2.imshow('img', img)
|
||||||
|
|
||||||
#显示当前帧的运动矢量的hsv表示
|
# 显示当前帧的运动矢量的hsv表示
|
||||||
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)
|
||||||
|
|
||||||
#显示当前帧的景深图
|
# 显示当前帧的景深图
|
||||||
plt.title("DepthMap")
|
plt.title("DepthMap")
|
||||||
plt.imshow(disparity)
|
plt.imshow(disparity)
|
||||||
plt.pause(0.5)
|
plt.pause(0.2)
|
||||||
|
|
||||||
prvs = next #当前帧覆盖上一帧,继续计算
|
prvs = next # 当前帧覆盖上一帧,继续计算
|
||||||
print("success")
|
print("success")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ffmpeg.input("./vid/avatar.mkv")
|
# ffmpeg.input("./vid/avatar.mkv")
|
||||||
|
|
||||||
# Motion Vector
|
# Motion Vector
|
||||||
|
Loading…
Reference in New Issue
Block a user