现在可以计算视差和运动矢量了:)

This commit is contained in:
家才 王 2019-05-02 16:23:17 +08:00
parent 0493491d7d
commit 86aefe67b8

View File

@ -8,7 +8,8 @@ from matplotlib import pyplot as plt
def openVid(): def openVid():
fileName = input("video path:") fileName = "./vid/zootopia.mkv"
#fileName = input("video path:")
while not os.path.isfile(fileName): while not os.path.isfile(fileName):
print("file doesn't exist!") print("file doesn't exist!")
fileName = input("video path:") fileName = input("video path:")
@ -40,21 +41,42 @@ if __name__ == "__main__":
cap = openVid() cap = openVid()
frameRate = getFrameRate(cap) frameRate = getFrameRate(cap)
frameCount = getFrameCount(cap) frameCount = getFrameCount(cap)
ret, img = cap.read()
imgR = np.split(img, 2, 1)[1]
for frameID in range(int(frameRate), int(frameCount), int(frameRate*100)): prvs = cv2.cvtColor(imgR, cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(imgR)
hsv[..., 1] = 255
cap.set(cv2.CAP_PROP_POS_FRAMES,12000)
for frameID in range(int(cap.get(cv2.CAP_PROP_POS_FRAMES)), int(frameCount), int(frameRate/2)):
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 isSuccess: if isSuccess:
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow('img', img)
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)
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)
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.namedWindow("imgR", cv2.WINDOW_NORMAL)
cv2.imshow('imgR', imgR)
cv2.namedWindow("MotionVector",cv2.WINDOW_NORMAL)
cv2.imshow("MotionVector",bgr)
prvs = next
stereo = cv2.StereoSGBM_create(numDisparities=96, blockSize=11) stereo = cv2.StereoSGBM_create(numDisparities=96, blockSize=11)
disparity = stereo.compute(imgL, imgR) disparity = stereo.compute(imgL, imgR)
#cv2.waitKey(1)
plt.title("DepthMap") plt.title("DepthMap")
plt.imshow(disparity) plt.imshow(disparity)
plt.pause(0.5) plt.pause(0.5)
else:
print("video read error")
print("success")