0

I am trying to create the DICOM viewer in python. I would like to use scroll mouse to skip images. It is working quite nice but the process is very slow, especially if I would like to do it in the full screen mode. I know that matplotlibt is not the faster library to do it. I know I could maybe use the fortbyte function from Pillow but I would like to use Navigation toolbar in the future. I was trying to get work the blit over matplotlibt, however I could not get it work properly. Does somebody has any idea to improve the speed of the image presented? Below I posted my code. Thanks in advance.

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()
Kamil
  • 1

1 Answers1

0

It seems that adding ax.clear() help already much with the speed of the view. However, if somebody has better view to imrove this code I will be glad to hear about it.

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))
Kamil
  • 1