特定点处的图像卷积

6

在Scipy(或其他类似库)中,是否有一种方法可以仅在某些所需点处使用给定卷积核对图像进行卷积?

我正在寻找类似于以下内容的东西:

ndimage.convolve(image, kernel, mask=mask)

当核需要应用时,mask包含True(或1),否则为False(或0)。

编辑:以下是示例Python代码,可以实现我想要做的事情(但不比使用Scipy进行整个图像卷积更快):

def kernel_responses(im, kernel, mask=None, flatten=True):
    if mask is None:
        mask = np.ones(im.shape[:2], dtype=np.bool)

    ks = kernel.shape[0]//2

    data = np.pad(im, ks, mode='reflect')
    y, x = np.where(mask)

    responses = np.empty(y.shape[0], float)

    for k, (i, j) in enumerate(zip(y, x)):
        responses[k] = (data[i:i+ks*2+1, j:j+ks*2+1] * kernel).sum()

    if flatten:
        return responses

    result = np.zeros(im.shape[:2], dtype=float)
    result[y, x] = responses
    return result

上述代码使用wrap边界条件完成了任务,但内部循环是用Python实现的,因此速度较慢。我想知道是否已经在scipy/opencv/skimage中实现了更快的方法。


@tom10 不是说要忽略评估中的点,而是在某些点上忽略评估。我不想卷积整个图像,我只想在给定的(比如说20个)点处得到内核响应。我希望通过仅“计算给定点处的内核响应”来加速传统的“卷积图像并获取点”的方法。 - Imanol Luengo
3个回答

5
我知道我在回答自己的问题,希望下面的代码可以做出更进一步的改进,或者对其他用户有用。
下面的代码是一个Cython/Python实现:
PYTHON:
def py_convolve(im, kernel, points):
    ks = kernel.shape[0]//2
    data = np.pad(im, ks, mode='constant', constant_values=0)
    return cy_convolve(data, kernel, points)

Cython:

import numpy as np
cimport cython

@cython.boundscheck(False)
def cy_convolve(unsigned char[:, ::1] im, double[:, ::1] kernel, Py_ssize_t[:, ::1] points):
    cdef Py_ssize_t i, j, y, x, n, ks = kernel.shape[0]
    cdef Py_ssize_t npoints = points.shape[0]
    cdef double[::1] responses = np.zeros(npoints, dtype='f8')

    for n in range(npoints):
        y = points[n, 0]
        x = points[n, 1]
        for i in range(ks):
            for j in range(ks):
                responses[n] += im[y+i, x+j] * kernel[i, j]

     return np.asarray(responses)

与其他方法的比较

下面的表格展示了4种方法的评估:

  1. 我在问题中使用的Python方法
  2. @Vighnesh Birodkar的方法
  3. 使用scipy进行完整的图像卷积
  4. 我在这篇文章中使用的Python / Cython实现

每行分别对应于3个不同图像(coins, cameralena,分别来自skimage.data)的那些方法,并且每列对应于需要计算核响应的不同点数(以百分比表示,意味着“在图像的%的点中计算响应”)。

对于在少于50%的点中计算核响应,我的实现比卷积整个图像更快,但在其它情况下则不会更快。

编辑: 测试的核窗口是5x5的均匀窗口(np.ones((5,5)))。

['303x384']    1%     2%     5%    10%     20%     50%
1            4.97   9.58  24.32  48.28  100.39  245.77
2            7.60  15.09  37.42  75.17  150.09  375.60
3            3.05   2.99   3.04   2.88    2.96    2.98
4            0.17   0.22   0.38   0.60    1.10    2.49

['512x512']     1%     2%     5%     10%     20%     50%
1            10.68  21.87  55.47  109.16  223.58  543.73
2            17.90  34.59  86.02  171.20  345.46  858.24
3             6.52   6.53   6.74    6.63    6.43    6.60
4             0.31   0.43   0.78    1.34    2.73    6.82

['512x512']     1%     2%     5%     10%     20%     50%
1            13.21  21.45  54.98  110.80  217.11  554.96
2            19.55  34.78  87.09  172.33  344.58  893.02
3             6.87   6.82   7.00    6.60    6.64    7.71
4             0.35   0.47   0.87    1.57    2.47    6.07

注意:时间单位为毫秒


