-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaligning_code_1
34 lines (27 loc) · 1.61 KB
/
aligning_code_1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import numpy as np
from skimage.registration import phase_cross_correlation
#numpy for numerical operations
#phase_cross_relation to align the images
#modify the size of the array to fit the size of your image
Dim_size1 = np.array((1000, 1000, 1000), dtype=int)
#read image from a raw file
with open('C:/Users/XXX/sequence_8bit-scaled.raw', 'rb') as f:
img_pre = np.fromfile(f, dtype=np.uint8) #open raw image file and read it into img_pre
img_pre = img_pre.reshape(Dim_size1[0], Dim_size1[1], Dim_size1[2]) #reshape the image data into the dimensions given for the array before
#empty list to store aligned images
aligned_images = []
#reference image for alignment
reference_image = img_pre[0, :, :] #set as the first slice of the image stack
#loop through each slice to align the images
for i in range(0, Dim_size1[0]): #start from the first slice
imgtest = img_pre[i, :, :] #extract the current slice
#calculate the shift needed to align the current slice with the reference image
shift = phase_cross_correlation(reference_image, imgtest)[0]
#apply the calculated shift to align the current slice
aligned_image = np.roll(imgtest, shift.astype(int), axis=(0, 1))
aligned_images.append(aligned_image)
#save the aligned images
save_path = 'C:/Users/XXXX/sequence_8bit_aligned.raw' #define save path
with open(save_path, 'wb') as f:
for aligned_image in aligned_images:
f.write(aligned_image.tobytes()) #write each aligned image to file