This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

Attachment 'lic.py'

Download

   1 import numpy as np
   2 
   3 
   4 def lic_flow(vectors,len_pix=10):
   5     vectors = np.asarray(vectors)
   6     m,n,two = vectors.shape
   7     if two!=2:
   8         raise ValueError
   9 
  10     result = np.zeros((2*len_pix+1,m,n,2),dtype=np.int32) # FIXME: int16?
  11     center = len_pix
  12     result[center,:,:,0] = np.arange(m)[:,np.newaxis]
  13     result[center,:,:,1] = np.arange(n)[np.newaxis,:]
  14 
  15     for i in range(m):
  16         for j in range(n):
  17             y = i
  18             x = j
  19             fx = 0.5
  20             fy = 0.5
  21             for k in range(len_pix):
  22                 vx, vy = vectors[y,x]
  23                 print x, y, vx, vy
  24                 if vx>=0:
  25                     tx = (1-fx)/vx
  26                 else:
  27                     tx = -fx/vx
  28                 if vy>=0:
  29                     ty = (1-fy)/vy
  30                 else:
  31                     ty = -fy/vy
  32                 if tx<ty:
  33                     print "x step"
  34                     if vx>0:
  35                         x+=1
  36                         fy+=vy*tx
  37                         fx=0.
  38                     else:
  39                         x-=1
  40                         fy+=vy*tx
  41                         fx=1.
  42                 else:
  43                     print "y step"
  44                     if vy>0:
  45                         y+=1
  46                         fx+=vx*ty
  47                         fy=0.
  48                     else:
  49                         y-=1
  50                         fx+=vx*ty
  51                         fy=1.
  52                 if x<0: x=0
  53                 if y<0: y=0
  54                 if x>=n: x=n-1
  55                 if y>=m: y=m-1
  56                 result[center+k+1,i,j,:] = y, x
  57 
  58 
  59 
  60     return result

New Attachment

File to upload
Rename to
Overwrite existing attachment of same name

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.