Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import pydicom | |
| import cv2 | |
| def load_dicom_image(path): | |
| ds = pydicom.dcmread(path) | |
| img = ds.pixel_array.astype(np.float32) | |
| return img | |
| def resize_image(img, target_size=(224, 224)): | |
| return cv2.resize(img, target_size, interpolation=cv2.INTER_LINEAR) | |
| def normalize_image(img): | |
| return img / 255.0 | |
| def to_3channel(img): | |
| return np.stack([img, img, img], axis=-1) | |
| def preprocess_dicom(path): | |
| img = load_dicom_image(path) | |
| img = resize_image(img) | |
| img = normalize_image(img) | |
| img = to_3channel(img) | |
| img = np.expand_dims(img, axis=0) | |
| return img | |