def default_camera(img_nr, img, H, scale, oshape, std=1.0, _coords=[]): """The default camera model simply blurs and downscales the image. Parameters ---------- img_nr : int The number of this image in the set. Useful for storing image-specific parameters, such as coordinates. img : ndarray High-resolution image data. H : (3,3) ndarray Transformation matrix to apply to `img`. oshape : tuple of ints Output shape. std : float Standard deviation of the blur mask. _coords : ndarray Coordinates suitable for use in ``ndimage.map_coordinates``. """ try: coords = _coords[img_nr] except IndexError: H = H.copy() H[:2, :] *= float(scale) H = np.linalg.inv(H) coords = _homography_coords(img, H, oshape) _coords.append(coords) out = homography(img, H, _coords=coords) out = out[:oshape[0], :oshape[1]] return out
def cost(p, A, B): M = _build_tf(p, translation_only=translation_only, fixed_scale=fixed_scale) try: T = transform.homography(B, M, order=2) except np.linalg.LinAlgError: raise RuntimeError('Could not invert transformation matrix. ' 'This may be because too many levels of ' 'downscaling was requested (%d).' % levels) H = joint_hist(A, T, win_size=win_size, std=std, fast=fast) S = mutual_info(H) return -S
import numpy as np from demo_data import chelsea from supreme.register.parzen import joint_hist, mutual_info from supreme.transform import homography import matplotlib.pyplot as plt h1 = chelsea(grey=True).astype(np.uint8) plt.suptitle('Parzen-Window Joint PDF Estimator') for n, t in enumerate([0, 0.01, 0.1]): plt.subplot(1, 3, n + 1) h2 = homography(h1, [[np.cos(t), -np.sin(t), 0], [np.sin(t), np.cos(t), 0], [0, 0, 1]]) if n == 0: plt.ylabel('Grey levels in A') plt.xlabel('Grey levels in B') H = joint_hist(h1, h2, win_size=5, std=1) S = mutual_info(H) plt.title('Rotation $%.2f^\\circ$\nS=%.5f' % ((t / np.pi * 180), S)) plt.imshow(np.log(H + 10), interpolation='nearest') plt.show()
import sys if len(sys.argv) == 3: from supreme.io import imread A = imread(sys.argv[1], flatten=True).astype(np.uint8) Ac = imread(sys.argv[1], flatten=False).astype(np.uint8) B = imread(sys.argv[2], flatten=True).astype(np.uint8) Bc = imread(sys.argv[2], flatten=False).astype(np.uint8) else: A = chelsea(grey=True).astype(np.uint8) Ac = A t = 20/180. * np.pi x = 3 y = 6 B = homography(A, [[np.cos(t), -np.sin(t), x], [np.sin(t), np.cos(t), y], [0, 0, 1]]) Bc = B M, S = register.dense_MI(A, B, levels=3, std=5, win_size=9) print "Mutual information: ", S if S < 1.5: print "Warning: registration probably failed." print "Transformation matrix:" print np.array2string(M, separator=', ') X = register.stack.with_transform([Ac, Bc], [np.eye(3), M], save_tiff=True) if len(sys.argv) != 3: import matplotlib.pyplot as plt
else: parser.print_help() sys.exit(0) ic = load_vgg(vgg_dir) # Perform crude photometric registration ref = ic[0].copy() images = [] scale_offset = [] for i in range(len(ic)): scale = 1 offset = 0 if options.photo_adjust: img_warp = homography(ic[i], ic[i].info['H']) scale, offset = photometric_adjust(img_warp, ref) scale_offset.append((scale, offset)) images.append(ic[i] * scale + offset) print "Images adjusted by: %s" % str(['%.2f, %.2f' % (a,b) for (a,b) in scale_offset]) images = [img for i,img in enumerate(images) if not i in options.ignore] HH = [i.info['H'] for i in images] oshape = np.floor(np.array(images[0].shape) * options.scale) avg = initial_guess_avg(images, HH, options.scale, oshape) #
import supreme.register as register import sys if len(sys.argv) == 3: from supreme.io import imread A = imread(sys.argv[1], flatten=True).astype(np.uint8) Ac = imread(sys.argv[1], flatten=False).astype(np.uint8) B = imread(sys.argv[2], flatten=True).astype(np.uint8) Bc = imread(sys.argv[2], flatten=False).astype(np.uint8) else: A = chelsea(grey=True).astype(np.uint8) Ac = A t = 20 / 180. * np.pi x = 3 y = 6 B = homography( A, [[np.cos(t), -np.sin(t), x], [np.sin(t), np.cos(t), y], [0, 0, 1]]) Bc = B M, S = register.dense_MI(A, B, levels=3, std=5, win_size=9) print "Mutual information: ", S if S < 1.5: print "Warning: registration probably failed." print "Transformation matrix:" print np.array2string(M, separator=', ') X = register.stack.with_transform([Ac, Bc], [np.eye(3), M], save_tiff=True) if len(sys.argv) != 3: import matplotlib.pyplot as plt plt.imshow(X)
import numpy as np from demo_data import chelsea from supreme.register.parzen import joint_hist, mutual_info from supreme.transform import homography import matplotlib.pyplot as plt h1 = chelsea(grey=True).astype(np.uint8) plt.suptitle('Parzen-Window Joint PDF Estimator') for n, t in enumerate([0, 0.01, 0.1]): plt.subplot(1, 3, n + 1) h2 = homography( h1, [[np.cos(t), -np.sin(t), 0], [np.sin(t), np.cos(t), 0], [0, 0, 1]]) if n == 0: plt.ylabel('Grey levels in A') plt.xlabel('Grey levels in B') H = joint_hist(h1, h2, win_size=5, std=1) S = mutual_info(H) plt.title('Rotation $%.2f^\\circ$\nS=%.5f' % ((t / np.pi * 180), S)) plt.imshow(np.log(H + 10), interpolation='nearest') plt.show()
else: parser.print_help() sys.exit(0) ic = load_vgg(vgg_dir) # Perform crude photometric registration ref = ic[0].copy() images = [] scale_offset = [] for i in range(len(ic)): scale = 1 offset = 0 if options.photo_adjust: img_warp = homography(ic[i], ic[i].info['H']) scale, offset = photometric_adjust(img_warp, ref) scale_offset.append((scale, offset)) images.append(ic[i] * scale + offset) print "Images adjusted by: %s" % str( ['%.2f, %.2f' % (a, b) for (a, b) in scale_offset]) images = [img for i, img in enumerate(images) if not i in options.ignore] HH = [i.info['H'] for i in images] oshape = np.floor(np.array(images[0].shape) * options.scale) avg = initial_guess_avg(images, HH, options.scale, oshape) #