|
@@ -1,11 +1,12 @@
|
|
import nibabel as nib
|
|
import nibabel as nib
|
|
import numpy as np
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import matplotlib.pyplot as plt
|
|
|
|
+from scipy.ndimage import rotate
|
|
|
|
|
|
# Define the file paths
|
|
# Define the file paths
|
|
templateFilePath = r"C:\Users\aswen\Desktop\Code\2024_Ruthe_SND\input\average_template_50_from_website.nii.gz"
|
|
templateFilePath = r"C:\Users\aswen\Desktop\Code\2024_Ruthe_SND\input\average_template_50_from_website.nii.gz"
|
|
densityFilePath = r"C:\Users\aswen\Desktop\Code\2024_Ruthe_SND\input\12_wks_coronal_100141780_50um_projection_density.nii.gz"
|
|
densityFilePath = r"C:\Users\aswen\Desktop\Code\2024_Ruthe_SND\input\12_wks_coronal_100141780_50um_projection_density.nii.gz"
|
|
-maskFilePath = r"C:\Users\aswen\Desktop\Code\2024_Ruthe_SND\output\Nifti_Trakte_registered\CC_Mop_dilated_registered.nii.gz"
|
|
|
|
|
|
+maskFilePath = r"C:\Users\aswen\Desktop\Code\2024_Ruthe_SND\output\Nifti_Trakte_registered\CC_Mop_desnitymask_50um.nii.gz"
|
|
|
|
|
|
# Load the NIfTI files
|
|
# Load the NIfTI files
|
|
templateNifti = nib.load(templateFilePath)
|
|
templateNifti = nib.load(templateFilePath)
|
|
@@ -15,13 +16,6 @@ maskNifti = nib.load(maskFilePath)
|
|
# Ensure that both images have the same data type (e.g., cast density to the data type of template)
|
|
# Ensure that both images have the same data type (e.g., cast density to the data type of template)
|
|
densityNifti = nib.Nifti1Image(densityNifti.get_fdata().astype(templateNifti.get_fdata().dtype), densityNifti.affine)
|
|
densityNifti = nib.Nifti1Image(densityNifti.get_fdata().astype(templateNifti.get_fdata().dtype), densityNifti.affine)
|
|
|
|
|
|
-# Load the mask data and normalize it to [0, 1]
|
|
|
|
-maskData = maskNifti.get_fdata()
|
|
|
|
-maskData = (maskData - np.min(maskData)) / (np.max(maskData) - np.min(maskData))
|
|
|
|
-
|
|
|
|
-# Define the color for the mask overlay (e.g., blue)
|
|
|
|
-mask_color = np.array([0, 0, 1]) # Blue
|
|
|
|
-
|
|
|
|
# Customize the figure DPI
|
|
# Customize the figure DPI
|
|
dpi = 400
|
|
dpi = 400
|
|
|
|
|
|
@@ -29,10 +23,10 @@ dpi = 400
|
|
num_slices_dim0, num_slices_dim1, num_slices_dim2 = templateNifti.shape
|
|
num_slices_dim0, num_slices_dim1, num_slices_dim2 = templateNifti.shape
|
|
|
|
|
|
# Set the slice step for display (every tenth slice)
|
|
# Set the slice step for display (every tenth slice)
|
|
-slice_step = 2
|
|
|
|
|
|
+slice_step = 10
|
|
|
|
|
|
# Choose the dimension for iteration (0, 1, or 2)
|
|
# Choose the dimension for iteration (0, 1, or 2)
|
|
-iteration_dimension = 0 # Change this value to select the dimension
|
|
|
|
|
|
+iteration_dimension = 1 # Change this value to select the dimension
|
|
|
|
|
|
# Calculate the number of rows and columns for the grid based on the selected dimension
|
|
# Calculate the number of rows and columns for the grid based on the selected dimension
|
|
if iteration_dimension == 0:
|
|
if iteration_dimension == 0:
|
|
@@ -55,6 +49,12 @@ for ax_row in axs:
|
|
for ax in ax_row:
|
|
for ax in ax_row:
|
|
ax.axis('off')
|
|
ax.axis('off')
|
|
|
|
|
|
|
|
+# Load the mask and perform necessary flips
|
|
|
|
+mask_data = np.squeeze(maskNifti.get_fdata())
|
|
|
|
+# Swap the mask over the x and y axes
|
|
|
|
+mask_data = mask_data.swapaxes(0, 2)
|
|
|
|
+mask_data = np.flip(mask_data, axis=0)
|
|
|
|
+
|
|
# Loop through and display every nth slice as subplots along the selected dimension
|
|
# Loop through and display every nth slice as subplots along the selected dimension
|
|
for i, sliceIdx in enumerate(range(0, num_slices, slice_step)):
|
|
for i, sliceIdx in enumerate(range(0, num_slices, slice_step)):
|
|
row = i // grid_size
|
|
row = i // grid_size
|
|
@@ -64,27 +64,27 @@ for i, sliceIdx in enumerate(range(0, num_slices, slice_step)):
|
|
if iteration_dimension == 0:
|
|
if iteration_dimension == 0:
|
|
templateSlice = np.squeeze(templateNifti.get_fdata()[sliceIdx, :, :]).astype(np.uint16)
|
|
templateSlice = np.squeeze(templateNifti.get_fdata()[sliceIdx, :, :]).astype(np.uint16)
|
|
densitySlice = np.squeeze(densityNifti.get_fdata()[sliceIdx, :, :]).astype(np.double)
|
|
densitySlice = np.squeeze(densityNifti.get_fdata()[sliceIdx, :, :]).astype(np.double)
|
|
- maskSlice = np.squeeze(maskData[sliceIdx, :, :])
|
|
|
|
|
|
+ maskSlice = np.squeeze(mask_data[sliceIdx, :, :])
|
|
elif iteration_dimension == 1:
|
|
elif iteration_dimension == 1:
|
|
templateSlice = np.squeeze(templateNifti.get_fdata()[:, sliceIdx, :]).astype(np.uint16)
|
|
templateSlice = np.squeeze(templateNifti.get_fdata()[:, sliceIdx, :]).astype(np.uint16)
|
|
densitySlice = np.squeeze(densityNifti.get_fdata()[:, sliceIdx, :]).astype(np.double)
|
|
densitySlice = np.squeeze(densityNifti.get_fdata()[:, sliceIdx, :]).astype(np.double)
|
|
- maskSlice = np.squeeze(maskData[:, sliceIdx, :])
|
|
|
|
|
|
+ maskSlice = np.squeeze(mask_data[:, sliceIdx, :])
|
|
else:
|
|
else:
|
|
templateSlice = np.squeeze(templateNifti.get_fdata()[:, :, sliceIdx]).astype(np.uint16)
|
|
templateSlice = np.squeeze(templateNifti.get_fdata()[:, :, sliceIdx]).astype(np.uint16)
|
|
densitySlice = np.squeeze(densityNifti.get_fdata()[:, :, sliceIdx]).astype(np.double)
|
|
densitySlice = np.squeeze(densityNifti.get_fdata()[:, :, sliceIdx]).astype(np.double)
|
|
- maskSlice = np.squeeze(maskData[:, :, sliceIdx])
|
|
|
|
|
|
+ maskSlice = np.squeeze(mask_data[:, :, sliceIdx])
|
|
|
|
|
|
# Normalize the templateSlice to [0, 1] for visualization
|
|
# Normalize the templateSlice to [0, 1] for visualization
|
|
templateSlice = (templateSlice - np.min(templateSlice)) / (np.max(templateSlice) - np.min(templateSlice))
|
|
templateSlice = (templateSlice - np.min(templateSlice)) / (np.max(templateSlice) - np.min(templateSlice))
|
|
|
|
|
|
- # Overlay the density onto the template slice
|
|
|
|
- overlayedImage = templateSlice + densitySlice
|
|
|
|
-
|
|
|
|
- # Apply the mask color to the mask region
|
|
|
|
- overlayedImage[np.where(maskSlice > 0.5)] = mask_color
|
|
|
|
|
|
+ # Overlay the density and mask onto the template slice
|
|
|
|
+ overlayedImage = templateSlice + densitySlice + maskSlice
|
|
|
|
|
|
# Display the overlaid image in the corresponding subplot
|
|
# Display the overlaid image in the corresponding subplot
|
|
- axs[row, col].imshow(overlayedImage, cmap='gray')
|
|
|
|
|
|
+ axs[row, col].imshow(templateSlice, cmap='gray')
|
|
|
|
+ axs[row, col].imshow(densitySlice, cmap='hot', alpha=0.3)
|
|
|
|
+ axs[row, col].imshow(maskSlice, cmap='cool', alpha=0.3) # Apply "hot" colormap to the mask
|
|
|
|
+ # Apply "cool" colormap to the mask
|
|
|
|
|
|
# Remove empty subplots if necessary
|
|
# Remove empty subplots if necessary
|
|
for i in range(num_slices // slice_step, grid_size * grid_size):
|
|
for i in range(num_slices // slice_step, grid_size * grid_size):
|