File size: 1,525 Bytes
4021c62 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
"""
Apply CT windowing parameter from DL_info.csv to Images_png
"""
import os
import cv2
import numpy as np
import pandas as pd
from glob import glob
from tqdm import tqdm
dir_in = '../Images_png'
dir_out = '../Images_png_wn'
info_fn = '../DL_info.csv'
if not os.path.exists(dir_out):
os.mkdir(dir_out)
dl_info = pd.read_csv(info_fn)
def clip_and_normalize(np_image: np.ndarray,
clip_min: int = -150,
clip_max: int = 250
) -> np.ndarray:
np_image = np.clip(np_image, clip_min, clip_max)
np_image = (np_image - clip_min) / (clip_max - clip_min)
return np_image
for idx, row in tqdm(dl_info.iterrows(), total=len(dl_info)):
folder = row['File_name'].rsplit('_', 1)[0]
images = sorted(glob(f'{dir_in}/{folder}/*.png'))
if not os.path.exists(f'{dir_out}/{folder}'):
os.mkdir(f'{dir_out}/{folder}')
DICOM_windows = [float(value.strip()) for value in row['DICOM_windows'].split(',')]
for im in images:
try:
image = cv2.imread(im, cv2.IMREAD_UNCHANGED)
image = image.astype('int32') - 32768
image = clip_and_normalize(image, *DICOM_windows)
image = (image*255).astype('uint8')
cv2.imwrite(f'{dir_out}/{folder}/{os.path.basename(im)}', image)
except AttributeError:
# Broken Images
# 001821_07_01/372.png
# 002161_04_02/116.png
print(f'Conversion failed: {im}')
continue
|