[英]Is there a faster way to recover images from a large 2-D array using vectorized operations in numpy
我有一个尺寸为nx 1008的大型2-D数组(通常为0.5至2GB)。该数组包含多个图像,并且数组中的值实际上是像素值。 基本上,恢复这些图像的方法如下
这是我的解决方案
counter=0
dump=np.array([], dtype=np.uint16)
#pixelDat is the array shaped n x 1008 containing the pixel values
for j in xrange(len(pixelDat)):
#Check if it is the last row for a particular image
if(j == (260*(counter+1)+ counter)):
counter += 1
dump=np.append(dump, pixelDat[j][:64])
#Reshape dump to form the image and write it to a fits file
hdu = fits.PrimaryHDU(np.reshape(dump, (512,512)))
hdu.writeto('img'+str("{0:0>4}".format(counter))+'.fits', clobber=True)
#Clear dump to enable formation of next image
dump=np.array([], dtype=np.uint16)
else:
dump=np.append(dump, pixelDat[j])
我一直在想是否有办法加快整个过程。 我想到的第一件事是使用矢量化的numpy操作。 但是我不太确定如何在这种情况下应用它。
PS:不要担心装配和hdu部分。 它只是为我的图像创建一个.fits文件。
这是尝试使用flattening和np.split
。 它避免了复制数据。
def chop_up(pixelDat):
sh = pixelDat.shape
try:
# since the array is large we do not want a copy
# the next line will succeed only if we can reshape in-place
pixelDat.shape = -1
except:
return False # user must resort to other method
N = len(pixelDat)
split = (np.arange(0, N, 261*1008)[:, None] + (0, 512*512)).ravel()[1:]
if split[-1] > N:
split = split[:-2]
result = [x.reshape(512,512) for x in np.split(pixelDat, split) if len(x) == 512*512]
pixelDat.shape = sh
return result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.