Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def ImageRegistration(scan1,scan2,cell=None,printdetails=False):
- positions = load_h5(scan1, "positions")
- y_axis_h5 = positions["/data/encoder_fast/data"]
- z_axis_h5 = positions["/data/encoder_slow/data"]
- y = np.array(y_axis_h5)
- z = np.array(z_axis_h5)
- current1 = counts2amps(scan1)
- current2 = counts2amps(scan2)
- y_grid1, z_grid1, current1_array = supergrid(y,z,current1,1000)
- y_grid2, z_grid2, current2_array = supergrid(y,z,current2,1000)
- # convert current1_array to a grayscale image format (values between 0 and 255)
- current1_array /= current1_array.max()
- current1_array *= (1/1-current1_array.min())
- current1_array += 1 - current1_array.max()
- current1_array[current1_array < 0] = 0
- current1_array = np.array(current1_array*255, dtype = np.uint8)
- current2_array /= current2_array.max()
- current2_array *= (1/1-current2_array.min())
- current2_array += 1 - current2_array.max()
- current2_array[current2_array < 0] = 0
- current2_array = np.array(current2_array*255, dtype = np.uint8)
- shape = np.shape(current1_array)
- warp_mode = cv2.MOTION_TRANSLATION # we only need translation in direction of y and z (no rotation etc.)
- warp_matrix = np.eye(2, 3, dtype=np.float32) # this is the matrix that the results of the ECC algorithm are stored in
- number_of_iterations = 5000 # Specify the number of iterations
- termination_eps = 1e-10 # Specify the threshold of the increment in the correlation coefficient between two iterations
- # Specify the overall criteria
- criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations, termination_eps)
- (cc, warp_matrix) = cv2.findTransformECC(current1_array, current2_array, warp_matrix, warp_mode, criteria, None, 1)
- if printdetails == True:
- scan2_aligned = cv2.warpAffine(current2_array, warp_matrix, (shape[1], shape[0]), flags = cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
- scan2_aligned = scan2_aligned.astype(np.float32)
- scan2_aligned[scan2_aligned == 0.0] = float("NaN")
- scan2_aligned_ravel = scan2_aligned.ravel()
- x = np.arange(np.shape(scan2_aligned_ravel)[0])
- plt.figure(figsize=(50,5))
- plt.plot(x, scan2_aligned_ravel)
- plt.show()
- fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize = (15, 15), constrained_layout = True)
- im1 = ax1.imshow(current1_array, origin = "lower", cmap = cm.afmhot)
- ax1.set_title("Scan #" + str(scan1))
- ax1_divider = make_axes_locatable(ax1) # Introduce a divider next to the axis already in place
- cax1 = ax1_divider.append_axes("right", size = "15%", pad = "5%") # Append a new axis object with name "cax" to the divider. Width ("size") and distance to the 2D-Plot ("pad") can be given in % of the width of the x axis.
- legend = colorbar(im1, cax = cax1) # Introduce a legend (colorbar) where the new axis has been placed
- cax1.yaxis.set_major_locator(AutoLocator())
- cax1.yaxis.set_minor_locator(AutoMinorLocator())
- im2 = ax2.imshow(current2_array, origin = "lower", cmap = cm.afmhot)
- ax2.set_title("Scan #" + str(scan2))
- ax2_divider = make_axes_locatable(ax2) # Introduce a divider next to the axis already in place
- cax2 = ax2_divider.append_axes("right", size = "15%", pad = "5%") # Append a new axis object with name "cax" to the divider. Width ("size") and distance to the 2D-Plot ("pad") can be given in % of the width of the x axis.
- legend = colorbar(im2, cax = cax2) # Introduce a legend (colorbar) where the new axis has been placed
- cax2.yaxis.set_major_locator(AutoLocator())
- cax2.yaxis.set_minor_locator(AutoMinorLocator())
- im3 = ax3.imshow(scan2_aligned, origin = "lower", cmap = "inferno")
- ax3.set_title("Scan #" + str(scan2) + " aligned")
- ax3.set_ylim(bottom = 0)
- ax3_divider = make_axes_locatable(ax3) # Introduce a divider next to the axis already in place
- cax3 = ax3_divider.append_axes("right", size = "15%", pad = "5%") # Append a new axis object with name "cax" to the divider. Width ("size") and distance to the 2D-Plot ("pad") can be given in % of the width of the x axis.
- legend = colorbar(im3, cax = cax3) # Introduce a legend (colorbar) where the new axis has been placed
- cax3.yaxis.set_major_locator(AutoLocator())
- cax3.yaxis.set_minor_locator(AutoMinorLocator())
- plt.tight_layout(w_pad=6)
- return warp_matrix
- ImageRegistration(181,182,printdetails=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement