texture_flow.py 1.1 KB
Newer Older
1
#!/usr/bin/env python
2

3 4 5
'''
Texture flow direction estimation.

6
Sample shows how cv.cornerEigenValsAndVecs function can be used
7 8 9 10 11 12
to estimate image texture flow direction.

Usage:
    texture_flow.py [<image>]
'''

13 14 15
# Python 2/3 compatibility
from __future__ import print_function

16
import numpy as np
17
import cv2 as cv
18 19 20

if __name__ == '__main__':
    import sys
21 22 23
    try:
        fn = sys.argv[1]
    except:
Dmitriy Anisimov's avatar
Dmitriy Anisimov committed
24
        fn = '../data/starry_night.jpg'
25

26
    img = cv.imread(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 33
    h, w = img.shape[:2]

34
    eigen = cv.cornerEigenValsAndVecs(gray, 15, 3)
35 36 37 38 39 40 41
    eigen = eigen.reshape(h, w, 3, 2)  # [[e1, e2], v1, v2]
    flow = eigen[:,:,2]

    vis = img.copy()
    vis[:] = (192 + np.uint32(vis)) / 2
    d = 12
    points =  np.dstack( np.mgrid[d/2:w:d, d/2:h:d] ).reshape(-1, 2)
42
    for x, y in np.int32(points):
tribta's avatar
tribta committed
43
        vx, vy = np.int32(flow[y, x]*d)
44 45 46 47
        cv.line(vis, (x-vx, y-vy), (x+vx, y+vy), (0, 0, 0), 1, cv.LINE_AA)
    cv.imshow('input', img)
    cv.imshow('flow', vis)
    cv.waitKey()