Home | History | Annotate | Download | only in python2
      1 #!/usr/bin/env python
      2 
      3 '''
      4 Texture flow direction estimation.
      5 
      6 Sample shows how cv2.cornerEigenValsAndVecs function can be used
      7 to estimate image texture flow direction.
      8 
      9 Usage:
     10     texture_flow.py [<image>]
     11 '''
     12 
     13 import numpy as np
     14 import cv2
     15 
     16 if __name__ == '__main__':
     17     import sys
     18     try:
     19         fn = sys.argv[1]
     20     except:
     21         fn = '../data/starry_night.jpg'
     22 
     23     img = cv2.imread(fn)
     24     if img is None:
     25         print 'Failed to load image file:', fn
     26         sys.exit(1)
     27 
     28     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
     29     h, w = img.shape[:2]
     30 
     31     eigen = cv2.cornerEigenValsAndVecs(gray, 15, 3)
     32     eigen = eigen.reshape(h, w, 3, 2)  # [[e1, e2], v1, v2]
     33     flow = eigen[:,:,2]
     34 
     35     vis = img.copy()
     36     vis[:] = (192 + np.uint32(vis)) / 2
     37     d = 12
     38     points =  np.dstack( np.mgrid[d/2:w:d, d/2:h:d] ).reshape(-1, 2)
     39     for x, y in points:
     40        vx, vy = np.int32(flow[y, x]*d)
     41        cv2.line(vis, (x-vx, y-vy), (x+vx, y+vy), (0, 0, 0), 1, cv2.LINE_AA)
     42     cv2.imshow('input', img)
     43     cv2.imshow('flow', vis)
     44     cv2.waitKey()
     45