使用python 读取DICOM文件,将其转化成图片,并使用PyQt5展示
ui.py文件代码
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1162, 912)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label.setText("")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1162, 26))
self.menubar.setObjectName("menubar")
self.menufile = QtWidgets.QMenu(self.menubar)
self.menufile.setObjectName("menufile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionopen = QtWidgets.QAction(MainWindow)
self.actionopen.setObjectName("actionopen")
self.menufile.addAction(self.actionopen)
self.menubar.addAction(self.menufile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menufile.setTitle(_translate("MainWindow", "file"))
self.actionopen.setText(_translate("MainWindow", "open"))
main.py代码
import sys
import pydicom
import numpy as np
from PIL import Image
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
import ui
def dicom_to_image(dicom_file, output_file):
# 读取DICOM文件
dicom = pydicom.dcmread(dicom_file)
# 提取像素数据
pixel_array = dicom.pixel_array
# 如果是多帧图像,选择第一帧
print(pixel_array.ndim)
if pixel_array.ndim > 2:
print(f"多维图像检测到,使用第一个维度的数据。原始shape: {pixel_array.shape}")
pixel_array = pixel_array[0]
# 将像素值归一化到[0, 255]范围内
image_array = (pixel_array - np.min(pixel_array)) / (np.max(pixel_array) - np.min(pixel_array)) * 255.0
image_array = image_array.astype(np.uint8)
# 检查数据维度并处理
if image_array.ndim == 3 and image_array.shape[2] == 1:
image_array = image_array[:, :, 0]
# 将NumPy数组转换为图像并保存
image = Image.fromarray(image_array)
image.save(output_file)
class MainWindow(QMainWindow,ui.Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
# 绑定点击函数
self.actionopen.triggered.connect(self.open_file)
def open_file(self):
filename, _ = QFileDialog.getOpenFileName(None, "dicom file", "./", "*.dcm", "")
if filename:
print(f"文件名称:{filename}")
dicom_to_image(filename, "output_image.png")
self.show_image_in_label("output_image.png",self.label)
def show_image_in_label(self,path,label):
# 如果是图像文件,则显示图像
pixmap = QPixmap(path)
# 将图像缩放为控件大小
pixmap_scaled = pixmap.scaled(label.size(), aspectRatioMode=Qt.KeepAspectRatio)
# 在label上显示图像
label.setPixmap(pixmap_scaled)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
文件转化代码
def dicom_to_image(dicom_file, output_file):
# 读取DICOM文件
dicom = pydicom.dcmread(dicom_file)
# 提取像素数据
pixel_array = dicom.pixel_array
# 如果是多帧图像,选择第一帧
print(pixel_array.ndim)
if pixel_array.ndim > 2:
print(f"多维图像检测到,使用第一个维度的数据。原始shape: {pixel_array.shape}")
pixel_array = pixel_array[0]
# 将像素值归一化到[0, 255]范围内
image_array = (pixel_array - np.min(pixel_array)) / (np.max(pixel_array) - np.min(pixel_array)) * 255.0
image_array = image_array.astype(np.uint8)
# 检查数据维度并处理
if image_array.ndim == 3 and image_array.shape[2] == 1:
image_array = image_array[:, :, 0]
# 将NumPy数组转换为图像并保存
image = Image.fromarray(image_array)
image.save(output_file)