Dicom Image Slices Viewer

Hello, I’m trying to use your code for Image Slices Viewer in Python to adapt the code for Dicom images. The result that I obtain running the code is wrong, I should watch a medical image and have the posibility to surf the images using the sroll, however, I obtain a plot with the information desroganized. I guess the problem is how the array is built, but I have not been able to fix it. I’m attachin a capture of the image that I get with this code.

This is the code adapted:

import pydicom
import matplotlib.pyplot as plt
import numpy as np
import os

class IndexTracker(object):
def init(self, ax, X):
self.ax = ax
ax.set_title(‘use scroll wheel to navigate images’)

    self.X = X
    self.slices, rows, columns  = X.shape
    self.ind = self.slices 

    self.im = plt.imshow(self.X[:, :,self.ind - 1], cmap='gray')

def onscroll(self, event):
    print("%s %s" % (event.button, event.step))
    if event.button == 'up':
        self.ind = (self.ind + 1) % self.slices
        self.ind = (self.ind - 1) % self.slices

def update(self):
    self.im.set_data(self.X[:, :, self.ind - 1])
    self.ax.set_ylabel('slice %s' % self.ind)

fig, ax = plt.subplots(1, 1)

print(‘-’ * 100)
path = ‘/Users/miguelyanez/Desktop/python/Proyectos_Miguel/Dicom/pruebas_miguel/Anonymized_20230228’
files = os.listdir(path)

imagenes =
skipcount = 0
for image in files:

im = pydicom.dcmread(f'{path}/{image}')
if hasattr(im, 'SliceLocation'):
    skipcount += 1

X = np.array(imagenes)

tracker = IndexTracker(ax, X)
fig.canvas.mpl_connect(‘scroll_event’, tracker.onscroll)


Here you have placed every image in a list meaning the outermost index (i.e., the first) indicates which image is which.

But this is using the last index to differentiate images.