Created
September 15, 2018 12:11
-
-
Save aoirint/811e9613998ae166146be28e4174def2 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 | |
cap = cv2.VideoCapture(0) | |
spoints = [] | |
def mouse_handler(event, x, y, flags, params): | |
if event == cv2.EVENT_LBUTTONUP: | |
if len(spoints) < 4: | |
spoints.append([x, y]) | |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) | |
cv2.namedWindow('img', cv2.WINDOW_NORMAL) | |
cv2.resizeWindow('img', (1080, 720)) | |
cv2.setMouseCallback('img', mouse_handler) | |
M = None | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if not ret: | |
time.sleep(0.01) | |
continue | |
if M is None: | |
for idx in range(len(spoints)): | |
pt = tuple(spoints[idx]) | |
cv2.circle(frame, pt, 4, (255, ), -1) | |
if idx > 0: | |
ppt = tuple(spoints[idx-1]) | |
cv2.line(frame, ppt, pt, (255, )) | |
if idx == len(spoints) - 1: | |
pt0 = tuple(spoints[0]) | |
cv2.line(frame, pt, pt0, (255, )) | |
else: | |
frame = cv2.warpAffine(frame, M, (1080, 720)) | |
cv2.imshow('img', frame) | |
key = cv2.waitKey(1) | |
if key == 13: | |
if M is None: | |
spoints = np.float32(spoints) | |
dpoints = np.float32([[0, 0], [0, 720], [1080, 720]]) | |
M = cv2.getAffineTransform(spoints, dpoints) | |
else: | |
M = 0 | |
spoints = [] | |
if key == 27: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment