This example shows how to implement a simple high-resolution image navigation using OpenCV for Python. We simply use the cv2.getRectSubPix() function which contains the following parameters :
Parameters:
#!/usr/bin/env python
# Python 2/3 compatibility
from __future__ import print_function
import sys
PY3 = sys.version_info[0] == 3
if PY3:
xrange = range
import numpy as np
import cv2
# built-in modules
import sys
if __name__ == '__main__':
print('This example shows how to implement a simple hi resolution image navigation.')
print('USAGE: browse.py [image filename]')
print()
fn = 'images/hi-res-nyc.jpg'
print('loading %s ...' % fn)
img = cv2.imread(fn)
if img is None:
print('Failed to load fn:', fn)
sys.exit(1)
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()
The output of this script is as follows:
The program zooms that patch of the hi-res image where the cursor is currently positioned.