mlii0117 commited on
Commit
025842f
·
verified ·
1 Parent(s): 61b016d

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +54 -29
README.md CHANGED
@@ -74,38 +74,63 @@ pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
74
  ).to("cuda")
75
  generator = torch.Generator("cuda").manual_seed(0)
76
 
77
- prompt = "Convert this non-contrast CT slice to mimic a venous-phase contrast-enhanced CT. Brighten and enhance the portal and hepatic veins, and emphasize organ boundaries."
 
78
 
 
79
  def load_dicom_folder(dicom_folder):
80
  dicom_folder = os.path.join(dicom_folder, 'DICOM')
81
- files = sorted(glob(os.path.join(dicom_folder, "*")))
82
- slices = [dcmread(f).pixel_array.astype(np.float32) for f in files]
83
- volume = np.stack(slices, axis=0)
84
- volume += dcmread(files[0]).RescaleIntercept
85
- volume = np.clip(volume, -1000, 1000)
86
- return (volume + 1000) / 2000.0
87
-
88
- def process(volume):
89
- results = []
90
- for i in tqdm(range(volume.shape[0])):
91
- img = Image.fromarray((volume[i] * 255).astype(np.uint8)).convert("RGB")
92
- out = pipe(prompt, image=img, num_inference_steps=20,
93
- image_guidance_scale=1.5, guidance_scale=10,
94
- generator=generator).images[0]
95
- gray = np.array(out.convert("L")).astype(np.float32) / 255.0
96
- gray = gray * 2000 - 1000
97
- results.append(gray)
98
- return np.stack(results, axis=0)
99
-
100
- def save_nifti(volume, path):
101
- nib.save(nib.Nifti1Image(volume, np.eye(4)), path)
102
-
103
- input_path = "/path/to/dicom_folder"
104
- output_path = "/path/to/output.nii.gz"
105
-
106
- vol = load_dicom_folder(input_path)
107
- out_vol = process(vol)
108
- save_nifti(out_vol, output_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  ```
110
 
111
  ---
 
74
  ).to("cuda")
75
  generator = torch.Generator("cuda").manual_seed(0)
76
 
77
+ prompt_art = "Convert this non-contrast CT slice to mimic an arterial-phase contrast-enhanced CT. Brighten and enhance the aorta, major arteries, and adjacent organ boundaries to emphasize arterial flow, focusing on clarity and contrast in these areas while maintaining other features unchanged."
78
+ prompt_ven = "Convert this non-contrast CT slice to mimic a venous-phase contrast-enhanced CT. Brighten and enhance the veins, especially the portal and hepatic veins, and emphasize organ boundaries to mimic venous flow, focusing on brightness and contrast in these areas while maintaining other features unchanged."
79
 
80
+ # read all dicoms
81
  def load_dicom_folder(dicom_folder):
82
  dicom_folder = os.path.join(dicom_folder, 'DICOM')
83
+ dicom_files = sorted(glob(os.path.join(dicom_folder, "*")))
84
+
85
+ slices = []
86
+ for dicom_file in dicom_files:
87
+ ds = dcmread(dicom_file)
88
+ slices.append(ds.pixel_array.astype(np.float32))
89
+
90
+ dicom_array = np.stack(slices, axis=0)
91
+ dicom_array += ds.RescaleIntercept
92
+ dicom_array = np.clip(dicom_array, -1000, 1000)
93
+ dicom_array = (dicom_array + 1000) / 2000.0
94
+ return dicom_array
95
+
96
+ # transfer to RGB and send to diffusion
97
+ def process_slices(dicom_array):
98
+ outputs = []
99
+ for i in tqdm(range(dicom_array.shape[0])):
100
+ slice_img = (dicom_array[i] * 255).astype(np.uint8)
101
+ pil_img = Image.fromarray(slice_img).convert("RGB")
102
+
103
+ edited_image = pipe(
104
+ prompt, #### chose prompt_art or prompt_ven
105
+ image=pil_img,
106
+ num_inference_steps=20,
107
+ image_guidance_scale=1.5,
108
+ guidance_scale=10,
109
+ generator=generator,
110
+ ).images[0]
111
+
112
+ gray = edited_image.convert("L")
113
+ gray_np = np.array(gray).astype(np.float32) / 255.0
114
+ gray_np = gray_np * 2000 - 1000 # scale back to [-1000, 1000]
115
+ outputs.append(gray_np)
116
+
117
+ volume = np.stack(outputs, axis=0)
118
+ return volume
119
+
120
+ # save to nii.gz
121
+ def save_nifti(volume, output_path):
122
+ affine = np.eye(4)
123
+ nii = nib.Nifti1Image(volume, affine)
124
+ nib.save(nii, output_path)
125
+
126
+ # main function
127
+ def main(input_dicom_path, output_nifti_path):
128
+ dicom_array = load_dicom_folder(input_dicom_path)
129
+ edited_volume = process_slices(dicom_array)
130
+ save_nifti(edited_volume, output_nifti_path)
131
+
132
+ # DMEO
133
+ # main("/path/to/dicom_folder", "/path/to/output.nii.gz")
134
  ```
135
 
136
  ---