four factors: dtype, ones/zeros,shape and order

more can be found in: http://note.shockjiang.vip/notebooks/numpyperf.ipynb

 

how to avoid for/for in numpy array processing, since loop-free processing might be 100x faster than loop one.

non-overlap: skimage.measure.block_reduce

overlap: change it to non-overlap.

e.g.., ksize=(2,2), overlap=(1,1)

1
2
3
4
5
6
7
8
9
10
ksize = self.blockheight - self.overlap
block1 = block_reduce(block, block_size=(ksize, ksize), func=np.max)
block2 = block1.repeat(2, axis=1)[:, 1:-1]
block22 = block2.repeat(2, axis=0)[1:-1] # interpolation to remove overlap
block3 = block_reduce(block22, block_size=(2, 2), func=np.max)
res = block3.repeat(offset, axis=1).repeat(offset, axis=0) #(2164, 2164)
h, w = res.shape
startx = h // 2 - height0 // 2
starty = w // 2 - width0 // 2
res = res[startx:startx+height0, starty:starty+width0]

 

e.g., ksize=(3,3), overlap=(2,2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
arr = np.arange(100).reshape((10, 10))
arr = arr.repeat(3, axis=1).repeat(3, axis=0)
arr[3] = arr[2]
arr[-4] = arr[-3]
arr[:,3] = arr[:,2]
arr[:,-4] = arr[:,-3]
arr = arr[3:-3, 3:-3]
M = len(arr)
sx = slice(2, M -1, 3)
sx2 = slice(3, M, 3)
sx3 = slice(4, M+1, 3)
N = len(arr[0])
sy = slice(2, N-1, 3)
sy2 = slice(3, N, 3)
sy3 = slice(4, N+1, 3)

e.g., ksize=(4,4), overlap=(3,3)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
arr = np.arange(64).reshape((8,8))
arr = arr.repeat(4, axis=1).repeat(4, axis=0)
arr[6] = arr[3]
arr[8] = arr[7]
arr[-7] = arr[-4]
arr[-9] = arr[-8]
arr[:,6] = arr[:,3]
arr[:,8] = arr[:,7]
arr[:,-7] = arr[:,-4]
arr[:,-9] = arr[:,-8]
arr = arr[6:-6, 6:-6]
 
arr[[2,4]] = arr[[4,2]]
arr[[-3,-5]] = arr[[-5,-3]]
arr[:,[2,4]] = arr[:,[4,2]]
arr[:,[-3,-5]] = arr[:,[-5,-3]]
print(arr)
M = len(arr)
sx = slice(3, M-8, 4)
sx2 = slice(8, M-3, 4)
sx3 = slice(6, M-5, 4)
N = len(arr[0])
sy = slice(3, N-8, 4)
sy2 = slice(8, N-3, 4)
sy3 = slice(6, N-5, 4)
arr[sx2] = arr[sx]
arr[sx] = arr[sx3]
arr[:,sy2] = arr[:,sy]
arr[:,sy] = arr[:,sy3]