我的目标是实现一个中值过滤器,这是一个函数,它可以用周围像素的中值来替换二维数组中的每个像素。它可以用来对图像进行去噪。
我的实现是从原始矩阵中提取子矩阵,其中包含像素本身和它的邻居。这个提取目前是通过for-loop来完成的,你可以想象for-loop占用了大约95%的执行时间。
这是我目前的实现。
def median_blur(img, fsize=3):
img_intermediate = np.zeros((img.shape[0] + 2, img.shape[1] + 2), np.uint8) # First intermediate img, used for padding original image
img_intermediate[1:img.shape[0]+1, 1:img.shape[1]+1] = img
img_result = np.empty((*img.shape, fsize, fsize), np.uint8) # Will contain result, first receives kernel-submatrices
# Extract submatrices from image
for i in range(img.shape[0]):
for j in range(img.shape[1]):
img_result[i, j] = img_intermediate[i:i+fsize, j:j+fsize]
img_result = img_result.reshape(*img.shape, fsize**2) # Reshape Result-Matrix
img_result = np.median(img_result, axis=2) # Calculate median in one go
return img_result.astype('uint8')
我如何使用向量化操作来提取这些子矩阵?
作为奖励,如果有计算机视觉方面的经验的人在看这篇文章。有没有更好的方法来实现中值滤波 除了应用在中间零填充矩阵上?
非常感谢您。
这是一个矢量化的解决方案。然而,你可以通过注意图像数组的内存顺序来提出一个更快的解决方案。
from numpy.lib.stride_tricks import as_strided
img_padded = np.pad(img, 1, mode='constant')
sub_shape = (fsize, fsize)
view_shape = tuple(np.subtract(img_padded.shape, sub_shape) + 1) + sub_shape
sub_img = as_strided(img_padded, view_shape, img_padded.strides * 2)
sub_img = sub_img.reshape(img.shape + (fsize**2,))
result = np.median(sub_img, axis=2).astype(int)
使用... pad
功能,先垫上,然后 as_strided
以获得子矩阵,最后应用 median
到你的步伐。
更新:使用 view_as_windows
由 @Divakar 在评论中建议。
from skimage.util.shape import view_as_windows
img_padded = np.pad(img, 1, mode='constant')
sub_shape = (fsize, fsize)
view_shape = view_as_windows(img_padded, sub_shape, 1)
sub_img = view_shape.reshape(img.shape + (fsize**2,))
result = np.median(sub_img, axis=2).astype(int)
view_as_windows
还提供了类似于strides的子矩阵。
样的图像和输出。
img:
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]
median_filtered:
[[ 0 2 3 4 5 0]
[ 2 8 9 10 11 6]
[ 8 14 15 16 17 12]
[ 0 14 15 16 17 0]]