【发布时间】:2020-04-04 19:53:56
【问题描述】:
我一直在用逐像素卷积运算计算图像的熵,它一直在工作,但速度很慢,随着内核大小的增加执行时间增加。
这是我的函数代码,首先在代码中我使用 gdal 读取图像并将其转换为数组以将其传递给函数。
@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.0
lenVet=mask.size
horList=list(set(mask))
if len(horList)==1 and horList.count(0)==1:
ArrayES[row,col]=0.0
else:
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]=He
N=len(horList)*1.0
if N == 1:
C=0
else:
Hmax=np.log2(N)
C=He/Hmax
if option==1:
ArrayES[row,col]=C
if option==2:
SDL=(1-C)*C
ArrayES[row,col]=SDL
if option==3:
D = 0.0
for p in prob:
D += (p-(1/N))**2
LMC=D*C
ArrayES[row,col]=LMC
return ArrayES
问题是当内核数>7时。 我该如何改进它?
【问题讨论】:
-
目前还不清楚您要在这里做什么,这可能应该在“算法”标签中。
spicy有一个可能有用的图像卷积函数 docs.scipy.org/doc/scipy-0.14.0/reference/generated/…
标签: python image performance numpy entropy