Convolve2d只是使用Numpy

All*_*ric 15 python numpy image-processing matrix convolution

我正在使用Numpy研究图像处理,并面临使用卷积过滤的问题.

我想卷一个灰度图像.(使用较小的2d阵列卷积2d阵列)

有没有人有想法改进我的方法?

我知道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:]]])
Run Code Online (Sandbox Code Playgroud)

子矩阵似乎很复杂,但我正在做的事情如下图所示.

子矩阵

接下来,我将每个子矩阵乘以一个过滤器.

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

multiplied_subs

并总结了他们.

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

因此这种行为可以称为我的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)
Run Code Online (Sandbox Code Playgroud)

但是,我发现这个my_convolve2d有三个原因很麻烦.

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

感谢您阅读此内容.

更新的种类.我为自己写了一个conv3d.我将此作为公共领域.

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
Run Code Online (Sandbox Code Playgroud)

Cri*_*pin 12

您可以使用as_strided [1]生成子数组:

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)
Run Code Online (Sandbox Code Playgroud)

要摆脱你的第二个"丑陋"的总和,改变你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]]
Run Code Online (Sandbox Code Playgroud)


Dan*_*rez 8

您还可以使用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
Run Code Online (Sandbox Code Playgroud)

欢呼,丹


Dan*_*l F 5

从上面使用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)
Run Code Online (Sandbox Code Playgroud)