Advertisement
OreganoHauch

alignXBICs

May 24th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. def ImageRegistration(scan1,scan2,cell=None,printdetails=False):
  2.  
  3. positions = load_h5(scan1, "positions")
  4. y_axis_h5 = positions["/data/encoder_fast/data"]
  5. z_axis_h5 = positions["/data/encoder_slow/data"]
  6. y = np.array(y_axis_h5)
  7. z = np.array(z_axis_h5)
  8. current1 = counts2amps(scan1)
  9. current2 = counts2amps(scan2)
  10.  
  11.  
  12. y_grid1, z_grid1, current1_array = supergrid(y,z,current1,1000)
  13. y_grid2, z_grid2, current2_array = supergrid(y,z,current2,1000)
  14.  
  15. # convert current1_array to a grayscale image format (values between 0 and 255)
  16. current1_array /= current1_array.max()
  17. current1_array *= (1/1-current1_array.min())
  18. current1_array += 1 - current1_array.max()
  19. current1_array[current1_array < 0] = 0
  20. current1_array = np.array(current1_array*255, dtype = np.uint8)
  21.  
  22. current2_array /= current2_array.max()
  23. current2_array *= (1/1-current2_array.min())
  24. current2_array += 1 - current2_array.max()
  25. current2_array[current2_array < 0] = 0
  26. current2_array = np.array(current2_array*255, dtype = np.uint8)
  27.  
  28. shape = np.shape(current1_array)
  29.  
  30. warp_mode = cv2.MOTION_TRANSLATION # we only need translation in direction of y and z (no rotation etc.)
  31.  
  32. warp_matrix = np.eye(2, 3, dtype=np.float32) # this is the matrix that the results of the ECC algorithm are stored in
  33.  
  34. number_of_iterations = 5000 # Specify the number of iterations
  35. termination_eps = 1e-10 # Specify the threshold of the increment in the correlation coefficient between two iterations
  36.  
  37. # Specify the overall criteria
  38. criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations, termination_eps)
  39.  
  40. (cc, warp_matrix) = cv2.findTransformECC(current1_array, current2_array, warp_matrix, warp_mode, criteria, None, 1)
  41.  
  42. if printdetails == True:
  43. scan2_aligned = cv2.warpAffine(current2_array, warp_matrix, (shape[1], shape[0]), flags = cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
  44. scan2_aligned = scan2_aligned.astype(np.float32)
  45. scan2_aligned[scan2_aligned == 0.0] = float("NaN")
  46.  
  47.  
  48.  
  49. scan2_aligned_ravel = scan2_aligned.ravel()
  50.  
  51. x = np.arange(np.shape(scan2_aligned_ravel)[0])
  52. plt.figure(figsize=(50,5))
  53. plt.plot(x, scan2_aligned_ravel)
  54. plt.show()
  55.  
  56.  
  57. fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize = (15, 15), constrained_layout = True)
  58.  
  59.  
  60. im1 = ax1.imshow(current1_array, origin = "lower", cmap = cm.afmhot)
  61. ax1.set_title("Scan #" + str(scan1))
  62.  
  63. ax1_divider = make_axes_locatable(ax1) # Introduce a divider next to the axis already in place
  64. 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.
  65. legend = colorbar(im1, cax = cax1) # Introduce a legend (colorbar) where the new axis has been placed
  66. cax1.yaxis.set_major_locator(AutoLocator())
  67. cax1.yaxis.set_minor_locator(AutoMinorLocator())
  68.  
  69. im2 = ax2.imshow(current2_array, origin = "lower", cmap = cm.afmhot)
  70. ax2.set_title("Scan #" + str(scan2))
  71.  
  72. ax2_divider = make_axes_locatable(ax2) # Introduce a divider next to the axis already in place
  73. 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.
  74. legend = colorbar(im2, cax = cax2) # Introduce a legend (colorbar) where the new axis has been placed
  75. cax2.yaxis.set_major_locator(AutoLocator())
  76. cax2.yaxis.set_minor_locator(AutoMinorLocator())
  77.  
  78.  
  79. im3 = ax3.imshow(scan2_aligned, origin = "lower", cmap = "inferno")
  80. ax3.set_title("Scan #" + str(scan2) + " aligned")
  81. ax3.set_ylim(bottom = 0)
  82.  
  83. ax3_divider = make_axes_locatable(ax3) # Introduce a divider next to the axis already in place
  84. 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.
  85. legend = colorbar(im3, cax = cax3) # Introduce a legend (colorbar) where the new axis has been placed
  86. cax3.yaxis.set_major_locator(AutoLocator())
  87. cax3.yaxis.set_minor_locator(AutoMinorLocator())
  88.  
  89. plt.tight_layout(w_pad=6)
  90.  
  91. return warp_matrix
  92.  
  93. ImageRegistration(181,182,printdetails=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement