browse.py 1.03 KB
Newer Older
1 2 3 4 5
'''
browse.py shows how to implement a simple hi resolution image navigation
'''

import numpy as np
Alexander Mordvintsev's avatar
Alexander Mordvintsev committed
6
import cv2
7 8 9 10 11 12 13 14 15 16 17 18
import sys

print 'This sample shows how to implement a simple hi resolution image navigation.'
print 'USAGE: browse.py [image filename]'
print 

if len(sys.argv) > 1:
    fn = sys.argv[1]
    print 'loading %s ...' % fn
    img = cv2.imread(fn)
else:
    sz = 4096
19
    print 'generating %dx%d procedural image ...' % (sz, sz)
20
    img = np.zeros((sz, sz), np.uint8)
21 22
    track = np.cumsum(np.random.rand(500000, 2)-0.5, axis=0)
    track = np.int32(track*10 + (sz/2, sz/2))
Alexander Mordvintsev's avatar
Alexander Mordvintsev committed
23
    cv2.polylines(img, [track], 0, 255, 1, cv2.CV_AA)
24 25

small = img
26
for i in xrange(3):
27 28 29 30 31 32 33 34 35
    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)

36
cv2.imshow('preview', small)
Alexander Mordvintsev's avatar
Alexander Mordvintsev committed
37
cv2.setMouseCallback('preview', onmouse, None)
38
cv2.waitKey()