import cv2
import numpy as np
from pylab import *
import matplotlib.pyplot as plt
frame1 =cv2.imread(r"C:UsersAdministratorDesktop est1.png")
# cap = cv2.VideoCapture('GOPR1745.avi')
# ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[...,1] = 255
count=0
# ret, frame2 = cap.read()
frame2 =cv2.imread(r"C:UsersAdministratorDesktop est2.png")
next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prvs,next, 0.5, 3, 15, 3, 10, 1.2, 0)
print np.nanmax(flow)
delta_x = flow[:,:,0]
delta_y = flow[:,:,1]
u = delta_x
v = delta_y
contourf(u)
colorbar()
show()
contourf(v)
colorbar()
show()
# plt.figure()
# Q = plt.quiver(u[::20,::20],v[::-20,::-20])
# qk = plt.quiverkey(Q, 0.5, 0.92, 2, r'$2 frac{m}{s}$', labelpos='W',
# fontproperties={'weight': 'bold'})
# show()
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)
rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
cv2.imshow('frame2',rgb)