Convolve2d 仅通过使用 Numpy

Convolve2d just by using Numpy

我正在研究使用 Numpy 进行图像处理,并且遇到了卷积滤波的问题。

我想对灰度图像进行卷积。 (将一个二维数组与一个较小的二维数组进行卷积)

有没有人想完善我的方法?

我知道 scipy 支持 convolve2d,但我只想使用 Numpy 制作一个 convolve2d。

我做了什么

首先,我制作了一个二维数组的子矩阵。

a = np.arange(25).reshape(5,5) # original matrix

submatrices = np.array([
     [a[:-2,:-2], a[:-2,1:-1], a[:-2,2:]],
     [a[1:-1,:-2], a[1:-1,1:-1], a[1:-1,2:]],
     [a[2:,:-2], a[2:,1:-1], a[2:,2:]]])

子矩阵看起来很复杂,但我所做的如下图所示。

接下来,我将每个子矩阵与一个过滤器相乘。

conv_filter = np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])
multiplied_subs = np.einsum('ij,ijkl->ijkl',conv_filter,submatrices)

并对它们求和。

np.sum(np.sum(multiplied_subs, axis = -3), axis = -3)
#array([[ 6,  7,  8],
#       [11, 12, 13],
#       [16, 17, 18]])

因此这个程序可以称为我的convolve2d。

def my_convolve2d(a, conv_filter):
    submatrices = np.array([
         [a[:-2,:-2], a[:-2,1:-1], a[:-2,2:]],
         [a[1:-1,:-2], a[1:-1,1:-1], a[1:-1,2:]],
         [a[2:,:-2], a[2:,1:-1], a[2:,2:]]])
    multiplied_subs = np.einsum('ij,ijkl->ijkl',conv_filter,submatrices)
    return np.sum(np.sum(multiplied_subs, axis = -3), axis = -3)

但是,我发现这 my_convolve2d 很麻烦,原因有 3 个。

  1. 子矩阵的生成太笨拙,难以阅读,只能在过滤器为 3*3 时使用
  2. 变子矩阵的大小似乎太大了,因为它比原始矩阵大约大 9 倍。
  3. 总和似乎有点不直观。简单的说,丑。

感谢您阅读到这里。

有点更新。我为自己写了一个conv3d。我会将其保留为 public 域。

def convolve3d(img, kernel):
    # calc the size of the array of submatracies
    sub_shape = tuple(np.subtract(img.shape, kernel.shape) + 1)

    # alias for the function
    strd = np.lib.stride_tricks.as_strided

    # make an array of submatracies
    submatrices = strd(img,kernel.shape + sub_shape,img.strides * 2)

    # sum the submatraces and kernel
    convolved_matrix = np.einsum('hij,hijklm->klm', kernel, submatrices)

    return convolved_matrix

您可以使用 as_strided:

生成子数组
import numpy as np

a = np.array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

sub_shape = (3,3)
view_shape = tuple(np.subtract(a.shape, sub_shape) + 1) + sub_shape
strides = a.strides + a.strides

sub_matrices = np.lib.stride_tricks.as_strided(a,view_shape,strides)

为了摆脱你的第二个“丑陋”的和,改变你的einsum,这样输出数组只有jk。这意味着你的第二次求和。

conv_filter = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
m = np.einsum('ij,ijkl->kl',conv_filter,sub_matrices)

# [[ 6  7  8]
#  [11 12 13]
#  [16 17 18]]

使用上面的 as_strided 和@Crispin 的 einsum 技巧进行了清理。将过滤器尺寸强制为扩展形状。如果索引兼容,甚至应该允许非正方形输入。

def conv2d(a, f):
    s = f.shape + tuple(np.subtract(a.shape, f.shape) + 1)
    strd = numpy.lib.stride_tricks.as_strided
    subM = strd(a, shape = s, strides = a.strides * 2)
    return np.einsum('ij,ijkl->kl', f, subM)

您还可以使用 fft(执行卷积的更快方法之一)

from numpy.fft import fft2, ifft2
import numpy as np

def fft_convolve2d(x,y):
    """ 2D convolution, using FFT"""
    fr = fft2(x)
    fr2 = fft2(np.flipud(np.fliplr(y)))
    m,n = fr.shape
    cc = np.real(ifft2(fr*fr2))
    cc = np.roll(cc, -m/2+1,axis=0)
    cc = np.roll(cc, -n/2+1,axis=1)
    return cc

干杯, 旦

https://laurentperrinet.github.io/sciblog/posts/2017-09-20-the-fastest-2d-convolution-in-the-world.html

在此处查看所有卷积方法及其各自的性能。 另外,我发现下面的代码片段更简单。

from numpy.fft  import fft2, ifft2
def np_fftconvolve(A, B):
    return np.real(ifft2(fft2(A)*fft2(B, s=A.shape)))