Created
January 22, 2017 09:17
-
-
Save wermarter/b98be6d086af80a3b3942d63715ed843 to your computer and use it in GitHub Desktop.
Rotation, Mouse events, Perspectives and more...
This file contains 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, time, sys | |
from random import * | |
import numpy as np | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
xi, yi = 0, 0 | |
def func(event, x, y, _, __): | |
global cap, img, xi, yi | |
xi, yi = x, y | |
if event == cv2.EVENT_LBUTTONUP: | |
___, img = cap.read() | |
cap.release() | |
cv2.destroyAllWindows() | |
downed = False | |
pts = list() | |
def rotate(event, x, y, _, __): | |
global _img, wi, hi | |
if event == cv2.EVENT_LBUTTONUP: | |
M = cv2.getRotationMatrix2D((wi/2, hi/2),-90,1) | |
_img = cv2.warpAffine(_img, M, (wi,hi)) | |
cv2.imshow('Result', _img) | |
def align(pts): | |
dst = lambda pt1, pt2: ((pt1[0]-pt2[0])**2+(pt1[1]-pt2[1])**2)**0.5 | |
ver, hor = 0.0, 0.0 | |
left, right = list(), list() | |
ver = sum([pt[0] for pt in pts])/4 | |
for pt in pts: | |
if pt[0]>ver: | |
right.append(pt) | |
else: | |
left.append(pt) | |
if len(right) == len(left): | |
ul, dl = left[0], left[1] | |
if left[0][1]>left[1][1]: | |
ul, dl = dl, ul | |
ur, dr = right[0], right[1] | |
if right[0][1]>right[1][1]: | |
ur, dr = dr, ur | |
else: | |
up, down = list(), list() | |
hor = sum([pt[1] for pt in pts])/4 | |
for pt in pts: | |
if pt[1] > hor: | |
down.append(pt) | |
else: | |
up.append(pt) | |
dl, dr = down[0], down[1] | |
if down[0][0]>down[1][0]: | |
dl, dr = dr, dl | |
ul, ur = up[0], up[1] | |
if up[0][0]>up[1][0]: | |
ul, ur = ur, ul | |
return np.float32([ul, ur, dr, dl]), int(dst(ul, ur)), int(dst(ul, dl)) | |
def func2(event, x, y, _, __): | |
global img, w, h, downed, ix, iy, pts, _img, wi, hi | |
if event == cv2.EVENT_LBUTTONDOWN: | |
downed = True | |
ix, iy = x, y | |
elif event == cv2.EVENT_LBUTTONUP: | |
downed = False | |
cv2.imshow('Image', img) | |
elif event == cv2.EVENT_MOUSEMOVE: | |
if downed: | |
M = np.float32([[1, 0, x-ix], [0, 1, y-iy]]) | |
cv2.imshow('Image', cv2.warpAffine(img, M, (w, h))) | |
elif event == cv2.EVENT_RBUTTONUP: | |
pts.append((x, y)) | |
cv2.circle(img, (x, y), 2, (0, 0, 255), -1) | |
cv2.imshow('Image', img) | |
if len(pts) == 4: | |
pts, wi, hi = align(pts) | |
cv2.line(img, tuple(pts[0]), tuple(pts[1]), (255, 0, 0), 2) | |
cv2.line(img, tuple(pts[0]), tuple(pts[3]), (255, 0, 0), 2) | |
cv2.line(img, tuple(pts[2]), tuple(pts[1]), (255, 0, 0), 2) | |
cv2.line(img, tuple(pts[2]), tuple(pts[3]), (255, 0, 0), 2) | |
cv2.imshow('Image', img) | |
pts_ = np.float32([(0, 0), (wi, 0), (wi, hi), (0, hi)]) | |
M = cv2.getPerspectiveTransform(pts, pts_) | |
_img = cv2.warpPerspective(img,M,(wi,hi)) | |
cv2.destroyWindow('Image') | |
cv2.imshow('Result', _img) | |
cv2.setMouseCallback('Result', rotate) | |
cap = cv2.VideoCapture(0) | |
cv2.namedWindow('Camera') | |
cv2.setMouseCallback('Camera', func) | |
w, h = int(cap.get(3)), int(cap.get(4)) | |
pos = (15, h-20) | |
while cap.isOpened(): | |
_, frame = cap.read() | |
cv2.putText(frame, 'Click to Capture IMAGE', pos, font, 1, (0, 0, 0), 2, cv2.LINE_AA) | |
cv2.putText(frame, str(xi)+','+str(yi), (0, 25), font, 1, (0, 0, 0), 1, cv2.LINE_AA) | |
cv2.imshow('Camera', frame) | |
if cv2.waitKey(1) & 0xFF == 27: | |
cap.release() | |
break | |
cv2.namedWindow('Image') | |
cv2.setMouseCallback('Image', func2) | |
cv2.imshow('Image', img) | |
if cv2.waitKey(0) & 0xFF == 27: | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment