更快地逐像素循环以计算图像中的熵的方法 [英] Faster way to looping pixel by pixel to calculate entropy in an image

查看:249
本文介绍了更快地逐像素循环以计算图像中的熵的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在通过逐像素卷积运算来计算具有像素的图像的熵,并且它一直在工作,但是速度非常慢,随着内核大小的增加,执行时间也越来越长.

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.

这是我的函数代码,在代码中,我首先用gdal读取图像并将其转换为数组以将其传递给函数.

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.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时. 我该如何改善呢?

The problem is when the number of kernel is >7. How can I improve it?

推荐答案

类似于matlab,加快此类操作速度的关键称为向量化".基本上,删除for循环并将您的计算转换为矢量和矩阵运算-对于每个步骤,找到一种对所有合格像素进行分组并使用一次调用对其进行运算的方法.

Similar to matlab, the key to speed up operations like this is called "vectorization". basically, removing for-loop and converting your calculations into vector and matrix operations - for each of the step, find a way to group all qualified pixels and operate on them using one call.

详细阅读

https://www.geeksforgeeks.org/vectorization-in-python/

许多方法都类似于matlab中的矢量化

many of the approaches are similar to vectorization in matlab

https://www.mathworks.com/help/matlab/matlab_prog /vectorization.html https://blogs.mathworks.com/videos /2014/06/04/vectorizing-code-in-matlab/

这篇关于更快地逐像素循环以计算图像中的熵的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