opencv/samples/python/browse.py

66 lines
1.5 KiB
Python
Raw Normal View History

2013-03-06 14:41:02 +08:00
#!/usr/bin/env python
2012-10-17 07:18:30 +08:00
'''
browse.py
=========
Sample shows how to implement a simple hi resolution image navigation
Usage
-----
browse.py [image filename]
'''
# Python 2/3 compatibility
from __future__ import print_function
import sys
PY3 = sys.version_info[0] == 3
if PY3:
xrange = range
2012-10-17 07:18:30 +08:00
import numpy as np
import cv2
2013-03-06 14:41:02 +08:00
# built-in modules
2012-10-17 07:18:30 +08:00
import sys
if __name__ == '__main__':
print('This sample shows how to implement a simple hi resolution image navigation.')
print('USAGE: browse.py [image filename]')
print()
2012-10-17 07:18:30 +08:00
if len(sys.argv) > 1:
fn = sys.argv[1]
print('loading %s ...' % fn)
2012-10-17 07:18:30 +08:00
img = cv2.imread(fn)
2013-03-06 14:41:02 +08:00
if img is None:
print('Failed to load fn:', fn)
2013-03-06 14:41:02 +08:00
sys.exit(1)
2012-10-17 07:18:30 +08:00
else:
sz = 4096
print('generating %dx%d procedural image ...' % (sz, sz))
2012-10-17 07:18:30 +08:00
img = np.zeros((sz, sz), np.uint8)
track = np.cumsum(np.random.rand(500000, 2)-0.5, axis=0)
track = np.int32(track*10 + (sz/2, sz/2))
2013-04-12 21:39:16 +08:00
cv2.polylines(img, [track], 0, 255, 1, cv2.LINE_AA)
2012-10-17 07:18:30 +08:00
2013-03-06 14:41:02 +08:00
2012-10-17 07:18:30 +08:00
small = img
for i in xrange(3):
small = cv2.pyrDown(small)
def onmouse(event, x, y, flags, param):
h, w = img.shape[:2]
h1, w1 = small.shape[:2]
x, y = 1.0*x*h/h1, 1.0*y*h/h1
zoom = cv2.getRectSubPix(img, (800, 600), (x+0.5, y+0.5))
cv2.imshow('zoom', zoom)
cv2.imshow('preview', small)
cv2.setMouseCallback('preview', onmouse)
cv2.waitKey()
cv2.destroyAllWindows()