当我试图从一个方法返回一个数组时,我获得了一个有线行为,下面是代码:
import matplotlib.pyplot as plt
import numpy as np
import sys
class ClickAndRoi:
def __init__(self, fig = [], ax = []):
self.selected_pixel = []
if fig == []:
fig = plt.gcf()
if ax == []:
ax = plt.gca()
self.fig = fig
self.ax = ax
self._ID1 = self.fig.canvas.mpl_connect('button_press_event', self._onclick)
plt.waitforbuttonpress()
if sys.flags.interactive:
plt.show(block=False)
else:
plt.show()
def _onclick(self, event):
if event.inaxes:
x, y = event.xdata, event.ydata
self.selected_pixel = [x, y]
self.ax.scatter(self.selected_pixel[0],
self.selected_pixel[1],
s=5,
c='red',
marker='o')
plt.draw()
if sys.flags.interactive:
pass
else:
plt.close(self.fig)
pass
def createROI(self, currentImage, PETSlice):
print(self.selected_pixel)
circle_masked_Im = currentImage[:, :, PETSlice]
nx, ny = np.shape(circle_masked_Im)
cordx, cordy = np.ogrid[0:nx, 0:ny]
circle_mask = ((cordx - self.selected_pixel[1])**2 +
(cordy - self.selected_pixel[0])**2 < 10)
circle_masked_Im[circle_mask] = 0
return currentImage
我这样称呼它:
plt.imshow(Current_Image[:, :, 50]) #50 is the slice of the 3d image
myROI = ClickAndRoi()
segemented_Image = myROI.createROI(SUV, 50) #50 is the slice of the 3d image
plt.close()
plt.imshow(segemented_Image[:,:, 50])
该类的目标是单击三维图像切片(np.array
)中的一个点,并在此位置通过阈值分割图像。你知道吗
这种奇怪的行为出现在方法createROI
上。正如它现在写的,理论上,应该取currentImage
并返回currentImage
,但是,相反,它返回circle_masked_Im
!!你知道吗
为什么会有这种行为?你知道吗
谢谢!你知道吗
我根据Python: unwanted change of variable occurs中的答案解决了这个问题。你知道吗
问题似乎是Python中的变量是引用而不是值。使用deepcopy解决了问题:
并将变量赋值为
也许有一个更优雅的方法来解决这个问题,但这只是工作的罚款。你知道吗
相关问题 更多 >
编程相关推荐