def test_fast_homography(): img = rgb2gray(data.lena()).astype(np.uint8) img = img[:, :100] theta = np.deg2rad(30) scale = 0.5 tx, ty = 50, 50 H = np.eye(3) S = scale * np.sin(theta) C = scale * np.cos(theta) H[:2, :2] = [[C, -S], [S, C]] H[:2, 2] = [tx, ty] for mode in ('constant', 'mirror', 'wrap'): print 'Transform mode:', mode p0 = homography(img, H, mode=mode, order=1) p1 = fast_homography(img, H, mode=mode) p1 = np.round(p1) ## import matplotlib.pyplot as plt ## f, (ax0, ax1, ax2, ax3) = plt.subplots(1, 4) ## ax0.imshow(img) ## ax1.imshow(p0, cmap=plt.cm.gray) ## ax2.imshow(p1, cmap=plt.cm.gray) ## ax3.imshow(np.abs(p0 - p1), cmap=plt.cm.gray) ## plt.show() d = np.mean(np.abs(p0 - p1)) print "delta=", d assert d < 0.2
def test_homography(): x = np.arange(9).reshape((3, 3)) + 1 theta = -np.pi/2 M = np.array([[np.cos(theta),-np.sin(theta),0], [np.sin(theta), np.cos(theta),2], [0, 0, 1]]) x90 = homography(x, M, order=1) assert_array_almost_equal(x90, np.rot90(x))
# Source coordinates sc = np.array(plt.ginput(n=4)) screen_coords = [(0, 0), (1023, 0), (1023, 767), (0, 767)] # Homography: source to camera H_SC = estimate_homography(screen_coords, sc) tc = np.array(plt.ginput(n=4)) print tc tc_in_screen = \ np.dot(np.linalg.inv(H_SC), np.hstack((tc, np.ones((4,1)))).T).T tc_in_screen /= tc_in_screen[:, 2, np.newaxis] # Screen to screen homography H_SS = estimate_homography(screen_coords, tc_in_screen) grid = sio.imread('fatgrid.jpg') grid_warp = tf.homography(grid, H_SS, output_shape=grid.shape, order=2) np.save('/tmp/H_SS.npy', H_SS) sio.imsave('/tmp/grid_warp.png', grid_warp)
from scikits.image import io, transform s = 0.7 img = io.imread('scikits_image_logo.png') h, w, c = img.shape print "\nScaling down logo by %.1fx..." % s img = transform.homography(img, [[s, 0, 0], [0, s, 0], [0, 0, 1]], output_shape=(int(h*s), int(w*s), 4), order=3) io.imsave('scikits_image_logo_small.png', img)