2

I am trying to create a slide search algorithm in python. I am trying to index the image by iterating over the co-ordinates. Please check the code below. Here I am using 2 FOR LOOPS (which takes lot of time). I want to implement it which will compute very fast. Any better vectorization method for FASTER implementation ?

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) 
Milo Lu
  • 3,176
  • 3
  • 35
  • 46
  • Possible duplicate of [Using numpy \`as\_strided\` function to create patches, tiles, rolling or sliding windows of arbitrary dimension](https://stackoverflow.com/questions/45960192/using-numpy-as-strided-function-to-create-patches-tiles-rolling-or-sliding-w) – Daniel F Oct 10 '18 at 08:39

1 Answers1

2

Use window_nd from here, and just do

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

This uses np.stride_tricks.as_strided to create the windows as views into the original image.

Alternately, you can use skimage.util.shape.view_as_blocks

skimage.util.shape.view_as_blocks(image, (64, 64, 3))
Daniel F
  • 13,620
  • 2
  • 29
  • 55