I have been calculating the entropy of an image with a pixel by pixel convolution operation, and it has been working but very slowly, increasing the execution time with the kernel size.
Here is my function code, where first in the code I read an image with gdal and transform it into an array to pass it to the function.
@jit
def convolution (ArrayES, ImArray, rows, cols, kernel, option):for row in prange(rows):for col in prange(cols):Lx=max(0,col-kernel+1)Ux=min(cols,col+kernel+1)Ly=max(0,row-kernel+1)Uy=min(rows,row+kernel+1)mask=ImArray[Ly:Uy,Lx:Ux].flatten()He=0.0lenVet=mask.sizehorList=list(set(mask))if len(horList)==1 and horList.count(0)==1:ArrayES[row,col]=0.0else:T7=time.time()prob=[(mask[mask==i]).size/(lenVet*1.0) for i in horList]for p in prob:if p>0:He += -1.0*p*np.log2(p)if option==0:ArrayES[row,col]=HeN=len(horList)*1.0if N == 1:C=0else:Hmax=np.log2(N)C=He/Hmaxif option==1:ArrayES[row,col]=Cif option==2:SDL=(1-C)*CArrayES[row,col]=SDLif option==3:D = 0.0for p in prob:D += (p-(1/N))**2LMC=D*CArrayES[row,col]=LMCreturn ArrayES
The problem is when the number of kernel is >7. How can I improve it?