def multiscale(): d_tot = np.zeros((J.shape[0], J.shape[1], 2, 1)) Jn = J n_scales = 7 for n in range(n_scales, 2, -1): sc = 2**(n - 1) dn, Cn = LK_equation(I, Jn, sc * 2.0, sc * 0.1, [40, 40], remove_outliers=True) d_tot += dn Jn = Cn lab2.gopimage(d_tot[:, :, :, 0]) plt.show() (err1, err2) = LK_errors(I, J, Jn) return d_tot[:, :, :, 0]
from matplotlib import pyplot as plt import PIL.Image from single_scale_error import LK_errors from LK_equation import LK_equation import lab2 def load_image_grayscale(path): "Load a grayscale image by path" return np.asarray(PIL.Image.open(path).convert('L')) I = load_image_grayscale('SCcar4/SCcar4_00070.bmp') J = load_image_grayscale('SCcar4/SCcar4_00071.bmp') d_tot = np.zeros((J.shape[0], J.shape[1], 2, 1)) Jn = J n_scales = 7 for n in range(n_scales, 2, -1): sc = 2 ** (n - 1) dn, Cn = LK_equation(I, Jn, sc * 2.0, sc * 0.1, [40, 40], remove_outliers=True) d_tot += dn Jn = Cn lab2.gopimage(d_tot[:, :, :, 0]) plt.show() (err1, err2) = LK_errors(I, J, Jn)
return I, J (I, J) = get_cameraman() plt.figure(1) plt.imshow(I) (ds, interpolated_J) = LK_equation(I, J, 15, 1.6, [10, 10]) #plt.imshow(interpolated_J, cmap="gray") #plt.show() plt.figure(2) plt.imshow(ds[:, :, 0, 0]) (err1, err2) = LK_errors(I, J, interpolated_J) plt.figure(3) lab2.gopimage(ds[:, :, :, 0]) plt.show() ''' plt.figure(1) plt.imshow(ds[:, :, 0, 0], cmap="gray") plt.figure(2) plt.imshow(I, cmap="gray") plt.figure(3) plt.imshow(J, cmap="gray") plt.show() '''
height = np.shape(J)[0] xcoords = np.arange(0, width) ycoords = np.arange(0, height) Iinterpol = scipy.interpolate.RectBivariateSpline(ycoords, xcoords, I) Jinterpol = scipy.interpolate.RectBivariateSpline(ycoords, xcoords, J) mg = np.array(np.meshgrid(xcoords, ycoords)).reshape(2, -1) Icoords = np.array( [mg[0] - d[..., 0].flatten() / 2, mg[1] - d[..., 1].flatten() / 2]) Jcoords = np.array( [mg[0] + d[..., 0].flatten() / 2, mg[1] + d[..., 1].flatten() / 2]) Id = Jinterpol(Icoords[1], Icoords[0], grid=False).reshape(np.shape(I)) Jd = Iinterpol(Jcoords[1], Jcoords[0], grid=False).reshape(np.shape(J)) lab2.image_grid({"I": I, "J": J}, share_all=True, imshow_opts={'cmap': 'gray'}) #plt.figure("I"), plt.imshow(I, cmap='gray') #plt.figure("J"), plt.imshow(J, cmap='gray') #plt.figure("Id"), plt.imshow(Id, cmap='gray') #plt.figure("Jd"), plt.imshow(Jd, cmap='gray') plt.figure("Jd"), plt.imshow(np.abs(Jd - Id), cmap='gray') print("|J-I| = ", np.linalg.norm(J[30:-30, 30:-30] - I[30:-30, 30:-30])) print("|Jd-Id| = ", np.linalg.norm(Jd[30:-30, 30:-30] - Id[30:-30, 30:-30])) lab2.gopimage(d) plt.show()