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'): 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)) assert d < 0.2
def stitchRL(image2, image3, points23): """ Stitch image1 to image2, with points points """ s = image2.shape[1] image2b = np.zeros((image2.shape[0], image2.shape[1] + 500)) image2b[:, :image2.shape[1]] = image2 image2 = image2b image3b = np.zeros((image3.shape[0], image3.shape[1] + 500)) image3b[:, :image3.shape[1]] = image3 image3 = image3b #points23[:, 0] += 500 #points23[:, 2] += 500 H2 = calculate_homography(points23) Hr = H2.copy() # Hr[0, 2] = 0 # Hr[1, 2] = 0 image3H = transform.homography(image3, H2) # image2Hr = np.zeros((image2.shape[0], 1500)) # image2Hr[:image2.shape[0], :image2.shape[1]] = image2 # image2Hr = transform.homography(image2Hr, Ht) # em = image2Hr.copy() # em[:image1.shape[0],:image1.shape[1]] = image1H em = image2 + image3H em[:,:s] = image2[:, :s] return em
def test_homography(): x = np.arange(9, dtype=np.uint8).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))
def stitchLR(image1, image2, points): """ Stitch image1 to image2, with points points """ # image 2 is base image. ie, we need to translate image1 image1b = np.zeros((image1.shape[0], image1.shape[1] + 500)) image1b[:, 500:] = image1 image1 = image1b image2b = np.zeros((image2.shape[0], image2.shape[1] + 500)) image2b[:, 500:] = image2 image2 = image2b points[:, 1] += 500 points[:, 3] += 500 H2 = calculate_homography(points) Hr = H2.copy() # Hr[0, 2] = 0 # Hr[1, 2] = 0 image1H = transform.homography(image1, H2) # image2Hr = np.zeros((image2.shape[0], 1500)) # image2Hr[:image2.shape[0], :image2.shape[1]] = image2 # image2Hr = transform.homography(image2Hr, Ht) # em = image2Hr.copy() # em[:image1.shape[0],:image1.shape[1]] = image1H em = image1H + image2 em[:, 500:] = image2[:, 500:] return em
def test_homography(): x = np.arange(9, dtype=np.uint8).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))
def test_homography(): x = np.zeros((5, 5), dtype=np.uint8) x[1, 1] = 255 x = img_as_float(x) theta = -np.pi/2 M = np.array([[np.cos(theta),-np.sin(theta),0], [np.sin(theta), np.cos(theta),4], [0, 0, 1]]) x90 = homography(x, M, order=1) assert_array_almost_equal(x90, np.rot90(x))
def random_rotate(image3D): theta = np.deg2rad(10) tx = 0 ty = 0 S, C = np.sin(theta), np.cos(theta) # Rotation matrix, angle theta, translation tx, ty H = np.array([[C, -S, tx], [S, C, ty], [0, 0, 1]]) # Translation matrix to shift the image center to the origin r, c = img.shape T = np.array([[1, 0, -c / 2.], [0, 1, -r / 2.], [0, 0, 1]]) # Skew, for perspective S = np.array([[1, 0, 0], [0, 1.3, 0], [0, 1e-3, 1]]) img_rot = transform.homography(img, H) img_rot_center_skew = transform.homography( img, S.dot(np.linalg.inv(T).dot(H).dot(T))) return image3D
from skimage 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)
def test_homography_basic(): homography(np.random.random((25, 25)), np.eye(3))
import skimage from skimage import io as sio from skimage import transform as tf from skimage import data, color img = data.camera() theta = np.deg2rad(30) c = np.cos(theta) s = np.sin(theta) a = 0.4 b = 0.4 H = np.array([[a * c, -b * s, 130], [a * s, b * c, 20], [1e-4, -5e-4, 1]]) img_tf = tf.homography(img, H) plt.subplot(121) plt.grid() plt.axis('image') plt.xlim(0, X_OUT) plt.ylim(0, Y_OUT) ax = plt.gca() ax.invert_yaxis() plt.subplot(122) plt.imshow(img_tf, cmap=plt.cm.gray, interpolation='nearest') plt.suptitle( 'Dewarping\nSelect 4 points on the left grid, then 4 in the right ' 'image.')
from skimage 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)
from skimage import data, color img = data.camera() theta = np.deg2rad(30) c = np.cos(theta) s = np.sin(theta) a = 0.4 b = 0.4 H = np.array([[a*c, -b*s, 130], [a*s, b*c, 20], [1e-4, -5e-4, 1]]) img_tf = tf.homography(img, H) plt.subplot(121) plt.grid() plt.axis('image') plt.xlim(0, X_OUT) plt.ylim(0, Y_OUT) ax = plt.gca() ax.invert_yaxis() plt.subplot(122) plt.imshow(img_tf, cmap=plt.cm.gray, interpolation='nearest') plt.suptitle('Dewarping\nSelect 4 points on the left grid, then 4 in the right ' 'image.')
theta = np.deg2rad(10) tx = 0 ty = 0 S, C = np.sin(theta), np.cos(theta) # Rotation matrix, angle theta, translation tx, ty H = np.array([[C, -S, tx], [S, C, ty], [0, 0, 1]]) # Translation matrix to shift the image center to the origin r, c = img.shape T = np.array([[1, 0, -c / 2.], [0, 1, -r / 2.], [0, 0, 1]]) # Skew, for perspective S = np.array([[1, 0, 0], [0, 1.3, 0], [0, 1e-3, 1]]) img_rot = transform.homography(img, H) img_rot_center_skew = transform.homography(img, S.dot(np.linalg.inv(T).dot(H).dot(T))) f, (ax0, ax1, ax2) = plt.subplots(1, 3) ax0.imshow(img, cmap=plt.cm.gray, interpolation='nearest') ax1.imshow(img_rot, cmap=plt.cm.gray, interpolation='nearest') ax2.imshow(img_rot_center_skew, cmap=plt.cm.gray, interpolation='nearest') plt.show()