Main Content

imfilter 边界填充选项

计算图像边界的输出像素时,卷积或相关性核的一部分通常不在图像边缘上,如下图所示。

当核的值落在图像之外时

A grid of pixels displaying the pixel values. The 3-by-3 kernel centered on the (1, 4) pixel is highlighted in gray and extends past the edge of the image, where there are no pixels.

imfilter 函数通常通过假设图像边缘之外的像素为 0 来填充这些像素。这称为零填充,如下图所示。

外部像素的零填充

A grid of pixels displaying the pixel values. Pixels are simulated where the 3-by-3 kernel extends past the edge of the image. The value of the simulated pixels is 0.

对图像进行滤波时,零填充会导致图像边缘周围出现深色波段,如以下示例所示。

I = imread("eight.tif");
h = ones(5,5) / 25;
I2 = imfilter(I,h);
imshow(I)
title("Original Image");
figure
imshow(I2)
title("Filtered Image with Black Border")

On the left is the original image and on the right is the filtered image with dark pixels along the edge of the image.

为了消除图像边缘周围零填充所带来的伪影,imfilter 提供一种称为边界复制的替代边界填充方法。在边界复制中,图像外部任何像素的值都是通过复制最近边界像素的值来确定的。如下图所示。

复制的边界像素

A grid of pixels displaying the pixel values. Pixels are simulated where the 3-by-3 kernel extends past the edge of the image. The value of each simulated pixel is equal to the value of the edge pixel adjacent to the simulated pixel.

要使用边界复制进行滤波,请将附加可选参量 "replicate" 传递给 imfilter

I3 = imfilter(I,h,"replicate");
figure
imshow(I3); 
title("Filtered Image with Border Replication")

The filtered image with border replication does not have dark pixels along the edge of the image.

imfilter 函数支持 "circular""symmetric" 等其他边界填充选项。

另请参阅

相关主题