Created
May 24, 2019 11:59
-
-
Save compustar/5d08f45b06cc7cb39cc44d38eb62ef1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import numpy as np | |
| import time | |
| # create video capture | |
| filename = "test.mp4" | |
| cap = cv2.VideoCapture(filename) | |
| h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
| w = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
| slit_pos = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) / 2) - 100 | |
| slit_size = 1 | |
| cv2.namedWindow('background', cv2.WINDOW_NORMAL) | |
| cv2.namedWindow('frame', cv2.WINDOW_NORMAL) | |
| # create a new image | |
| background = np.zeros((h, w * slit_size, 3), np.uint8) | |
| slit_num = 0 | |
| while(1): | |
| # read the frames | |
| success,frame = cap.read() | |
| if success: | |
| # copy a line from the frame to the background | |
| background[0:h,slit_num:slit_num+slit_size] = frame[0:h,slit_pos:slit_pos+slit_size] | |
| # increment the slit position | |
| slit_num+=slit_size | |
| # draw a line that shows the slit | |
| cv2.line(frame,(slit_pos,0),(slit_pos,h),(0,0,255),2) | |
| # show images | |
| cv2.imshow('background',background) | |
| cv2.imshow('frame',frame) | |
| #if key pressed is 'Esc', exit the loop | |
| key = cv2.waitKey(1) | |
| if key == 27: | |
| break | |
| # copy to the result and write to a file | |
| result = np.zeros((h, slit_num, 3), np.uint8) | |
| result[0:h,0:slit_num] = background[0:h,0:slit_num] | |
| cv2.imwrite(f"{filename}.{time.time()}.jpg", result) | |
| # clean up everything | |
| cv2.destroyAllWindows() | |
| cap.release() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment