forked from sangjoon-park/Medical_X-VL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcm2jpg.py
39 lines (31 loc) · 991 Bytes
/
dcm2jpg.py
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
import glob
import os
import pydicom
import cv2
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
dir = '/8TB_hdd/covidx/'
all_images = glob.glob(dir + '/**/*.jpg', recursive=True)
all_images.extend(glob.glob(dir + '/**/*.png', recursive=True))
for dcm in tqdm(all_images):
img = cv2.imread(dcm, 0)
# dicom = pydicom.dcmread(dcm)
# img = dicom.pixel_array
# monochrome = dicom.PhotometricInterpretation
# ds.SetFileName(dcm)
# img = sitk.GetArrayFromImage(sitk.ReadImage(dcm))[0]
# img = np.asarray(img)
img = cv2.resize(img, dsize=(320, 320))
img = img - img.min()
img = img / img.max()
# if monochrome == 'MONOCHROME2':
# pass
# elif monochrome == 'MONOCHROME1':
# img = 1. - img
# else:
# raise AssertionError()
img = img * 255
img = img.astype(np.uint8)
img = cv2.equalizeHist(img)
cv2.imwrite(dcm.replace('.png', '.jpg'), img, [int(cv2.IMWRITE_JPEG_QUALITY), 95])