滑动搜索窗口,无需在 Python 中使用'for'循环



我正在尝试在python中创建幻灯片搜索算法。我正在尝试通过迭代坐标来索引图像。请检查下面的代码。在这里,我使用了 2 个 FOR 循环(这需要很多时间(。我想实现它将非常快速地计算。有更好的矢量化方法来实现更快的方法吗?

width,height,depth = img.shape
wind_size = 64
xx = np.arange(0,width,wind_size)
yy = np.arange(0,height,wind_size)
for i in xx[:-1]:
for j in yy[:-1]:
img_w = img[i:i+wind_size,j:j+wind_size,:]
img_w = cv2.cvtColor(img_w,cv2.COLOR_BGR2RGB)
img_lst1.append(img_w/255.0) 

从这里使用window_nd,然后做

window_nd(image, window = 64, steps = 64, axis = (0, 1))

这将使用np.stride_tricks.as_strided将窗口创建为原始图像的视图。

或者,您可以使用skimage.util.shape.view_as_blocks

skimage.util.shape.view_as_blocks(image, (64, 64, 3))

最新更新