在MATLAB中可以很方便的使用padarray函数进行图像的填充。
padarray函数可以有两个至四个参数,以下是官方文档介绍
B = padarray(A,PADSIZE) pads array A with PADSIZE(k) number of zeros
along the k-th dimension of A. PADSIZE should be a vector of
nonnegative integers.
B = padarray(A,PADSIZE,PADVAL) pads array A with PADVAL (a scalar)
instead of with zeros.
B = padarray(A,PADSIZE,PADVAL,DIRECTION) pads A in the direction
specified by the string DIRECTION. DIRECTION can be one of the
following strings.
String values for DIRECTION
'pre' Pads before the first array element along each
dimension .
'post' Pads after the last array element along each
dimension.
'both' Pads before the first array element and after the
last array element along each dimension.
By default, DIRECTION is 'both'.
B = padarray(A,PADSIZE,METHOD,DIRECTION) pads array A using the
specified METHOD. METHOD can be one of these strings:
String values for METHOD
'circular' Pads with circular repetition of elements.
'replicate' Repeats border elements of A.
'symmetric' Pads array with mirror reflections of itself.
padsize给出了给出了填充的行数和列数,通常用[r c]来表示。
padval表示填充方法。它的具体值和描述如下:
padval: ‘symmetric’表示图像大小通过围绕边界进行镜像反射来扩展;
‘replicate’表示图像大小通过复制外边界中的值来扩展;
‘circular’图像大小通过将图像看成是一个二维周期函数的一个周期来进行扩展。
direction表示填充的方向。它的具体值和描述如下:
direction : ‘pre’表示在每一维的第一个元素前填充;
‘post’表示在每一维的最后一个元素后填充;
‘both’表示在每一维的第一个元素前和最后一个元素后填充,此项为默认值。
下面举个栗子:
>>f=[1 2;3 4];
>> fp=padarray(f,[3 2],'replicate','post');
>> fp
结果为
fp =
1 2 2 2
3 4 4 4
3 4 4 4
3 4 4 4
3 4 4 4
一.更改direction
1.把’post’换成’pre’
>>f=[1 2;3 4];
>> fp=padarray(f,[3 2],'replicate','pre');
>> fp
结果为
fp =
1 1 1 2
1 1 1 2
1 1 1 2
1 1 1 2
3 3 3 4
2.再换成’both’
fp =
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
二,更改padval
1.把’replicate’改为’symmetric’
结果为
fp =
1 2 2 1
3 4 4 3
3 4 4 3
1 2 2 1
1 2 2 1
2.改为’circular’
结果为
fp =
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
1 2 1 2