Views
No views yet
mlii0117/sd1.5_MPECTStable Diffusion v1.5 for medical image-to-image translationInstructPix2Pix framework to enable flexible prompt-conditioned generation, enabling control over contrast timing without requiring explicit paired data.pip install diffusers==0.25.0 nibabel pydicom tqdm pillow1from diffusers import StableDiffusionInstructPix2PixPipeline
2import torch
3
4pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
5 "mlii0117/sd1.5_MPECT", torch_dtype=torch.float16
6).to("cuda")
7generator = torch.Generator("cuda").manual_seed(0)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.
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."1import os
2import numpy as np
3import nibabel as nib
4from PIL import Image
5from glob import glob
6from tqdm import tqdm
7from pydicom import dcmread
8from diffusers import StableDiffusionInstructPix2PixPipeline
9
10pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
11 "mlii0117/sd1.5_MPECT", torch_dtype=torch.float16
12).to("cuda")
13generator = torch.Generator("cuda").manual_seed(0)
14
15prompt_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."
16prompt_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."
17
18# read all dicoms
19def load_dicom_folder(dicom_folder):
20 dicom_folder = os.path.join(dicom_folder, 'DICOM')
21 dicom_files = sorted(glob(os.path.join(dicom_folder, "*")))
22
23 slices = []
24 for dicom_file in dicom_files:
25 ds = dcmread(dicom_file)
26 slices.append(ds.pixel_array.astype(np.float32))
27
28 dicom_array = np.stack(slices, axis=0)
29 dicom_array += ds.RescaleIntercept
30 dicom_array = np.clip(dicom_array, -1000, 1000)
31 dicom_array = (dicom_array + 1000) / 2000.0
32 return dicom_array
33
34# transfer to RGB and send to diffusion
35def process_slices(dicom_array):
36 outputs = []
37 for i in tqdm(range(dicom_array.shape[0])):
38 slice_img = (dicom_array[i] * 255).astype(np.uint8)
39 pil_img = Image.fromarray(slice_img).convert("RGB")
40
41 edited_image = pipe(
42 prompt, #### chose prompt_art or prompt_ven
43 image=pil_img,
44 num_inference_steps=20,
45 image_guidance_scale=1.5,
46 guidance_scale=10,
47 generator=generator,
48 ).images[0]
49
50 gray = edited_image.convert("L")
51 gray_np = np.array(gray).astype(np.float32) / 255.0
52 gray_np = gray_np * 2000 - 1000 # scale back to [-1000, 1000]
53 outputs.append(gray_np)
54
55 volume = np.stack(outputs, axis=0)
56 return volume
57
58# save to nii.gz
59def save_nifti(volume, output_path):
60 affine = np.eye(4)
61 nii = nib.Nifti1Image(volume, affine)
62 nib.save(nii, output_path)
63
64# main function
65def main(input_dicom_path, output_nifti_path):
66 dicom_array = load_dicom_folder(input_dicom_path)
67 edited_volume = process_slices(dicom_array)
68 save_nifti(edited_volume, output_nifti_path)
69
70# DMEO
71# main("/path/to/dicom_folder", "/path/to/output.nii.gz")⚠️ Disclaimer: This model is for research purposes only. It is not intended for clinical decision-making or diagnostic use.
@inproceedings{li2025text,
title={Text-Conditioned Latent Diffusion Model for Synthesis of Contrast-Enhanced CT from Non-Contrast CT},
author={Li, Mingjie and Chen, Yizheng and Xing, Lei and Gensheimer, Michael},
booktitle={AAPM Annual Meeting (Oral)},
year={2025}
}