Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/python/test/test_texture_flow.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
Texture flow direction estimation.
5
6
Sample shows how cv.cornerEigenValsAndVecs function can be used
7
to estimate image texture flow direction.
8
'''
9
10
# Python 2/3 compatibility
11
from __future__ import print_function
12
13
import numpy as np
14
import cv2 as cv
15
import sys
16
17
from tests_common import NewOpenCVTests
18
19
20
class texture_flow_test(NewOpenCVTests):
21
22
def test_texture_flow(self):
23
24
img = self.get_sample('samples/data/chessboard.png')
25
26
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
27
h, w = img.shape[:2]
28
29
eigen = cv.cornerEigenValsAndVecs(gray, 5, 3)
30
eigen = eigen.reshape(h, w, 3, 2) # [[e1, e2], v1, v2]
31
flow = eigen[:,:,2]
32
33
d = 300
34
eps = d / 30
35
36
points = np.dstack( np.mgrid[d/2:w:d, d/2:h:d] ).reshape(-1, 2)
37
38
textureVectors = []
39
for x, y in np.int32(points):
40
textureVectors.append(np.int32(flow[y, x]*d))
41
42
for i in range(len(textureVectors)):
43
self.assertTrue(cv.norm(textureVectors[i], cv.NORM_L2) < eps
44
or abs(cv.norm(textureVectors[i], cv.NORM_L2) - d) < eps)
45
46
if __name__ == '__main__':
47
NewOpenCVTests.bootstrap()
48
49