Created
September 4, 2016 05:43
-
-
Save smeschke/204e071a72fe8822f706ed8ffc654c27 to your computer and use it in GitHub Desktop.
augmented reality
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 | |
#path to video paths | |
#source_path = '/home/sm/Desktop/VIRB0054.MP4' | |
source_path = 0 | |
save_path = '/home/sm/Desktop/mandt_effect.avi' | |
image_path = '/home/sm/Desktop/map.png' | |
frame_rate, resolution = 25.0, (640,480) | |
text = cv2.imread(image_path) | |
#write outfile | |
fourcc = cv2.VideoWriter_fourcc(*'XVID') | |
out = cv2.VideoWriter(save_path,fourcc, frame_rate, resolution) | |
#-----------------------------------------------------------------------------# | |
#press a to pause and click on light | |
# press s to get rid of tracker | |
#-----------------------------------------------------------------------------# | |
#global variables to work with mouse callback function | |
global f, points | |
f, points = 0, [] | |
# mouse callback function | |
def draw_circle(event,x,y,flags,param): | |
if event == cv2.EVENT_LBUTTONDOWN: | |
global points | |
points.append((x,y)) | |
print x,y | |
#returns coordinates | |
def get_coords(p1): | |
try: return int(p1[0][0][0]), int(p1[0][0][1]) | |
except: return int(p1[0][0]), int(p1[0][1]) | |
#warps image | |
def warp(p1,p2,p3,img): | |
rows, cols, _ = img.shape | |
pts1 = np.float32([[100,0], [100,600], [700,0]]) | |
pts2 = np.float32([p1,p2,p3]) | |
cv2.circle(img,(0,0),16,(0,120,255),-1) | |
cv2.circle(img,(320,480),16,(0,120,255),-1) | |
cv2.circle(img,(640,0),16,(0,120,255),-1) | |
M = cv2.getAffineTransform(pts1,pts2) | |
dst = cv2.warpAffine(img,M,(640,480)) | |
return dst | |
#capture source video | |
cap = cv2.VideoCapture(source_path) | |
# Parameters for lucas kanade optical flow | |
lk_params = dict( winSize = (50,50), | |
maxLevel = 25, | |
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) | |
#setup mouse callback function | |
cv2.namedWindow('image') | |
cv2.setMouseCallback('image',draw_circle) | |
def get_coords(p1): | |
try: return int(p1[0][0][0]), int(p1[0][0][1]) | |
except: return int(p1[0][0]), int(p1[0][1]) | |
# Create some random colors | |
color = np.random.randint(0,255,(100,3)) | |
# Take first frame and find corners in it | |
ret, old_frame = cap.read() | |
cv2.imshow('image',old_frame) | |
cv2.waitKey(0) | |
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) | |
p0_list = [] | |
for point in points: | |
#p0 = np.array([[[point[0],point[1]]]], np.float32) | |
p0 = np.array([[point]], np.float32) | |
p0_list.append(p0) | |
# Create a mask image for drawing purposes | |
mask = np.zeros_like(old_frame) | |
while(1): | |
f+=1 | |
ret,frame = cap.read() | |
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
new_p0_list = [] | |
corners = [] | |
for p0 in p0_list: | |
# calculate optical flow | |
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) | |
new_p0_list.append(p1) | |
p = get_coords(p1) | |
#cv2.circle(frame, p, 5, (255,0,0),-1) | |
corners.append(p) | |
if len(corners) == 3: | |
pts = np.array([corners], np.int32) | |
pts = pts.reshape((-1,1,2)) | |
p0_list = new_p0_list | |
p1 = get_coords(p0_list[0]) | |
p2 = get_coords(p0_list[1]) | |
p3 = get_coords(p0_list[2]) | |
cv2.circle(frame,p1,6,(0,0,255),-1) | |
cv2.circle(frame,p2,6,(0,0,255),-1) | |
cv2.circle(frame,p3,6,(0,0,255),-1) | |
cv2.imshow('image',frame) | |
warp_image =warp(p1,p2,p3,text) | |
cv2.imshow('warp', warp_image) | |
out.write(frame) | |
k = cv2.waitKey(1) & 0xff | |
if k == 97: | |
cv2.waitKey(0) | |
p0_list = [] | |
p0_list.append(np.array([[points[-0]]], np.float32)) | |
p0_list.append(np.array([[points[-1]]], np.float32)) | |
p0_list.append(np.array([[points[-2]]], np.float32)) | |
print points[-3:] | |
print p0_list | |
if k == 115: | |
p0_list = [] | |
# Now update the previous frame and previous points | |
old_gray = frame_gray.copy() | |
# Convert BGR to HSV | |
hsv = cv2.cvtColor(warp_image, cv2.COLOR_BGR2HSV) | |
# define range of green color in HSV | |
lower_blue = np.array([5,5,5]) | |
upper_blue = np.array([255,255,255]) | |
# Threshold the HSV image to get only green colors | |
mask = cv2.inRange(hsv, lower_blue, upper_blue) | |
img1 = frame | |
img2 = warp_image | |
# I want to put logo on top-left corner, So I create a ROI | |
rows,cols,channels = img2.shape | |
roi = img1[0:rows, 0:cols ] | |
mask_inv = cv2.bitwise_not(mask) | |
# Now black-out the area of logo in ROI | |
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv) | |
# Take only region of logo from logo image. | |
img2_fg = cv2.bitwise_and(img2,img2,mask = mask) | |
# Put logo in ROI and modify the main image | |
dst = cv2.add(img1_bg,img2_fg) | |
img1[0:rows, 0:cols ] = dst | |
#cv2.imshow('mask', mask) | |
cv2.imshow('res',dst) | |
out.write(dst) | |
cv2.destroyAllWindows() | |
cap.release() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment