有没有一种类似于NumPy的方法可以让我的代码运行更快?

huangapple go评论53阅读模式
英文:

Is there any numpy-like way to make my code faster?

问题

以下是您要翻译的内容:

我正在编写一段部分归一化输入图像强度的代码由于我找不到NumPy库的相应函数我创建了以下代码

```python
def arealNorm(img, kernel_size=7): #图像应为灰度图
    img_norm = np.zeros(img.shape)
    for v in range(kernel_size, img.shape[0] - kernel_size):
        for u in range(kernel_size, img.shape[1] - kernel_size):
            MaxV = np.max(img[v - kernel_size:v + kernel_size, u - kernel_size:u + kernel_size])
            minV = np.min(img[v - kernel_size:v + kernel_size, u - kernel_size:u + kernel_size])
            newval = (img[v, u] - minV) / (MaxV - minV)
            img_norm[v, u] = newval
    return img_norm

由于它需要访问图像的每个像素,这段代码运行非常慢。

是否有一种类似于NumPy的方法可以使我的代码运行更快?


<details>
<summary>英文:</summary>

I am making a code that partially normalizes intensity of an input image. Since I could not find corresponding function of Numpy library, I made a code like following: 

def arealNorm(img,kernel_size = 7): #Img should me gray
img_norm = np.zeros(img.shape)
for v in range(kernel_size,img.shape[0]-kernel_size):
for u in range(kernel_size,img.shape[1]-kernel_size):
MaxV = np.max(img[v-kernel_size:v+kernel_size,u-kernel_size:u+kernel_size])
minV = np.min(img[v-kernel_size:v+kernel_size,u-kernel_size:u+kernel_size])
newval = (img[v,u]-minV)/(MaxV-minV)
img_norm[v,u] = newval
return img_norm


Since it should access every pixel of the image this code works very slowly. 

Is there any numpy-like way to convert my code to work faster ? 


</details>


# 答案1
**得分**: 1

使用内置函数scipy.ndimage.filters.generic_filter来加速这段代码的一种方法。该函数将用户定义的函数应用于输入图像中的每个邻域。

以下是使用generic_filter的示例实现:

```python
from scipy.ndimage.filters import generic_filter

def arealNorm(img, kernel_size=7):
  def local_norm(patch):
    max_val = np.max(patch)
    min_val = np.min(patch)
    return (img - min_val) / (max_val - min_val)

  img_norm = generic_filter(img, local_norm, size=kernel_size)
  return img_norm
英文:

One approach to speed up this code is to use the built-in function scipy.ndimage.filters.generic_filter. This function applies a user-defined function to each neighborhood in the input image.

Here is an example implementation using generic_filter:

from scipy.ndimage.filters import generic_filter

def arealNorm(img, kernel_size=7):
  def local_norm(patch):
    max_val = np.max(patch)
    min_val = np.min(patch)
    return (img - min_val) / (max_val - min_val)

  img_norm = generic_filter(img, local_norm, size=kernel_size)
  return img_norm

答案2

得分: 1

感谢 @Michael Butscher,我将我的代码最终确定为:

def arealNorm(img, kernel_size=7):  # Img should me gray
    # img_norm = np.zeros(img.shape)
    img_max = ndi.filters.maximum_filter(img, size=kernel_size)
    img_min = ndi.filters.minimum_filter(img, size=kernel_size)
    img_norm = (img - img_min) / (img_max - img_min)

    return img_norm
英文:

Thanks to @Michael Butscher , I finalized my code as :

def arealNorm(img,kernel_size = 7): #Img should me gray
    #img_norm = np.zeros(img.shape)
    img_max = ndi.filters.maximum_filter(img,size = kernel_size)
    img_min = ndi.filters.minimum_filter(img,size = kernel_size)
    img_norm = (img - img_min)/(img_max-img_min)

    return img_norm

huangapple
  • 本文由 发表于 2023年2月16日 12:14:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定