【问题标题】:How to improve speed of ploting DICOM in the matplotlib如何提高在 matplotlib 中绘制 DICOM 的速度
【发布时间】:2020-04-04 21:23:56
【问题描述】:

我正在尝试在 python 中创建 DICOM 查看器。我想使用滚动鼠标跳过图像。它工作得很好,但过程很慢,特别是如果我想在全屏模式下进行。我知道 matplotlibt 不是更快的库。我知道我可以使用 Pillow 的 fortbyte 功能,但我想在将来使用导航工具栏。我试图在 matplotlibt 上进行 blit 工作,但是我无法让它正常工作。有人有任何想法来提高呈现图像的速度吗?下面我发布了我的代码。提前致谢。

import tkinter
import pydicom as dicom
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib import *
import time

pathDir = 'C:/DICOM/'
images = os.listdir(pathDir)

slices = [dicom.read_file(pathDir + s, force=True) for s in images]

rows = int(slices[0].Rows)
cols = int(slices[0].Columns)
RS = slices[0].RescaleSlope
RI = slices[0].RescaleIntercept
WC = slices[0].WindowCenter
WW = slices[0].WindowWidth
lv = (WC - WW) / 2
hv = (WC + WW) / 2

imageDim = np.zeros((rows, cols, len(slices)), 'int')
ii = int
for ii in range(len(slices)):
    imageDim[:, :, ii] = slices[ii].pixel_array * RS + RI

root = tkinter.Tk()
# root.wm_title("Try MouseWheel")
# root.geometry("1920x1080")
root.resizable(width=True, height=True)
slide = 128
minInt = np.min(imageDim)
maxInt = np.max(imageDim)
# print(minInt, maxInt)

def main_loop():
    fig = Figure()
    ax = fig.subplots()
    ax.imshow(imageDim[:, :, slide], cmap='gray', vmin=minInt, vmax=maxInt)
    fig.canvas = FigureCanvasTkAgg(fig, master=root)
    fig.canvas.draw()
    fig.canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand='true')
    fig.canvas.mpl_connect('scroll_event', mouse_wheel)


def mouse_wheel(event):
    global slide
    fig = event.canvas.figure
    ax = fig.axes[0]

    if event.step < 0:
        slide += event.step

    if event.step > 0:
        slide += event.step

    tstart = time.time()
    ax.imshow(imageDim[:, :, int(slide)], cmap='gray', vmin=minInt, vmax=maxInt)
    fig.canvas.draw()
    print('FPS:', 1 / (time.time() - tstart))


main_loop()
root.mainloop()

【问题讨论】:

    标签: python image matplotlib pydicom


    【解决方案1】:

    似乎添加 ax.clear() 对视图速度的帮助已经很大。但是,如果有人对改进此代码有更好的看法,我将很高兴听到它。

    def mouse_wheel(event):
        global slide
        fig = event.canvas.figure
        ax = fig.axes[0]
    
        if event.step < 0:
            slide += event.step
    
        if event.step > 0:
            slide += event.step
    
        tstart = time.time()
        ax.clear()
        ax.imshow(imageDim[:, :, int(slide)], cmap='gray', vmin=minInt, vmax=maxInt)
        fig.canvas.draw()
        print('FPS:', 1 / (time.time() - tstart))
    

    【讨论】:

    • 更新现有图形/轴也比每次都从头开始创建新图形更快。
    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 2015-01-28
    • 2021-06-15
    • 2019-01-22
    • 1970-01-01
    • 2016-10-19
    • 2012-05-17
    • 1970-01-01
    相关资源
    最近更新 更多