我嘗試將DICOM(.dcm)圖檔轉換為PNG或JPG檔,但是轉換出來的檔案顏色不正常!
想詢問前輩們,是否知道哪一個環節出了問題~
兩種方式批量轉換的結果顏色都是錯誤的...
感謝前輩們不吝指點!
#補充說明:顏色錯誤是指原本應該白底黑字或彩色,但是成果顏色過紅...
import dicom2jpg
dicom_dir = "/Users/USER/Desktop/Database"
export_location = "/Users/USER/Desktop/Database2"
# convert all DICOM files in dicom_dir folder to jpg format
dicom2jpg.dicom2jpg(dicom_dir, export_location)
# convert all DICOM files in dicom_dir folder to png format
dicom2jpg.dicom2png(dicom_dir, export_location)
import pydicom
import os
import imageio
path = '/Users/USER/Desktop/Database/'
images = os.listdir(path)
#print(images)
for i in images:
#print(i)
in_ = path + i
out = 'jpg_path' + os.path.splitext(i)[0] + '.jpg'
ds = pydicom.read_file(in_)
img = ds.pixel_array
print(img.shape)
imageio.imwrite(out,img)
其實 froce 上面的那篇文章已經有提到
How to convert grayscale DICOM file to RGB DICOM file with Python
醫學影像的數值範圍不一樣,所以你要去取得 DICOM 內的相關參數值或是一般的數值(e.g., width: 400, level: 50 for abdominal CT),再做一次轉換
def apply_ct_window(img, window):
# window = (window width, window level)
R = (img-window[1]+0.5*window[0])/window[0]
R[R<0] = 0
R[R>1] = 1
return R
display_img = apply_ct_window(img, [400,50])
中文的類似這種
HU值和像素值的转化,以dicom数据为例
亨氏單位
HU值的计算公式:
HU = pixel_val * slope + intercept
補充:看了一下套件,應該會幫你做處理,可以再確認看看
https://pypi.org/project/dicom2jpg/
https://github.com/ykuo2/dicom2jpg