diff --git a/.gitignore b/.gitignore index 51f7ae9..7f6afa4 100644 --- a/.gitignore +++ b/.gitignore @@ -330,3 +330,4 @@ ASALocalRun/ .mfractor/ /1.png /vid +/pic diff --git a/MV.py b/MV.py index 1702520..0416140 100644 --- a/MV.py +++ b/MV.py @@ -8,7 +8,7 @@ prvs = cv.cvtColor(frame1, cv.COLOR_BGR2GRAY) hsv = np.zeros_like(frame1) hsv[..., 1] = 255 print("jump ahead") -cap.set(cv.CAP_PROP_POS_FRAMES, 59760) +cap.set(cv.CAP_PROP_POS_FRAMES, 12000) print("jump done") while(1): @@ -22,9 +22,9 @@ while(1): hsv[..., 0] = ang*180/np.pi/2 hsv[..., 2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX) bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR) - cv.namedWindow("frame2", cv.WINDOW_NORMAL) + cv.namedWindow("MotionVector", cv.WINDOW_NORMAL) cv.namedWindow("frame", cv.WINDOW_NORMAL) - cv.imshow('frame2', bgr) + cv.imshow('MotionVector', bgr) cv.imshow("frame", frame2) cv.waitKey(1) #k = cv.waitKey(0.1) & 0xff diff --git a/StereoVidComfort.py b/StereoVidComfort.py index 421d0fe..19ce69b 100644 --- a/StereoVidComfort.py +++ b/StereoVidComfort.py @@ -6,10 +6,18 @@ import os import sys from matplotlib import pyplot as plt +''' +TODO: + 0:读取视频 √ + 1:获取视差 √ + 2:获取运动矢量 √ + 3:确定舒适度 + 4:加舒适度水印 + ... +''' def openVid(): - fileName = "./vid/zootopia.mkv" - #fileName = input("video path:") + fileName = input("video path:") while not os.path.isfile(fileName): print("file doesn't exist!") fileName = input("video path:") @@ -41,40 +49,56 @@ if __name__ == "__main__": cap = openVid() frameRate = getFrameRate(cap) frameCount = getFrameCount(cap) - ret, img = cap.read() + + isSuccess, img = cap.read() + if not isSuccess: + print("video read error.") + sys.exit() + + imgL = np.split(img, 2, 1)[0] imgR = np.split(img, 2, 1)[1] 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,52000) + + for frameID in range(int(cap.get(cv2.CAP_PROP_POS_FRAMES)), int(frameCount), int(frameRate/4)): cap.set(cv2.CAP_PROP_POS_FRAMES, frameID) isSuccess, img = cap.read() - if isSuccess: + if not isSuccess: + print("video read error.") + sys.exit() - imgL = np.split(img, 2, 1)[0] - 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) + imgL = np.split(img, 2, 1)[0] + 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("img", cv2.WINDOW_NORMAL) + cv2.imshow('img', img) + cv2.namedWindow("MotionVector",cv2.WINDOW_NORMAL) + cv2.imshow("MotionVector",bgr) + prvs = next + + + stereo = cv2.StereoSGBM_create(numDisparities=96, blockSize=11) + disparity = stereo.compute(imgL, imgR) + print("time: ", frameID/frameRate) + print("AVG depth: ",np.mean(disparity)) + print("AVG motion: ",np.mean(hsv[...,2])) + print() + cv2.waitKey(1) + #cv2.waitKey(1) + #plt.title("DepthMap") + #plt.imshow(disparity) + #plt.pause(0.5) - 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) - disparity = stereo.compute(imgL, imgR) - #cv2.waitKey(1) - plt.title("DepthMap") - plt.imshow(disparity) - plt.pause(0.5) - else: - print("video read error") print("success") diff --git a/picMV.py b/picMV.py new file mode 100644 index 0000000..1e16bba --- /dev/null +++ b/picMV.py @@ -0,0 +1,22 @@ +import cv2 as cv +import numpy as np + +img = cv.imread("./pic/2.jpg") +imgL = np.split(img, 2, 1)[0] +imgR = np.split(img, 2, 1)[1] + +prvs = cv.cvtColor(imgL, cv.COLOR_BGR2GRAY) +hsv = np.zeros_like(imgL) +hsv[..., 1] = 255 + +next = cv.cvtColor(imgR, cv.COLOR_BGR2GRAY) +flow = cv.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0) +mag, ang = cv.cartToPolar(flow[..., 0], flow[..., 1]) +hsv[..., 0] = ang*180/np.pi/2 +hsv[..., 2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX) +bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR) +cv.namedWindow("MotionVector", cv.WINDOW_NORMAL) +cv.namedWindow("frame", cv.WINDOW_NORMAL) +cv.imshow('MotionVector', bgr) +cv.imshow("frame", img) +cv.waitKey(100) \ No newline at end of file diff --git a/pic_en/1.jpg b/pic_en/1.jpg deleted file mode 100644 index 1d74f22..0000000 Binary files a/pic_en/1.jpg and /dev/null differ diff --git a/pic_en/10.jpg b/pic_en/10.jpg deleted file mode 100644 index 8814a7a..0000000 Binary files a/pic_en/10.jpg and /dev/null differ diff --git a/pic_en/2.jpg b/pic_en/2.jpg deleted file mode 100644 index 2457db4..0000000 Binary files a/pic_en/2.jpg and /dev/null differ diff --git a/pic_en/3.jpg b/pic_en/3.jpg deleted file mode 100644 index 130e573..0000000 Binary files a/pic_en/3.jpg and /dev/null differ diff --git a/pic_en/4.jpg b/pic_en/4.jpg deleted file mode 100644 index a519b09..0000000 Binary files a/pic_en/4.jpg and /dev/null differ diff --git a/pic_en/5.jpg b/pic_en/5.jpg deleted file mode 100644 index dee5616..0000000 Binary files a/pic_en/5.jpg and /dev/null differ diff --git a/pic_en/6.jpg b/pic_en/6.jpg deleted file mode 100644 index e094f92..0000000 Binary files a/pic_en/6.jpg and /dev/null differ diff --git a/pic_en/7.jpg b/pic_en/7.jpg deleted file mode 100644 index 7232ffe..0000000 Binary files a/pic_en/7.jpg and /dev/null differ diff --git a/pic_en/8.jpg b/pic_en/8.jpg deleted file mode 100644 index 2956a74..0000000 Binary files a/pic_en/8.jpg and /dev/null differ diff --git a/pic_en/9.jpg b/pic_en/9.jpg deleted file mode 100644 index 5b941be..0000000 Binary files a/pic_en/9.jpg and /dev/null differ