samples/python/snippets/texture_flow.py#

An example using cornerEigenValsAndVecs in python

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