-
Notifications
You must be signed in to change notification settings - Fork 15
Description
Dear Author,
I’m using your label fusion tool for my Bachelor thesis on diffusion models. The script runs fine, but the fused output is fully black.
The atlas images have high, nearly identical intensities, which makes the weight maps almost the same. I suspect the issue is with normalization — I tried [-1,1] and [1,3], but both give black results.
Any advice would be greatly appreciated!
I’ve attached a simplified version of my script. Any advice or a reference script would be greatly appreciated.
Thank you!
`
import tempfile
import SimpleITK as sitk
from pathlib import Path
import numpy as np
from monai.transforms import Compose, ScaleIntensityRange, Resize
import torch
preprocess = Compose([
ScaleIntensityRange(a_min=0, a_max=255, b_min=-1, b_max=1, clip=True),
Resize(spatial_size=(512, 512), mode='bilinear', align_corners=False)
])
def apply_monai_preprocessing(slice_img):
# Convert to torch tensor format [C, H, W]
img_tensor = torch.tensor(slice_img, dtype=torch.float32).unsqueeze(0)
processed = preprocess(img_tensor)
return processed.squeeze(0).numpy()
def convert_and_fuse(input_nii, output_nii, label_fusion_path, r_radius=2):
img = sitk.ReadImage(input_nii)
vol = sitk.GetArrayFromImage(img)
z_slices = vol.shape[0]
tmp = Path("greedy_workdir")
tmp.mkdir(exist_ok=True)
slice_paths = []
for i in range(z_slices):
monai_prepped = apply_monai_preprocessing(vol[i])
slice_img = sitk.GetImageFromArray(monai_prepped.astype(np.float32))
path = tmp / f"atlas_{i:03d}.tif"
sitk.WriteImage(slice_img, str(path))
slice_paths.append(str(path))
# Select center slice as target
target_index = z_slices // 2
target_path = tmp / "fix_img.tif"
os.rename(slice_paths[target_index], target_path)
start = max(0, target_index - r_radius)
end = min(z_slices, target_index + r_radius + 1)
atlas_paths = [slice_paths[i] for i in range(start, end) if i != target_index]
fused_2d_path = tmp / "synthResult.tif"
weight_pattern = tmp / "weights%03d.tif"
cmd = [
label_fusion_path,
"2",
"-g", *atlas_paths,
"-m", "Joint[50,2]",
"-rp", "5x5",
"-rs", "0x0",
"-w", str(weight_pattern),
str(target_path),
str(fused_2d_path)
]
print(f" Radius: {r_radius}")
print(f" Running:", " ".join(map(str, cmd)))
subprocess.run(cmd, check=True)
# Read and validate output
fused_slice = sitk.GetArrayFromImage(sitk.ReadImage(str(fused_2d_path)))
if np.all(fused_slice == 0):
print(" Fused slice is completely black. Replacing with original.")
fused_slice = vol[target_index]
else:
print(f" Fused slice stats — min: {fused_slice.min()}, max: {fused_slice.max()}")
fused_vol = vol.copy()
fused_vol[target_index] = fused_slice
fused_img = sitk.GetImageFromArray(fused_vol.astype('uint8'))
fused_img.CopyInformation(img)
sitk.WriteImage(fused_img, output_nii)
print(f"\n Self-fused result saved to: {output_nii}")
print(f"Temporary data saved to: {tmp}")
Example usage
if name == "main":
convert_and_fuse(
input_nii="/mnt/c/Users/PC1/Desktop/TestFusion/self_fusion/015.nii_processed.nii.gz",
output_nii="/mnt/c/Users/PC1/Desktop/TestFusion/self_fusion/015.nii_sfusion_monai.nii.gz",
label_fusion_path="/mnt/c/Users/PC1/Desktop/fusion_software/OCT_DDPM/label-fusion/label_fusion",
r_radius=2
)
`
output of the script: and my debuging:
Running: /mnt/c/Users/PC1/Desktop/fusion_software/OCT_DDPM/label-fusion/label_fusion 2 -g /tmp/tmp3o8v2z2m/atlas_206.tif /tmp/tmp3o8v2z2m/atlas_207.tif /tmp/tmp3o8v2z2m/atlas_209.tif /tmp/tmp3o8v2z2m/atlas_210.tif -m Joint[30,2] -rp 5x5 -rs 0x0 -w /tmp/tmp3o8v2z2m/weights%03d.tif /tmp/tmp3o8v2z2m/fix_img.tif /tmp/tmp3o8v2z2m/synthResult.tif
LABEL FUSION PARAMETERS:
Target image: /tmp/tmp3o8v2z2m/fix_img.tif
Output image: /tmp/tmp3o8v2z2m/synthResult.tif
Mask image :
Atlas images:
0 /tmp/tmp3o8v2z2m/atlas_206.tif
1 /tmp/tmp3o8v2z2m/atlas_207.tif
2 /tmp/tmp3o8v2z2m/atlas_209.tif
3 /tmp/tmp3o8v2z2m/atlas_210.tif
Method: Joint
Alpha: 30
Beta: 2
Search Radius: [0, 0]
Patch Radius: [5, 5]
Weight Map Filename Pattern: /tmp/tmp3o8v2z2m/weights%03d.tif
Padding Enabled: 0
Executing with the default number of threads: 24
Output Requested Region: [5, 5], [545, 190] (103550 pixels)
No mask supplied, using whole image
...............................................................................................
VOTING
Fused slice is completely black. Replacing with the original.