browse.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 as cv
  18. # built-in modules
  19. import sys
  20. def main():
  21. if len(sys.argv) > 1:
  22. fn = cv.samples.findFile(sys.argv[1])
  23. print('loading %s ...' % fn)
  24. img = cv.imread(fn)
  25. if img is None:
  26. print('Failed to load fn:', fn)
  27. sys.exit(1)
  28. else:
  29. sz = 4096
  30. print('generating %dx%d procedural image ...' % (sz, sz))
  31. img = np.zeros((sz, sz), np.uint8)
  32. track = np.cumsum(np.random.rand(500000, 2)-0.5, axis=0)
  33. track = np.int32(track*10 + (sz/2, sz/2))
  34. cv.polylines(img, [track], 0, 255, 1, cv.LINE_AA)
  35. small = img
  36. for _i in xrange(3):
  37. small = cv.pyrDown(small)
  38. def onmouse(event, x, y, flags, param):
  39. h, _w = img.shape[:2]
  40. h1, _w1 = small.shape[:2]
  41. x, y = 1.0*x*h/h1, 1.0*y*h/h1
  42. zoom = cv.getRectSubPix(img, (800, 600), (x+0.5, y+0.5))
  43. cv.imshow('zoom', zoom)
  44. cv.imshow('preview', small)
  45. cv.setMouseCallback('preview', onmouse)
  46. cv.waitKey()
  47. print('Done')
  48. if __name__ == '__main__':
  49. print(__doc__)
  50. main()
  51. cv.destroyAllWindows()