I have an array
I which stores
N images of size
P (number of pixels). Every image is of size
P = q*q.
Now I want to delete patches of size
ps around a selected index
IDX (set all values to zero).
My approach was to reshape every single image using
reshape(q,q) and delete the pixels around
IDX. I also have to check if the index is not outside the image.
Here is an example:
My code is a real bottleneck and I would like to know if there is a way to improve the performance of my approach.
import numpy as np import matplotlib.pyplot as plt import time def myplot(I): imgs = 5 for i in range(imgs**2): plt.subplot(imgs,imgs,(i+1)) plt.imshow(I[i].reshape(q,q), cmap="viridis", interpolation="none") plt.axis("off") plt.show() N = 10000 q = 28 P = q*q I = np.ones((N,P)) myplot(I) ps = 5 IDX = np.random.randint(0,P,(N,1)) x0, y0 = np.unravel_index(IDX,(q,q)) t0 = time.time() # HOW TO IMPROVE THIS PART ? # for i in range(N): img = I[i].reshape(q,q) for x in range(ps): for y in range(ps): if (x0[i]+x < q) and (y0[i]+y < q): img[x0[i]+x,y0[i]+y] = 0.0 I[i] = img.reshape(1,q*q) print(time.time()-t0) myplot(I)
I call this code (without the plotting procedure) about one million times from another code. Every call takes about 1 second on my system. This makes the code so far quite useless.