我有一个使用numpy的Python脚本,它应该在返回单个值之前拍摄图像并执行一些计算。当我单独执行每条线时,它按预期工作。当我将它放在一个.py脚本中并从命令行或Canopy内运行时,它将返回一个数组。Python脚本返回一个数组而不是单个值
我已经修改了代码略微不要求通常的图像输入,结果是一样的:
import numpy as np
# Instead of loading an image, generate a test case (w or wo structured noise)
roi = np.random.poisson(38,(256,256));
blob = np.random.poisson(5,(128,128));
roi[64:192,64:192] = roi[64:192,64:192]+blob;
# Load the other variables if necessary (i.e., no DICOM to load)
[xDim,yDim] = [512,512];
roiLength = xDim/2;
pix = 1.18958;
# Declare memory for the FFTs
sizeFFT = xDim;
NPS2D = np.zeros((sizeFFT,sizeFFT)); # declare memory for fft results
fftslice = np.zeros((sizeFFT,sizeFFT));
# Set the dimension of the ROI and pull the pixel size. This will be
# used for the scaling factor in the 2D NPS.
deltaX = pix;
deltaY = pix;
scaleFactor = (deltaX/roiLength)*(deltaY/roiLength);
# Calculate the NPS
roiMean = np.mean(roi);
fftslice = np.fft.fft2((roi-roiMean),s=[sizeFFT,sizeFFT]);
NPS2D = scaleFactor*np.fft.fftshift(np.multiply(fftslice,np.conj(fftslice)));
NPS2D = NPS2D.real;
# Subtract the white noise from the NPS to get the structured NPS
stNPS = NPS2D - roiMean*deltaX*deltaY;
# Calculate SNI
SNI=sum(stNPS)/sum(NPS2D);
# Display the result
print SNI;
如果我执行每一行是0.107213670449(或类似的,因为它是再生结果随机数组)。如果我使用python foo.py
从命令行运行脚本,或单击Canopy中的播放按钮,结果是一个512长度的数组[4.64940089e-03 ... -4.59789051e-02 -7.15113682e-02]
,我已经手动删除了509个条目。
有什么想法?我错过了明显的东西吗?
感谢注意到,。我从MATLAB修改了这段代码,由于使用逐行执行方法在python中工作,我没有添加np。这两种执行方法之间'sum'的工作方式有什么不同? – 2015-04-06 14:02:27