Search code examples
pythonmatplotlibrotationdicom

Contour data rotation in python in dicom image


I have a huge data text file. I plot these data on a CT image which results can be seen on the below image. But how could I rotate my dose data axis to be same with my CT image ?

enter image description here

My code so far:

%matplotlib notebook
import numpy as np
import dicom
import matplotlib.pyplot as plt
from itertools import islice
import matplotlib.colors as colors
import matplotlib.cm as cm
data = dicom.read_file("NEW.dcm")
plt.set_cmap("gray")

pixel_array = data.pixel_array

plt.pcolormesh(pixel_array)
plt.gca().set_aspect("equal")
plt.show()
CS=plt.contour(xi,yi,zi,cmap=plt.cm.jet)

import numpy as np
import matplotlib.pyplot as plt

a = np.loadtxt('New.txt')
i = a[:,1]
j = a[:,2]
energies = a[:,3]
xi = np.linspace(i.min(), i.max())
yi = np.linspace(j.min(), j.max())
zi = scipy.interpolate.griddata((i, j), energies, (xi[None,:], yi[:,None]), method='cubic')
fig = plt.figure()
CS=plt.contour(xi, yi, zi,colors='k',  norm=plt.Normalize(vmax=abs(zi).max(), vmin=-abs(zi).max()))
CS = plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)

Data extract

30  1   2   0.00951305
30  1   3   0.0110269
30  1   4   0.0141366
30  1   5   0.00468656
30  1   6   0.0144487
30  1   7   0.0253241
30  1   8   0.0239877
30  1   9   0.0175475
30  1   10  0.0134009
…

(Full code and data available here)

Thanks you.


Solution

  • If you transpose z grid, you will have the contours rotated. In last two lines change z to z.T:

    CS=plt.contour(xi, yi, zi.T,colors='k', norm=plt.Normalize(vmax=abs(zi).max(), vmin=-abs(zi).max()))
    CS = plt.contourf(xi, yi, zi.T,15,cmap=plt.cm.jet)
    

    Remember that in images X axis is horizontal and Y is vertical, but for matrices 1st dimension (X) is vertical and 2nd (Y) is horizontal.