医 学 系 统 主 功 能 设 计 医学系统主功能设计 医学系统主功能设计
主功能一: dicom的图像和tag信息展示
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main_window_ui.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1200, 800)
MainWindow.setMinimumSize(QtCore.QSize(1200, 800))
MainWindow.setMaximumSize(QtCore.QSize(200000, 800))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(10, 11, 1171, 701))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setSpacing(0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.file_dir_tree = QtWidgets.QTreeView(self.layoutWidget)
self.file_dir_tree.setObjectName("file_dir_tree")
self.horizontalLayout.addWidget(self.file_dir_tree)
self.img_show = QtWidgets.QLabel(self.layoutWidget)
self.img_show.setMinimumSize(QtCore.QSize(0, 0))
self.img_show.setMaximumSize(QtCore.QSize(100000, 100000))
self.img_show.setObjectName("img_show")
self.horizontalLayout.addWidget(self.img_show)
self.tag_view = QtWidgets.QTableView(self.layoutWidget)
self.tag_view.setMaximumSize(QtCore.QSize(256, 16777215))
self.tag_view.setObjectName("tag_view")
self.horizontalLayout.addWidget(self.tag_view)
self.horizontalLayout.setStretch(0, 2)
self.horizontalLayout.setStretch(1, 8)
self.horizontalLayout.setStretch(2, 2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1200, 30))
self.menubar.setMinimumSize(QtCore.QSize(0, 30))
self.menubar.setObjectName("menubar")
self.menuq = QtWidgets.QMenu(self.menubar)
self.menuq.setObjectName("menuq")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setMinimumSize(QtCore.QSize(0, 30))
self.menu.setObjectName("menu")
self.menu_2 = QtWidgets.QMenu(self.menubar)
self.menu_2.setObjectName("menu_2")
self.menu_3 = QtWidgets.QMenu(self.menubar)
self.menu_3.setObjectName("menu_3")
self.menu_4 = QtWidgets.QMenu(self.menubar)
self.menu_4.setObjectName("menu_4")
self.menu_5 = QtWidgets.QMenu(self.menubar)
self.menu_5.setObjectName("menu_5")
self.menu_6 = QtWidgets.QMenu(self.menubar)
self.menu_6.setObjectName("menu_6")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setMinimumSize(QtCore.QSize(0, 30))
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.toolBar_2 = QtWidgets.QToolBar(MainWindow)
self.toolBar_2.setObjectName("toolBar_2")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar_2)
self.toolBar_3 = QtWidgets.QToolBar(MainWindow)
self.toolBar_3.setObjectName("toolBar_3")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar_3)
self.toolBar_4 = QtWidgets.QToolBar(MainWindow)
self.toolBar_4.setObjectName("toolBar_4")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar_4)
self.action = QtWidgets.QAction(MainWindow)
self.action.setObjectName("action")
self.action_2 = QtWidgets.QAction(MainWindow)
self.action_2.setObjectName("action_2")
self.open_file = QtWidgets.QAction(MainWindow)
self.open_file.setObjectName("open_file")
self.open_dir = QtWidgets.QAction(MainWindow)
self.open_dir.setObjectName("open_dir")
self.action_3 = QtWidgets.QAction(MainWindow)
self.action_3.setObjectName("action_3")
self.action_4 = QtWidgets.QAction(MainWindow)
self.action_4.setObjectName("action_4")
self.action_5 = QtWidgets.QAction(MainWindow)
self.action_5.setObjectName("action_5")
self.action_6 = QtWidgets.QAction(MainWindow)
self.action_6.setObjectName("action_6")
self.action_7 = QtWidgets.QAction(MainWindow)
self.action_7.setObjectName("action_7")
self.action_8 = QtWidgets.QAction(MainWindow)
self.action_8.setObjectName("action_8")
self.action_9 = QtWidgets.QAction(MainWindow)
self.action_9.setObjectName("action_9")
self.action_10 = QtWidgets.QAction(MainWindow)
self.action_10.setObjectName("action_10")
self.action_12 = QtWidgets.QAction(MainWindow)
self.action_12.setObjectName("action_12")
self.action_13 = QtWidgets.QAction(MainWindow)
self.action_13.setObjectName("action_13")
self.action_14 = QtWidgets.QAction(MainWindow)
self.action_14.setObjectName("action_14")
self.action_15 = QtWidgets.QAction(MainWindow)
self.action_15.setObjectName("action_15")
self.action_16 = QtWidgets.QAction(MainWindow)
self.action_16.setObjectName("action_16")
self.action_17 = QtWidgets.QAction(MainWindow)
self.action_17.setObjectName("action_17")
self.menuq.addAction(self.action)
self.menuq.addAction(self.action_2)
self.menu_2.addAction(self.action_14)
self.menu_2.addAction(self.action_15)
self.menu_2.addAction(self.action_16)
self.menu_2.addAction(self.action_17)
self.menu_3.addAction(self.action_6)
self.menu_3.addAction(self.action_7)
self.menu_3.addAction(self.action_8)
self.menu_3.addAction(self.action_9)
self.menu_3.addAction(self.action_10)
self.menu_3.addAction(self.action_12)
self.menu_4.addAction(self.action_3)
self.menu_4.addAction(self.action_4)
self.menu_4.addAction(self.action_5)
self.menu_4.addAction(self.action_13)
self.menubar.addAction(self.menuq.menuAction())
self.menubar.addAction(self.menu.menuAction())
self.menubar.addAction(self.menu_2.menuAction())
self.menubar.addAction(self.menu_3.menuAction())
self.menubar.addAction(self.menu_4.menuAction())
self.menubar.addAction(self.menu_5.menuAction())
self.menubar.addAction(self.menu_6.menuAction())
self.toolBar.addAction(self.open_file)
self.toolBar.addAction(self.open_dir)
self.toolBar_2.addAction(self.action_14)
self.toolBar_2.addAction(self.action_15)
self.toolBar_2.addAction(self.action_16)
self.toolBar_2.addAction(self.action_17)
self.toolBar_3.addAction(self.action_6)
self.toolBar_3.addAction(self.action_7)
self.toolBar_3.addAction(self.action_8)
self.toolBar_3.addAction(self.action_9)
self.toolBar_4.addAction(self.action_3)
self.toolBar_4.addAction(self.action_4)
self.toolBar_4.addAction(self.action_5)
self.toolBar_4.addAction(self.action_13)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.img_show.setText(_translate("MainWindow", "TextLabel"))
self.menuq.setTitle(_translate("MainWindow", "文件"))
self.menu.setTitle(_translate("MainWindow", "保存"))
self.menu_2.setTitle(_translate("MainWindow", "绘画"))
self.menu_3.setTitle(_translate("MainWindow", "图像处理"))
self.menu_4.setTitle(_translate("MainWindow", "智能医学"))
self.menu_5.setTitle(_translate("MainWindow", "生成报告"))
self.menu_6.setTitle(_translate("MainWindow", "病情显示"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.toolBar_2.setWindowTitle(_translate("MainWindow", "toolBar_2"))
self.toolBar_3.setWindowTitle(_translate("MainWindow", "toolBar_3"))
self.toolBar_4.setWindowTitle(_translate("MainWindow", "toolBar_4"))
self.action.setText(_translate("MainWindow", "打开文件"))
self.action_2.setText(_translate("MainWindow", "打开文件夹"))
self.open_file.setText(_translate("MainWindow", "打开文件"))
self.open_dir.setText(_translate("MainWindow", "打开文件夹"))
self.action_3.setText(_translate("MainWindow", "图像分类"))
self.action_4.setText(_translate("MainWindow", "图像分割"))
self.action_5.setText(_translate("MainWindow", "图像检测"))
self.action_6.setText(_translate("MainWindow", "形态学"))
self.action_7.setText(_translate("MainWindow", "模糊度"))
self.action_8.setText(_translate("MainWindow", "边缘检测"))
self.action_9.setText(_translate("MainWindow", "亮度"))
self.action_10.setText(_translate("MainWindow", "对比度"))
self.action_12.setText(_translate("MainWindow", "旋转"))
self.action_13.setText(_translate("MainWindow", "三维重建"))
self.action_14.setText(_translate("MainWindow", "直线"))
self.action_15.setText(_translate("MainWindow", "矩形"))
self.action_16.setText(_translate("MainWindow", "圆形"))
self.action_17.setText(_translate("MainWindow", "自由绘制"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
import os
import pydicom
from PIL import Image
from resources.main_window_ui import Ui_MainWindow
class MyMainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
self.setupUi(self)
self.open_file.triggered.connect(self.call_back_action_open_file)
self.open_dir.triggered.connect(self.call_back_action_open_dir)
self.cwd = os.getcwd() # 获取当前路径
self.dir_model = QFileSystemModel() # 文件系统模型
self.dicom_tag_model = QStandardItemModel(5,1)
# 设置水平方向头标签文本内容
self.dicom_tag_model.setVerticalHeaderLabels(['ID', '姓名', '就诊日期', '性别', '年龄'])
self.dicom_tag_model.setHorizontalHeaderLabels(['值'])
def call_back_action_open_file(self):
print("call_back_action_open_file")
fileName_choose, filetype = QFileDialog.getOpenFileName(self,
"选取文件",
self.cwd, # 起始路径
"All Files (*);;Text Files (*.txt)") # 设置文件扩展名过滤,用双分号间隔
if fileName_choose == "":
print("\n取消选择")
return
print("\n你选择的文件夹为:")
print(fileName_choose)
'''
1.显示图像
'''
dcm = pydicom.read_file(fileName_choose)
raw_img = dcm.pixel_array
# img = Image.fromarray(raw_img)
# img.show()
# 2.映射到0-255之间
raw_flatten_img = raw_img.flatten()
max_val = max(raw_flatten_img)
min_val = min(raw_flatten_img)
raw_img = (raw_img - min_val) / (max_val - min_val) # 图像归一化
raw_img = raw_img * 255
img = Image.fromarray(raw_img)
img.convert('RGB').save("rgb_show.jpg", format='jpeg')
# img.show() # 正常
# 转QImage
# 显示图片
self.img_show.setPixmap(QPixmap("rgb_show.jpg"))
self.img_show.setScaledContents(True)
'''
2.显示Tag信息
'''
info = {}
info["PatientID"] = dcm.PatientID # 患者ID
info["PatientName"] = dcm.PatientName # 患者姓名
info['StudyDate'] = dcm.StudyDate # 检查日期
info['PatientSex'] = dcm.PatientSex # 患者性别
info["PatientAge"] = dcm.PatientAge # 患者年龄
# print(info)
val_list = []
for val in info.values():
val_list.append(val)
# 2.在tabel中显示元信息
for row in range(len(info)):
print(val_list[row])
item = QStandardItem(str(val_list[row]))
# 设置每个位置的文本值
self.dicom_tag_model.setItem(row, 0, item)
self.tag_view.setModel(self.dicom_tag_model)
def call_back_action_open_dir(self):
dir_choose = QFileDialog.getExistingDirectory(self,
"选取文件夹",
self.cwd) # 起始路径
if dir_choose == "":
print("\n取消选择")
return
print("\n你选择的文件夹为:")
print(dir_choose)
self.dir_model.setRootPath(dir_choose)
self.file_dir_tree.setModel(self.dir_model)
self.file_dir_tree.setRootIndex(self.dir_model.index(dir_choose))
self.file_dir_tree.clicked.connect(self.show_info)
self.file_dir_tree.setColumnWidth(0,300)
print("call_back_action_store_dir")
def show_info(self, signal):
'''
1.显示图像
'''
file_path=self.dir_model.filePath(signal)
# 1.pydicom读取dicom图像
dcm = pydicom.read_file(file_path)
raw_img = dcm.pixel_array
# img = Image.fromarray(raw_img)
# img.show()
# 2.映射到0-255之间
raw_flatten_img =raw_img.flatten()
max_val = max(raw_flatten_img)
min_val = min(raw_flatten_img)
raw_img = (raw_img-min_val)/(max_val-min_val) # 图像归一化
raw_img = raw_img*255
img = Image.fromarray(raw_img)
img.convert('RGB').save("rgb_show.jpg", format='jpeg')
# img.show() # 正常
# 转QImage
# 显示图片
self.img_show.setPixmap(QPixmap("rgb_show.jpg"))
self.img_show.setScaledContents(True)
'''
2.显示Tag信息
'''
info = {}
info["PatientID"] = dcm.PatientID # 患者ID
info["PatientName"] = dcm.PatientName # 患者姓名
info["PatientAge"] = dcm.PatientAge # 患者年龄
info['PatientSex'] = dcm.PatientSex # 患者性别
info['StudyDate'] = dcm.StudyDate # 检查日期
# print(info)
val_list = []
for val in info.values():
val_list.append(val)
# 2.在tabel中显示元信息
for row in range(len(info)):
print(val_list[row])
item = QStandardItem(str(val_list[row]))
# 设置每个位置的文本值
self.dicom_tag_model.setItem(row, 0, item)
self.tag_view.setModel(self.dicom_tag_model)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainWindow()
myWin.show()
sys.exit(app.exec_())
主功能二:绘画功能
主功能三:智能医学
主功能四:生成病情报告
主功能五:病人病情显示
主功能六:三维重建
import vtk
# 定义渲染窗口、交互模式
aRender = vtk.vtkRenderer()
Renwin = vtk.vtkRenderWindow()
Renwin.AddRenderer(aRender)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(Renwin)
# 定义个图片读取接口
#读取PNG图片就换成PNG_Reader = vtk.vtkPNGReader()
Jpg_Reader = vtk.vtkPNGReader()# vtk.vtkJPEGReader()
Jpg_Reader.SetNumberOfScalarComponents(1)
Jpg_Reader.SetFileDimensionality(3) # 说明图像是三维的
# 定义图像大小,本行表示图像大小为(512*512*240)
Jpg_Reader.SetDataExtent(0, 512, 0, 512, 0, 240)
# 设置图像的存放位置
Jpg_Reader.SetFilePrefix(r"./data/")
# 设置图像前缀名字
#表示图像前缀为数字(如:0.jpg)
Jpg_Reader.SetFilePattern("%s%d.png")
Jpg_Reader.Update()
Jpg_Reader.SetDataByteOrderToLittleEndian()
# 计算轮廓的方法
contour = vtk.vtkMarchingCubes()
contour.SetInputConnection(Jpg_Reader.GetOutputPort())
contour.ComputeNormalsOn()
contour.SetValue(0, 255)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(contour.GetOutputPort())
mapper.ScalarVisibilityOff()
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
renderer.SetBackground([0.1, 0.1, 0.5])
renderer.AddActor(actor)
window = vtk.vtkRenderWindow()
window.SetSize(512, 512)
window.AddRenderer(renderer)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(window)
# 开始显示
window.Render()
interactor.Initialize()
interactor.Start()
主功能七:图像的保存(jpg、dicom)
from PyQt5.Qt import *
import sys
import os
import pydicom
from PIL import Image
from resources.main_window_ui import Ui_MainWindow
class MyMainWindow(QMainWindow, Ui_MainWindow,QPainter):
'''
<1> 构造函数
'''
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
self.setupUi(self)
# 信号绑定槽函数
self.open_file.triggered.connect(self.call_back_action_open_file)
self.open_dir.triggered.connect(self.call_back_action_open_dir)
self.drawing_line.triggered.connect(self.DrawLine)
self.drawing_rect.triggered.connect(self.DrawRectangle)
self.drawing_circle.triggered.connect(self.DrawCircle)
self.drawing_polygon.triggered.connect(self.DrawPolygon)
self.drawing_text.triggered.connect(self.DrawText)
self.cwd = os.getcwd() # 获取当前路径
self.dir_model = QFileSystemModel() # 文件系统模型
self.dicom_tag_model = QStandardItemModel(5,1)
# 设置水平方向头标签文本内容
self.dicom_tag_model.setVerticalHeaderLabels(['ID', '姓名', '就诊日期', '性别', '年龄'])
self.dicom_tag_model.setHorizontalHeaderLabels(['值'])
'''
<2> 绘画
'''
def DrawLine(self):
self.update()
self.Draw = "Line"
print("line")
def DrawRectangle(self):
self.update()
self.Draw = "Rectangle"
print("Rectangle")
def DrawCircle(self):
self.update()
self.Draw = "Circle"
print("Circle")
def DrawPolygon(self):
self.update()
self.Draw = "Polygon"
print("Polygon")
def DrawText(self):
self.update()
self.Draw = "Text"
print("Text")
'''
<3> dicom的图像和tag信息展示
'''
def call_back_action_open_file(self):
print("call_back_action_open_file")
fileName_choose, filetype = QFileDialog.getOpenFileName(self,
"选取文件",
self.cwd, # 起始路径
"All Files (*);;Text Files (*.txt)") # 设置文件扩展名过滤,用双分号间隔
if fileName_choose == "":
print("\n取消选择")
return
print("\n你选择的文件夹为:")
print(fileName_choose)
'''
1.显示图像
'''
dcm = pydicom.read_file(fileName_choose)
raw_img = dcm.pixel_array
# img = Image.fromarray(raw_img)
# img.show()
# 2.映射到0-255之间
raw_flatten_img = raw_img.flatten()
max_val = max(raw_flatten_img)
min_val = min(raw_flatten_img)
raw_img = (raw_img - min_val) / (max_val - min_val) # 图像归一化
raw_img = raw_img * 255
img = Image.fromarray(raw_img)
img.convert('RGB').save("rgb_show.jpg", format='jpeg')
# img.show() # 正常
# 转QImage
# 显示图片
self.img_show.setPixmap(QPixmap("rgb_show.jpg"))
self.img_show.setScaledContents(True)
'''
2.显示Tag信息
'''
info = {}
info["PatientID"] = dcm.PatientID # 患者ID
info["PatientName"] = dcm.PatientName # 患者姓名
info['StudyDate'] = dcm.StudyDate # 检查日期
info['PatientSex'] = dcm.PatientSex # 患者性别
info["PatientAge"] = dcm.PatientAge # 患者年龄
# print(info)
val_list = []
for val in info.values():
val_list.append(val)
# 2.在tabel中显示元信息
for row in range(len(info)):
print(val_list[row])
item = QStandardItem(str(val_list[row]))
# 设置每个位置的文本值
self.dicom_tag_model.setItem(row, 0, item)
self.tag_view.setModel(self.dicom_tag_model)
def call_back_action_open_dir(self):
dir_choose = QFileDialog.getExistingDirectory(self,
"选取文件夹",
self.cwd) # 起始路径
if dir_choose == "":
print("\n取消选择")
return
print("\n你选择的文件夹为:")
print(dir_choose)
self.dir_model.setRootPath(dir_choose)
self.file_dir_tree.setModel(self.dir_model)
self.file_dir_tree.setRootIndex(self.dir_model.index(dir_choose))
self.file_dir_tree.clicked.connect(self.show_info)
self.file_dir_tree.setColumnWidth(0,300)
print("call_back_action_store_dir")
def show_info(self, signal):
'''
1.显示图像
'''
file_path=self.dir_model.filePath(signal)
# 1.pydicom读取dicom图像
dcm = pydicom.read_file(file_path)
raw_img = dcm.pixel_array
# img = Image.fromarray(raw_img)
# img.show()
# 2.映射到0-255之间
raw_flatten_img =raw_img.flatten()
max_val = max(raw_flatten_img)
min_val = min(raw_flatten_img)
raw_img = (raw_img-min_val)/(max_val-min_val) # 图像归一化
raw_img = raw_img*255
img = Image.fromarray(raw_img)
img.convert('RGB').save("rgb_show.jpg", format='jpeg')
# img.show() # 正常
# 转QImage
# 显示图片
self.img_show.setPixmap(QPixmap("rgb_show.jpg"))
self.img_show.setScaledContents(True)
'''
2.显示Tag信息
'''
info = {}
info["PatientID"] = dcm.PatientID # 患者ID
info["PatientName"] = dcm.PatientName # 患者姓名
info["PatientAge"] = dcm.PatientAge # 患者年龄
info['PatientSex'] = dcm.PatientSex # 患者性别
info['StudyDate'] = dcm.StudyDate # 检查日期
# print(info)
val_list = []
for val in info.values():
val_list.append(val)
# 2.在tabel中显示元信息
for row in range(len(info)):
print(val_list[row])
item = QStandardItem(str(val_list[row]))
# 设置每个位置的文本值
self.dicom_tag_model.setItem(row, 0, item)
self.tag_view.setModel(self.dicom_tag_model)
'''
<4> 图像的保存(jpg、dicom)
'''
'''
<5> 智能医学
'''
'''
<6> 三维重建
'''
'''
<7> 生成病情报告
'''
'''
<8> 病人病情显示
'''
if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainWindow()
myWin.show()
sys.exit(app.exec_())
主功能八:常规图像处理
8.1旋转
# 0.导入需要的包和模块
import pydicom
from PyQt5.Qt import *
import sys
from PIL import Image
import os
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Major")
self.resize(800, 800)
self.cwd = os.getcwd() # 获取当前路径
'''
1.展示图片的QLabel
'''
self.show_label = QLabel(self)
self.show_label.resize(512,512)
self.show_label.setStyleSheet("background-color:#888888")
self.show_label.move(100,100)
'''
2.打开dicom图片的btn
'''
self.open_btn = QPushButton(self)
self.open_btn.setText("打开Dicom图像")
self.open_btn.move(0,0)
self.open_btn.clicked.connect(self.open_dicom_file)
'''
3.控制图片的QSlider
'''
self.sd = QDial(self)
self.sd.move(200,0)
# 设置最大值和最小值
self.sd.setMinimum(0)
self.sd.setMaximum(360)
# 绑定槽函数
self.sd.valueChanged.connect(self.processing_img)
# 外观倒立
self.sd.setInvertedAppearance(True)
'''
4.处理的图像
'''
self.processed_img = None
def processing_img(self):
self.show_label.setText(str(self.sd.value()))
img = Image.open("rgb_show.jpg")
img_rotated = img.rotate(angle=self.sd.value())
img_rotated.save("rgb_rotated_show.jpg")
self.show_label.setPixmap(QPixmap("rgb_rotated_show.jpg"))
self.show_label.setScaledContents(True)
def open_dicom_file(self):
print(self.show_label)
fileName_choose, filetype = QFileDialog.getOpenFileName(self,
"选取文件",
self.cwd, # 起始路径
"All Files (*);;Text Files (*.txt)") # 设置文件扩展名过滤,用双分号间隔
if fileName_choose == "":
print("\n取消选择")
return
print("\n你选择的文件夹为:")
print(fileName_choose)
dcm = pydicom.read_file(fileName_choose)
raw_img = dcm.pixel_array
# img = Image.fromarray(raw_img)
# img.show()
# 2.映射到0-255之间
raw_flatten_img = raw_img.flatten()
max_val = max(raw_flatten_img)
min_val = min(raw_flatten_img)
raw_img = (raw_img - min_val) / (max_val - min_val) # 图像归一化
raw_img = raw_img * 255
img = Image.fromarray(raw_img)
img.convert('RGB').save("rgb_show.jpg", format='jpeg')
self.show_label.setPixmap(QPixmap("rgb_show.jpg"))
self.show_label.setScaledContents(True)
if __name__ == '__main__':
# 1.创建一个应用程序对象
app = QApplication(sys.argv)
# 2.控件的操作
# 2.1创建控件
window = Window()
# 2.2设置控件
# 2.3展示控件
window.show()
# 3.应用程序的执行,进入到信息循环
sys.exit(app.exec_())
8.2 图像的缩放
# 0.导入需要的包和模块
import cv2
import pydicom
from PyQt5.Qt import *
import sys
from PIL import Image
import os
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Major")
self.resize(800, 800)
self.cwd = os.getcwd() # 获取当前路径
'''
1.展示图片的QLabel
'''
self.show_label = QLabel(self)
# self.show_label.resize(512,512)
self.show_label.setStyleSheet("background-color:#888888")
self.show_label.move(100,100)
'''
2.打开dicom图片的btn
'''
self.open_btn = QPushButton(self)
self.open_btn.setText("打开Dicom图像")
self.open_btn.move(0,0)
self.open_btn.clicked.connect(self.open_dicom_file)
'''
3.控制图片的QSlider
'''
self.sd = QSlider(self)
self.sd.move(200,0)
# 设置最大值和最小值
self.sd.setMinimum(10)
self.sd.setMaximum(500)
# 绑定槽函数
self.sd.valueChanged.connect(self.processing_img)
# 外观倒立
self.sd.setInvertedAppearance(True)
'''
4.处理的图像
'''
self.processed_img = None
def processing_img(self):
self.show_label.setText(str(self.sd.value()))
img = cv2.imread('rgb_show.jpg')
# 方法一:通过设置缩放比例,来对图像进行放大或缩小
res1 = cv2.resize(img, None, fx=self.sd.value()/100, fy=self.sd.value()/100,
interpolation=cv2.INTER_CUBIC)
cv2.imwrite("scaled_img.jpg", res1)
self.show_label.setPixmap(QPixmap("scaled_img.jpg"))
self.show_label.adjustSize() # 窗口适应图片
def open_dicom_file(self):
print(self.show_label)
fileName_choose, filetype = QFileDialog.getOpenFileName(self,
"选取文件",
self.cwd, # 起始路径
"All Files (*);;Text Files (*.txt)") # 设置文件扩展名过滤,用双分号间隔
if fileName_choose == "":
print("\n取消选择")
return
print("\n你选择的文件夹为:")
print(fileName_choose)
dcm = pydicom.read_file(fileName_choose)
raw_img = dcm.pixel_array
# img = Image.fromarray(raw_img)
# img.show()
# 2.映射到0-255之间
raw_flatten_img = raw_img.flatten()
max_val = max(raw_flatten_img)
min_val = min(raw_flatten_img)
raw_img = (raw_img - min_val) / (max_val - min_val) # 图像归一化
raw_img = raw_img * 255
img = Image.fromarray(raw_img)
img.convert('RGB').save("rgb_show.jpg", format='jpeg')
self.show_label.setPixmap(QPixmap("rgb_show.jpg"))
self.show_label.setScaledContents(True)
if __name__ == '__main__':
# 1.创建一个应用程序对象
app = QApplication(sys.argv)
# 2.控件的操作
# 2.1创建控件
window = Window()
# 2.2设置控件
# 2.3展示控件
window.show()
# 3.应用程序的执行,进入到信息循环
sys.exit(app.exec_())
8.3仿射变换(需要仿射矩阵)
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图片
src = cv2.imread('bird.png')
#获取图像大小
rows, cols = src.shape[:2]
#设置图像仿射变换矩阵
pos1 = np.float32([[50,50], [200,50], [50,200]])
pos2 = np.float32([[10,100], [200,50], [100,250]])
M = cv2.getAffineTransform(pos1, pos2)
print(M)
#图像仿射变换
result = cv2.warpAffine(src, M, (2*cols, 2*rows))
#显示图像
cv2.imshow("original", src)
cv2.imshow("result", result)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
8.4透视变化(需要透视变换矩阵)
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图片
src = cv2.imread('bird.png')
#获取图像大小
rows, cols = src.shape[:2]
#设置图像透视变换矩阵
pos1 = np.float32([[114, 82], [287, 156],
[8, 100], [143, 177]])
pos2 = np.float32([[0, 0], [188, 0],
[0, 262], [188, 262]])
M = cv2.getPerspectiveTransform(pos1, pos2)
#图像透视变换
result = cv2.warpPerspective(src, M, (2*cols,2*rows))
#显示图像
cv2.imshow("original", src)
cv2.imshow("result", result)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
图像几何变化
1.平移
2.缩小
3.放大
4.中心旋转
5.翻转
6.仿射
7.透射
8.5图像滤波
8.5.1 方框滤波
8.5.2 均值模糊
8.5.3 高斯模糊
8.5.4 双边滤波
8.5.5 图像锐化
8.5.6 直方图均衡化
8.5.7 Gamma变化
8.6 形态学操作
8.6.1.腐蚀
8.6.2.膨胀
8.6.3.开运算
8.6.4.闭运算
8.6.5 .形态学梯度:
8.6.6 .顶帽
8.6.7.黑帽
8.7 亮度
# 0.导入需要的包和模块
import cv2
import pydicom
from PyQt5.Qt import *
import sys
from PIL import Image
import os
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Major")
self.resize(800, 800)
self.cwd = os.getcwd() # 获取当前路径
'''
1.展示图片的QLabel
'''
self.show_label = QLabel(self)
self.show_label.resize(512,512)
self.show_label.setStyleSheet("background-color:#888888")
self.show_label.move(100,100)
'''
2.打开dicom图片的btn
'''
self.open_btn = QPushButton(self)
self.open_btn.setText("打开Dicom图像")
self.open_btn.move(0,0)
self.open_btn.clicked.connect(self.open_dicom_file)
'''
3.控制图片的QSlider
'''
self.sd = QSlider(self)
self.sd.move(200,0)
# 设置最大值和最小值
self.sd.setMinimum(10)
self.sd.setMaximum(500)
# 绑定槽函数
self.sd.valueChanged.connect(self.processing_img)
# 外观倒立
self.sd.setInvertedAppearance(True)
'''
4.处理的图像
'''
self.processed_img = None
def processing_img(self):
self.show_label.setText(str(self.sd.value()))
print(self.sd.value()/100)
img = cv2.imread('rgb_show.jpg')
# 调节亮度
res = img*(self.sd.value()/100)
cv2.imwrite("light_img.jpg", res)
self.show_label.setPixmap(QPixmap("light_img.jpg"))
self.show_label.setScaledContents(True)
def open_dicom_file(self):
print(self.show_label)
fileName_choose, filetype = QFileDialog.getOpenFileName(self,
"选取文件",
self.cwd, # 起始路径
"All Files (*);;Text Files (*.txt)") # 设置文件扩展名过滤,用双分号间隔
if fileName_choose == "":
print("\n取消选择")
return
print("\n你选择的文件夹为:")
print(fileName_choose)
dcm = pydicom.read_file(fileName_choose)
raw_img = dcm.pixel_array
# img = Image.fromarray(raw_img)
# img.show()
# 2.映射到0-255之间
raw_flatten_img = raw_img.flatten()
max_val = max(raw_flatten_img)
min_val = min(raw_flatten_img)
raw_img = (raw_img - min_val) / (max_val - min_val) # 图像归一化
raw_img = raw_img * 255
img = Image.fromarray(raw_img)
img.convert('RGB').save("rgb_show.jpg", format='jpeg')
self.show_label.setPixmap(QPixmap("rgb_show.jpg"))
self.show_label.setScaledContents(True)
if __name__ == '__main__':
# 1.创建一个应用程序对象
app = QApplication(sys.argv)
# 2.控件的操作
# 2.1创建控件
window = Window()
# 2.2设置控件
# 2.3展示控件
window.show()
# 3.应用程序的执行,进入到信息循环
sys.exit(app.exec_())
8.8 对比度
# 0.导入需要的包和模块
import cv2
import pydicom
from PyQt5.Qt import *
import sys
from PIL import Image
import os
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Major")
self.resize(800, 800)
self.cwd = os.getcwd() # 获取当前路径
'''
1.展示图片的QLabel
'''
self.show_label = QLabel(self)
self.show_label.resize(512,512)
self.show_label.setStyleSheet("background-color:#888888")
self.show_label.move(100,100)
'''
2.打开dicom图片的btn
'''
self.open_btn = QPushButton(self)
self.open_btn.setText("打开Dicom图像")
self.open_btn.move(0,0)
self.open_btn.clicked.connect(self.open_dicom_file)
'''
3.控制图片的QSlider
'''
self.sd = QSlider(self)
self.sd.move(200,0)
# 设置最大值和最小值
self.sd.setMinimum(0)
self.sd.setMaximum(255)
# 绑定槽函数
self.sd.valueChanged.connect(self.processing_img)
# 外观倒立
self.sd.setInvertedAppearance(True)
'''
4.处理的图像
'''
self.processed_img = None
def processing_img(self):
self.show_label.setText(str(self.sd.value()))
print(self.sd.value()/100)
img = cv2.imread('rgb_show.jpg')
# 调节对比度
res = img+self.sd.value()
cv2.imwrite("light_img.jpg", res)
self.show_label.setPixmap(QPixmap("light_img.jpg"))
self.show_label.setScaledContents(True)
def open_dicom_file(self):
print(self.show_label)
fileName_choose, filetype = QFileDialog.getOpenFileName(self,
"选取文件",
self.cwd, # 起始路径
"All Files (*);;Text Files (*.txt)") # 设置文件扩展名过滤,用双分号间隔
if fileName_choose == "":
print("\n取消选择")
return
print("\n你选择的文件夹为:")
print(fileName_choose)
dcm = pydicom.read_file(fileName_choose)
raw_img = dcm.pixel_array
# img = Image.fromarray(raw_img)
# img.show()
# 2.映射到0-255之间
raw_flatten_img = raw_img.flatten()
max_val = max(raw_flatten_img)
min_val = min(raw_flatten_img)
raw_img = (raw_img - min_val) / (max_val - min_val) # 图像归一化
raw_img = raw_img * 255
img = Image.fromarray(raw_img)
img.convert('RGB').save("rgb_show.jpg", format='jpeg')
self.show_label.setPixmap(QPixmap("rgb_show.jpg"))
self.show_label.setScaledContents(True)
if __name__ == '__main__':
# 1.创建一个应用程序对象
app = QApplication(sys.argv)
# 2.控件的操作
# 2.1创建控件
window = Window()
# 2.2设置控件
# 2.3展示控件
window.show()
# 3.应用程序的执行,进入到信息循环
sys.exit(app.exec_())
主窗口框架设计
# 0.导入需要的包和模块
from PyQt5.Qt import *
import sys
class FramePane(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Major")
self.resize(1400, 900)
self.setup_ui()
def setup_ui(self):
# 创建子控件
menu_bar = QWidget()
menu_bar.setStyleSheet("background-color:yellow;")
tool_tar = QWidget()
tool_tar.setStyleSheet("background-color:red;")
left = QWidget()
left.setStyleSheet("background-color:blue;")
body = QWidget()
body.setStyleSheet("background-color:blue;")
right = QWidget()
right.setStyleSheet("background-color:blue;")
# 1.创建布局管理器对象
v_layout = QVBoxLayout()
h_layout = QHBoxLayout()
# 2.把布局管理器对象设置给需要布局的父控件
self.setLayout(v_layout)
# 3.添加需要布局的子控件到布局管理器当中
v_layout.addWidget(menu_bar)
v_layout.addWidget(tool_tar)
h_layout.addWidget(left)
h_layout.addWidget(body)
h_layout.addWidget(right)
v_layout.addLayout(h_layout)
# 4.设置伸缩因子
v_layout.setStretchFactor(menu_bar, 1)
v_layout.setStretchFactor(tool_tar, 1)
v_layout.setStretchFactor(h_layout, 20)
h_layout.setStretchFactor(left,1)
h_layout.setStretchFactor(body, 6)
h_layout.setStretchFactor(right, 1)
if __name__ == '__main__':
# 1.创建一个应用程序对象
app = QApplication(sys.argv)
# 2.控件的操作
# 2.1创建控件
window = FramePane()
# 2.2设置控件
# 2.3展示控件
window.show()
# 3.应用程序的执行,进入到信息循环
sys.exit(app.exec_())
# 0.导入需要的包和模块
from PyQt5.Qt import *
import sys
class FramePane(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Major")
self.resize(1400, 900)
self.setup_ui()
def setup_ui(self):
# 创建子控件
menu_bar = QWidget()
# menu_bar.setStyleSheet("background-color:yellow;")
tool_tar = QWidget()
# tool_tar.setStyleSheet("background-color:red;")
left = QWidget()
left.setStyleSheet("background-color:blue;")
body = QWidget()
body.setStyleSheet("background-color:blue;")
right = QWidget()
right.setStyleSheet("background-color:blue;")
# 1.创建布局管理器对象
v_layout = QVBoxLayout()
h_layout = QHBoxLayout()
# 2.把布局管理器对象设置给需要布局的父控件
self.setLayout(v_layout)
# 3.添加需要布局的子控件到布局管理器当中
v_layout.addWidget(menu_bar)
v_layout.addWidget(tool_tar)
h_layout.addWidget(left)
h_layout.addWidget(body)
h_layout.addWidget(right)
v_layout.addLayout(h_layout)
# 4.设置伸缩因子
v_layout.setStretchFactor(menu_bar, 1)
v_layout.setStretchFactor(tool_tar, 1)
v_layout.setStretchFactor(h_layout, 20)
h_layout.setStretchFactor(left,1)
h_layout.setStretchFactor(body, 6)
h_layout.setStretchFactor(right, 1)
'''
Menu_Bar
'''
file_btn = QPushButton(menu_bar)
file_btn.setText("file")
file_menu = QMenu()
open_recent_menu = QMenu(file_menu)
open_recent_menu.setTitle("最近打开")
# 1.新建
new_action = QAction(QIcon(r"D:\pyqt5\resources\images\1.png"), "新建", file_menu)
new_action.triggered.connect(lambda: print("新建文件"))
# 2.打开
open_action = QAction(QIcon(r"D:\pyqt5\resources\images\1.png"), "打开", file_menu)
open_action.triggered.connect(lambda: print("打开文件"))
# 3.退出
exit_action = QAction(QIcon(r"D:\pyqt5\resources\images\1.png"), "退出", file_menu)
exit_action.triggered.connect(lambda: print("退出程序"))
# 4.子菜单行为
file_action = QAction("python_test")
open_recent_menu.addAction(file_action)
# 添加
file_menu.addAction(new_action)
file_menu.addAction(open_action)
file_menu.addMenu(open_recent_menu)
file_menu.addSeparator()
file_menu.addAction(exit_action)
file_btn.setMenu(file_menu)
file_btn.setFlat(True)
'''
Tool_Bar
'''
tb = QToolButton(tool_tar)
tb.setText("工具")
tb.setIconSize(QSize(25,25))
tb.setToolTip("这是一个新建按钮")
tb.setAutoRaise(True)
tb1 = QToolButton(tool_tar)
tb1.setText("工具2")
tb1.move(60,0)
tb1.setIconSize(QSize(25,25))
tb1.setToolTip("这是一个新建按钮")
tb1.setAutoRaise(True)
'''
Left
'''
'''
Body
'''
'''
Right
'''
if __name__ == '__main__':
# 1.创建一个应用程序对象
app = QApplication(sys.argv)
# 2.控件的操作
# 2.1创建控件
window = FramePane()
# 2.2设置控件
# 2.3展示控件
window.show()
# 3.应用程序的执行,进入到信息循环
sys.exit(app.exec_())