我想遍历+1500帧的大tif堆栈,并为每个帧提取局部最大值的坐标。下面的代码可以完成这项工作,但是对于大文件来说速度非常慢。在较小的位(例如20帧)上运行时,每帧几乎都是立即完成的-在整个数据集上运行时,每帧需要几秒钟。
是否有运行更快代码的解决方案?我认为这是由于加载了较大的tiff文件而引起的-但是最初只需要一次?
我有以下代码:
from pims import ImageSequence
from skimage.feature import peak_local_max
def cmask(index,array):
radius = 3
a,b = index
nx,ny = array.shape
y,x = np.ogrid[-a:nx-a,-b:ny-b]
mask = x*x + y*y <= radius*radius
return(sum(array[mask])) # number of pixels
images = ImageSequence('tryhard_red_small.tif')
frame_list = []
x = []
y = []
int_liposome = []
BG_liposome = []
for i in range(len(images[0])):
tmp_frame = images[0][i]
xy = pd.DataFrame(peak_local_max(tmp_frame, min_distance=8,threshold_abs=3000))
x.extend(xy[0].tolist())
y.extend(xy[1].tolist())
for j in range(len(xy)):
index = x[j],y[j]
int_liposome.append(cmask(index,tmp_frame))
frame_list.extend([i]*len(xy))
print "Frame: ", i, "of ",len(images[0])
features = pd.DataFrame(
{'lip_int':int_liposome,
'y' : y,
'x' : x,
'frame' : frame_list})
最佳答案
您是否尝试过分析代码,例如在ipython中使用%prun
或%lprun
进行描述?这将准确告诉您减速发生的位置。
没有tif堆栈,我无法创建自己的版本,但是我怀疑问题是您正在使用列表存储所有内容。每次执行追加或扩展时,python都必须分配更多的内存。您可以尝试先获取最大值的总数,然后分配输出数组,然后重新运行以填充数组。像下面这样
# run through once to get the count of local maxima
npeaks = (len(peak_local_max(f, min_distance=8, threshold_abs=3000))
for f in images[0])
total_peaks = sum(npeaks)
# allocate storage arrays and rerun
x = np.zeros(total_peaks, np.float)
y = np.zeros_like(x)
int_liposome = np.zeros_like(x)
BG_liposome = np.zeros_like(x)
frame_list = np.zeros(total_peaks, np.int)
index_0 = 0
for frame_ind, tmp_frame in enumerate(images[0]):
peaks = pd.DataFrame(peak_local_max(tmp_frame, min_distance=8,threshold_abs=3000))
index_1 = index_0 + len(peaks)
# copy the data from the DataFrame's underlying numpy array
x[index_0:index_1] = peaks[0].values
y[index_0:index_1] = peaks[1].values
for i, peak in enumerate(peaks, index_0):
int_liposome[i] = cmask(peak, tmp_frame)
frame_list[index_0:index_1] = frame_ind
# update the starting index
index_0 = index_1
print "Frame: ", frame_ind, "of ",len(images[0])
关于python - 循环通过大的.tif堆栈(图像栅格)并提取位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50074548/