对于您的个人资料表,您应该添加内核形状(因为不同的内核大小显然会改变您的结果)。另外,一个拼写错误建议,您在西班牙语中写成了百分比:“(is in percentajes as meaning...” - Scott

3
我不知道有任何函数可以完全满足您的要求。如果您提供一个点列表而不是提供要卷积的掩模,例如:[(7, 7), (100, 100)],那么它可能就像获取适当的图像块(与您提供的内核大小相同),卷积图像块和内核,并插入回原始图像一样简单。
下面是一个编码示例,希望它足够接近,以便您可以轻松修改:

[编辑:我注意到我的填充和图像块算法中有一些错误。以前,您无法卷积边界上的点(例如(0,0)),我加倍了填充,修复了一些算法,现在一切都好了。]

import cv2
import numpy as np
from scipy import ndimage
from matplotlib import pyplot as plt

def image_convolve_mask(image, list_points, kernel):
# list_points ex. [(7, 7), (100, 100)]
# assuming kernels of dims 2n+1 x 2n+1
rows, cols = image.shape
k_rows, k_cols = kernel.shape
r_pad = int(k_rows/2)
c_pad = int(k_cols/2)
# zero-pad the image in case desired point is close to border
padded_image = np.zeros((rows + 2*k_rows, cols + 2*k_cols))
# set the original image in the center
padded_image[k_rows: rows + k_rows, k_cols: cols + k_cols] = image
# should you prefer to use np.pad:
# padded_image = np.pad(image, (k_rows, k_cols), 'constant', constant_values=(0, 0))

for p in list_points:
    # extract pertinent patch from image
    # arbitrarily choosing the patch as same size as the kernel; change as needed
    patch = padded_image[p[0] + k_rows - r_pad: p[0] + 2*k_rows - r_pad, p[1] + k_cols - c_pad: p[1] + 2*k_cols - c_pad]

    # here use whatever function for convolution; I prefer cv2filter2D()
    # commented out is another option
    # conv = ndimage.convolve(patch, kernel, mode='constant', cval=0.0)
    conv = cv2.filter2D(patch, -1, kernel)
    # set the convolved patch back in to the image
    padded_image[p[0] + k_rows - r_pad: p[0] + 2*k_rows - r_pad, p[1] + k_cols - c_pad: p[1] + 2*k_cols - c_pad] = conv

return padded_image[k_rows: rows + k_rows, k_cols: cols + k_cols]

现在我们来尝试在一张图片上应用它:
penguins = cv2.imread('penguins.png', 0)
kernel = np.ones((5,5),np.float32)/25
# kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], np.float32)
conv_image = image_convolve_mask(penguins, [(7, 7), (36, 192), (48, 207)], kernel)
plt.imshow(conv_image, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()

我应用了一个5x5的平滑处理器,不能在像素(7, 7)周围看到任何变化,但我选择了另外两个点作为最左边两只企鹅喙的顶端。所以你可以看到平滑的区域。 enter image description here enter image description here 这是一张lena512图像,有21个卷积点(时间:0.006177秒)。 enter image description here [编辑2:使用掩码生成行、列元组列表并馈入函数的示例。]
mask = np.eye(512)
k = np.ones((25, 25), np.float32)/625
list_mask = zip(np.where(mask==1)[0], np.where(mask==1)[1])
tic = time.time()
conv_image = image_convolve_mask(lena, list_mask, k)
print 'time: ', time.time()-tic # 0.08136 sec

enter image description here


谢谢!这已经非常接近我所需要的了(+1),但我仍然需要检查它的性能。使用Python循环可能比使用Scipy或OpenCV对整个图像进行卷积要慢。 - Imanol Luengo
非常感谢。然而,这种方法比使用Scipy(它是Fortran / C ++)对整个图像进行卷积要慢。我已经添加了一个带有一些分析的答案。 - Imanol Luengo
由于没有其他的实现,而你的帮助让我完成了Cython版本,所以我只能给你答案 :P 感谢你的帮助! - Imanol Luengo
1
请注意np.pad函数。 - Stefan van der Walt
@StefanvanderWalt 有时候你只需要自己尝试一下算术运算。我会使用np.pad插入一行注释。 - Scott
显示剩余2条评论

0
您可以使用以下代码片段。如果您的掩码足够密集,它可能不会那么低效。
def mask_conv(img, kernel, mask):
    out = filters.convolve(img, kernel)
    return np.where(mask, out, img)

一些示例代码

from skimage import data, draw, io, color
from scipy.ndimage import filters
import numpy as np

def mask_conv(img, kernel, mask):
    out = filters.convolve(img, kernel)
    return np.where(mask, out, img)

img = data.camera()
mask = np.zeros_like(img, dtype=np.bool)

kernel = np.ones((9,9))/100
circle = draw.circle(300, 350, 100)
mask[circle] = True

out = mask_conv(img, kernel, mask)

io.imshow(out)
io.show()

Convolution with mask


这需要对整个图像进行卷积,而这正是我想避免的。我的“掩模”是图像周围的一些稀疏像素,在其中我想评估核函数。 - Imanol Luengo
我编辑了答案,并附上了一个示例Python代码,展示我所寻找的内容。 - Imanol Luengo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接