script_good_features.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. from cv2 import cv as cv
  3. import cv2
  4. from common import anorm, clock
  5. from functools import partial
  6. import numpy as np
  7. help_message = '''Good Features to Track
  8. USAGE: good_features.py [ <image> ]
  9. '''
  10. if __name__ == '__main__':
  11. import sys
  12. try: fn1 = sys.argv[1]
  13. except:
  14. fn1 = "test_images/mona_lisa_face.png"
  15. print help_message
  16. # Good features parameters
  17. gf_params = dict( maxCorners = 200,
  18. qualityLevel = 0.1,
  19. minDistance = 7,
  20. blockSize = 20,
  21. useHarrisDetector = False,
  22. k = 0.04 )
  23. img = cv2.imread(fn1, cv2.CV_LOAD_IMAGE_COLOR)
  24. grey = cv2.cvtColor(img, cv.CV_BGR2GRAY)
  25. start = clock()
  26. keypoints = cv2.goodFeaturesToTrack(grey, mask = None, **gf_params)
  27. if keypoints is not None:
  28. for x, y in np.float32(keypoints).reshape(-1, 2):
  29. cv2.circle(img, (x, y), 3, (0, 255, 0, 0), cv.CV_FILLED, 8, 0)
  30. print "Elapsed time:", 1000 * (clock() - start), "milliseconds"
  31. cv2.imshow("Keypoints", img)
  32. cv2.waitKey()