Convolve2d只是使用Numpy

问题描述 投票:10回答:3

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

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

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

我知道scipy支持convolve2d,但我想只使用Numpy进行convolve2d。

What I have done

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

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:]]])

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

submatrices

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

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

multiplied_subs

并总结了他们。

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有三个原因很麻烦。

  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
python numpy image-processing matrix convolution
3个回答
11
投票

您可以使用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)

要摆脱你的第二个“丑陋”的总和,改变你的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]]

5
投票

你也可以使用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

欢呼,丹


2
投票

使用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)
© www.soinside.com 2019 - 2024. All rights reserved.