Skip to content
Browse files

preliminary optical flow monitoring between frames

  • Loading branch information...
1 parent 02a53f7 commit b610465c2f705bebd8b3d2e61f2247222e2af471 @cathywu committed Jan 21, 2012
Showing with 165 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +33 −0 deriv.py
  3. +70 −0 lk.py
  4. +61 −0 main.py
View
1 .gitignore
@@ -0,0 +1 @@
+*.pyc
View
33 deriv.py
@@ -0,0 +1,33 @@
+import numpy as np
+import scipy.signal as si
+from PIL import Image
+
+def gauss_kern():
+ h1 = 15
+ h2 = 15
+ x, y = np.mgrid[0:h2, 0:h1]
+ x = x-h2/2
+ y = y-h1/2
+ sigma = 1.5
+ g = np.exp( -( x**2 + y**2 ) / (2*sigma**2) );
+ return g / g.sum()
+
+def deriv(im1, im2):
+ g = gauss_kern()
+ Img_smooth = si.convolve(im1,g,mode='same')
+ fx,fy=np.gradient(Img_smooth)
+ ft = si.convolve2d(im1, 0.25 * np.ones((2,2))) + \
+ si.convolve2d(im2, -0.25 * np.ones((2,2)))
+
+ fx = fx[0:fx.shape[0]-1, 0:fx.shape[1]-1]
+ fy = fy[0:fy.shape[0]-1, 0:fy.shape[1]-1];
+ ft = ft[0:ft.shape[0]-1, 0:ft.shape[1]-1];
+ return fx, fy, ft
+
+if __name__ == "__main__":
+ im1 = np.asarray(Image.open('flow1-bw-0.png'))
+ im2 = np.asarray(Image.open("flow2-bw-0.png"))
+ fx, fy, ft = deriv(im1, im2)
+
+
+
View
70 lk.py
@@ -0,0 +1,70 @@
+import matplotlib.pyplot as plt
+import numpy as np
+import scipy.signal as si
+from PIL import Image
+import deriv
+import numpy.linalg as lin
+
+def lk(im1, im2, window_size) :
+ u = np.zeros(im1.shape)
+ v = np.zeros(im2.shape)
+
+ fx, fy, ft = deriv.deriv(im1, im2)
+ halfWindow = np.floor(window_size/2)
+ for i in range(int(halfWindow+1),int(fx.shape[0]-halfWindow)):
+ for j in range(int(halfWindow+1),int(fx.shape[1]-halfWindow)):
+ curFx = fx[i-halfWindow-1:i+halfWindow,
+ j-halfWindow-1:j+halfWindow]
+ curFy = fy[i-halfWindow-1:i+halfWindow,
+ j-halfWindow-1:j+halfWindow]
+ curFt = ft[i-halfWindow-1:i+halfWindow,
+ j-halfWindow-1:j+halfWindow]
+
+ curFx = curFx.flatten()
+ curFy = curFy.flatten()
+ curFt = -curFt.flatten()
+
+ A = np.vstack((curFx, curFy)).T
+ U = np.dot(np.dot(lin.pinv(np.dot(A.T,A)),A.T),curFt)
+ u[i,j] = U[0]
+ v[i,j] = U[1]
+ return (u,v)
+
+def lk_point(im1, im2, i, j, window_size) :
+ fx, fy, ft = deriv.deriv(im1, im2)
+ halfWindow = np.floor(window_size/2)
+ curFx = fx[i-halfWindow-1:i+halfWindow,
+ j-halfWindow-1:j+halfWindow]
+ curFy = fy[i-halfWindow-1:i+halfWindow,
+ j-halfWindow-1:j+halfWindow]
+ curFt = ft[i-halfWindow-1:i+halfWindow,
+ j-halfWindow-1:j+halfWindow]
+ #curFx = curFx.T
+ #curFy = curFy.T
+ #curFt = curFt.T
+
+ curFx = curFx.flatten()
+ curFy = curFy.flatten()
+ curFt = -curFt.flatten()
+
+ A = np.vstack((curFx, curFy)).T
+ U = np.dot(np.dot(lin.pinv(np.dot(A.T,A)),A.T),curFt)
+ return U[0], U[1]
+
+if __name__ == "__main__":
+ x=165
+ y=95
+ win=50
+ im1 = np.asarray(Image.open('flow1-bw-0.png'))
+ print im1.shape
+ #im2 = np.asarray(Image.open('flow2-bw-0.png'))
+ #im2 = np.asarray(Image.open('upright.png'))
+ im2 = np.asarray(Image.open('dleft.png'))
+ print im2.shape
+ u, v = lk(im1, im2, x, y, win)
+ print u, v
+ plt.imshow(im1, cmap='gray')
+ plt.hold(True)
+ plt.plot(x,y,'+r');
+ plt.plot(x+u*3,y+v*3,'og')
+ plt.show()
View
61 main.py
@@ -0,0 +1,61 @@
+#!/usr/bin/python
+
+#import utilities as util
+import matplotlib.pyplot as plt
+from SimpleCV import Image
+import SimpleCV as scv
+import lk
+import numpy as np
+
+#DATA_PATH = "/home/cathywu/Dropbox/UROP/wearable/data/exp001/compressed/iphone4s-1920r_30f_all_auto.avi"
+DATA_PATH = "/home/cathywu/Dropbox/UROP/wearable/data/exp001/compressed/firefly_fw_640r_60f_320s.avi"
+
+class Video:
+ def __init__(self,path):
+ self.capture = scv.VirtualCamera(path,"video")
+ self.im = None
+ def step(self,stepsize=1,scale=0.25):
+ for i in range(stepsize-1):
+ self.capture.getImage()
+ self.im = np.asarray(self.capture.getImage().copy().scale(scale).getGrayscaleMatrix())
+ return self.get_image()
+ def show(self):
+ plt.figure()
+ plt.show()
+ plt.imshow(self.im,cmap="gray")
+ def get_image(self):
+ return self.im
+
+vid = Video(DATA_PATH)
+vid.step(stepsize=31)
+
+# lucas kanade
+#[http://ascratchpad.blogspot.com/2011/10/optical-flow-lucas-kanade-in-python.html]
+end = None
+plt.figure()
+plt.show()
+while end != "q":
+ plt.clf()
+ win=10
+ im1 = vid.step(30)
+ im2 = vid.step(4)
+ u,v = lk.lk(im1,im2,win)
+ print u,v
+ print u.max(),v.max()
+ print u.min(),v.min()
+ #plt.imshow((u**2+v**2)**0.5)
+ plt.imshow(im1,cmap='gray')
+ raw_input()
+ plt.imshow(im2,cmap='gray')
+ raw_input()
+ plt.hold(True)
+ plt.plot(np.nonzero(v>0.01)[1],np.nonzero(v>0.01)[0],'+b')
+ plt.plot(np.nonzero(u>0.01)[1],np.nonzero(u>0.01)[0],'+r')
+ plt.show()
+ #plt.imshow(im1np, cmap='gray')
+ #plt.hold(True)
+ #plt.plot(x,y,'+r');
+ #plt.plot(x+u*3,y+v*3,'og')
+ #plt.show()
+ end = raw_input("Continue...")
+

0 comments on commit b610465

Please sign in to comment.
Something went wrong with that request. Please try again.