[英]Struggling with Understanding Numpy Vectorization for my use case
I understand the basics of how vectorization works, but I'm struggling to see how to apply that knowledge to my use case.我了解矢量化工作原理的基础知识,但我很难了解如何将这些知识应用到我的用例中。 I have a working algorithm for some image processing.
我有一些图像处理的工作算法。 However, the particular algorithm that I'm working with doesn't process the entire image as there is a border to account for the "window" that gets shifted around the image.
但是,我正在使用的特定算法不会处理整个图像,因为有一个边框可以解释在图像周围移动的“窗口”。
I'm trying to use this to better understand Numpy's vectorization, but I can't figure out how to account for the window and the border.我试图用它来更好地理解 Numpy 的矢量化,但我不知道如何解释 window 和边界。 Below is what I have in vanilla python (with the actual algorithm redacted, I'm only asking for help on how to vectorize).
下面是我在 vanilla python 中的内容(经过编辑的实际算法,我只是寻求有关如何矢量化的帮助)。 I looked into np.fromfunction and a few other options, but have had no luck.
我查看了 np.fromfunction 和其他一些选项,但没有运气。 Any suggestions would be welcome at this point.
此时欢迎任何建议。
half_k = np.int(np.floor(k_size / 2));
U = np.zeros(img_a.shape, dtype=np.float64);
V = np.zeros(img_b.shape, dtype=np.float64);
for y in range(half_k, img_a.shape[0] - half_k):
for x in range(half_k, img_a.shape[1] - half_k):
# init variables for window calc goes here
for j in range(y - half_k, y + half_k + 1):
for i in range(x - half_k, x + half_k + 1):
# stuff init-ed above gets added to here
# final calc on things calculated in windows goes here
U[y][x] = one_of_the_window_calculations
V[y][x] = the_other_one
return U, V
I think you can create an array of the indices of the patches with a function like this get_patch_idx
in the first place我认为您可以首先创建一个带有 function 的补丁索引数组,就像这个
get_patch_idx
def get_patch_idx(ind,array_shape,step):
row_nums,col_nums = array_shape
col_idx = ind-(ind//col_nums)*col_nums if ind%col_nums !=0 else col_nums
row_idx = ind//col_nums if ind%col_nums !=0 else ind//col_nums
if col_idx+step==col_nums or row_idx+step==row_nums or col_idx-step==-1 or row_idx-step==-1: raise ValueError
upper = [(row_idx-1)*col_nums+col_idx-1,(row_idx-1)*col_nums+col_idx,(row_idx-1)*col_nums+col_idx+1]
middle = [row_idx*col_nums+col_idx-1,row_idx*col_nums+col_idx,row_idx*col_nums+col_idx+1]
lower = [(row_idx+1)*col_nums+col_idx-1,(row_idx+1)*col_nums+col_idx,(row_idx+1)*col_nums+col_idx+1]
return [upper,middle,lower]
Assume you have an (10,8)
array, and half_k
is 1假设您有一个
(10,8)
数组,并且half_k
为 1
test = np.linspace(1,80,80).reshape(10,8)*2
mask = np.linspace(0,79,80).reshape(10,8)[1:-1,1:-1].ravel().astype(np.int)
in which the indices in mask
are allowed, then you can create an array of indices of the patches其中
mask
中的索引是允许的,那么您可以创建一个补丁索引数组
patches_inds = np.array([get_patch_idx(ind,test.shape,1) for ind in mask])
with this patches_inds
, patches of the original array test
can be sliced with np.take
有了这个
patches_inds
,原始数组test
的补丁可以用np.take
切片
patches = np.take(test,patches_inds)
This will bypass for loop efficiently.这将有效地绕过 for 循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.