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