【发布时间】:2019-12-05 15:41:22
【问题描述】:
我有一个函数,它接受一个增强的 dicom 文件并执行以下操作:
- 使用 for 循环从 dicom 文件创建单个切片,然后将其索引到围绕一组六个斑点的较小数组中。
- 第二个 for 循环围绕六个斑点和一个背景绘制圆圈
- 显示带有绘制的所有圆圈和当前切片位置的图像。
当我调用该函数时,我可以创建多个以单独的图形显示的图像。那么,我怎样才能将所有这些数字合二为一呢?理想情况下,我想将它们显示在 3x3 网格中。
这是我当前的代码:
import matplotlib.pyplot as plt
import pydicom # Used for opening DICOM files
import numpy as np # General mathematical package
from pylab import text
# Import DICOM files
filename = "U:/File location"
ds = pydicom.dcmread(filename)
# Speck Locations
centerSpeck = (1690, 1477)
centerSpeck2 = (100, 100)
twelveOclockSpeck = (45, 84)
twoOkclockSpeck = (66, 148)
tenOclockSpeck = (136, 147)
fiveOclockSpeck = (157, 82)
sevenOclockSpeck = (102, 41)
backgroundSignalValue = (71, 63)
speckLocations = np.array([centerSpeck2, twelveOclockSpeck, twoOkclockSpeck, fiveOclockSpeck, sevenOclockSpeck, tenOclockSpeck, backgroundSignalValue])
# Function that draws a circle around a given pixel
def drawCircle(arrayToPLot, zeroIndex, oneIndex, specks, r = 10):
x = speckLocations[:,1] # get x axis variables from the speckLocations array
y = speckLocations[:,0] # get y axis variables from the speckLocations array
for i in range(zeroIndex,oneIndex,1): # For loop, note that the function range is: range(start, stop, step)
tempIm = arrayToPLot.pixel_array[i,:,:].astype(float) # Get one slice as float
slicedArray = tempIm[centerSpeck[0]-100:centerSpeck[0]+100, centerSpeck[1]-100:centerSpeck[1]+100].astype(float)
for x,y in (speckLocations):
plt.imshow(slicedArray, cmap='gray')
circle = plt.Circle((y, x),r, fc='none', ec="red")
plt.gca().add_patch(circle)
text(10, 180, i, fontsize=12, color='red') # Print the current slice on the image
plt.show() # Plot each slice with circles drawn around all the specks and the background signal value location
#Call the function
drawCircle(ds, 33, 34, speckLocations)
【问题讨论】:
-
创建一个带有 3x3 子图的图形。然后将每个图像绘制到其中一个轴上。
-
你能给我更多关于如何做到这一点的信息吗?我想出了如何用 3x3 子图创建一个图形,但无法将我的图形放入其中。我正在使用此页面中的说明:matplotlib.org/3.1.1/gallery/recipes/create_subplots.html.
标签: python numpy matplotlib dicom