test_texture_flow.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python
  2. '''
  3. Texture flow direction estimation.
  4. Sample shows how cv.cornerEigenValsAndVecs function can be used
  5. to estimate image texture flow direction.
  6. '''
  7. # Python 2/3 compatibility
  8. from __future__ import print_function
  9. import numpy as np
  10. import cv2 as cv
  11. import sys
  12. from tests_common import NewOpenCVTests
  13. class texture_flow_test(NewOpenCVTests):
  14. def test_texture_flow(self):
  15. img = self.get_sample('samples/data/chessboard.png')
  16. gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  17. h, w = img.shape[:2]
  18. eigen = cv.cornerEigenValsAndVecs(gray, 5, 3)
  19. eigen = eigen.reshape(h, w, 3, 2) # [[e1, e2], v1, v2]
  20. flow = eigen[:,:,2]
  21. d = 300
  22. eps = d / 30
  23. points = np.dstack( np.mgrid[d/2:w:d, d/2:h:d] ).reshape(-1, 2)
  24. textureVectors = []
  25. for x, y in np.int32(points):
  26. textureVectors.append(np.int32(flow[y, x]*d))
  27. for i in range(len(textureVectors)):
  28. self.assertTrue(cv.norm(textureVectors[i], cv.NORM_L2) < eps
  29. or abs(cv.norm(textureVectors[i], cv.NORM_L2) - d) < eps)
  30. if __name__ == '__main__':
  31. NewOpenCVTests.bootstrap()