browse.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. '''
  3. browse.py
  4. =========
  5. Sample shows how to implement a simple hi resolution image navigation
  6. Usage
  7. -----
  8. browse.py [image filename]
  9. '''
  10. # Python 2/3 compatibility
  11. from __future__ import print_function
  12. import sys
  13. PY3 = sys.version_info[0] == 3
  14. if PY3:
  15. xrange = range
  16. import numpy as np
  17. import cv2
  18. # built-in modules
  19. import sys
  20. if __name__ == '__main__':
  21. print('This sample shows how to implement a simple hi resolution image navigation.')
  22. print('USAGE: browse.py [image filename]')
  23. print()
  24. if len(sys.argv) > 1:
  25. fn = sys.argv[1]
  26. print('loading %s ...' % fn)
  27. img = cv2.imread(fn)
  28. if img is None:
  29. print('Failed to load fn:', fn)
  30. sys.exit(1)
  31. else:
  32. sz = 4096
  33. print('generating %dx%d procedural image ...' % (sz, sz))
  34. img = np.zeros((sz, sz), np.uint8)
  35. track = np.cumsum(np.random.rand(500000, 2)-0.5, axis=0)
  36. track = np.int32(track*10 + (sz/2, sz/2))
  37. cv2.polylines(img, [track], 0, 255, 1, cv2.LINE_AA)
  38. small = img
  39. for i in xrange(3):
  40. small = cv2.pyrDown(small)
  41. def onmouse(event, x, y, flags, param):
  42. h, w = img.shape[:2]
  43. h1, w1 = small.shape[:2]
  44. x, y = 1.0*x*h/h1, 1.0*y*h/h1
  45. zoom = cv2.getRectSubPix(img, (800, 600), (x+0.5, y+0.5))
  46. cv2.imshow('zoom', zoom)
  47. cv2.imshow('preview', small)
  48. cv2.setMouseCallback('preview', onmouse)
  49. cv2.waitKey()
  50. cv2.destroyAllWindows()