如何将图像划分为块并为每个块添加零边框



我有一个问题,我需要将图像划分为块并为每个块添加零边框,一个或两个零就足够了,我为所有图像添加了边框,但我想为每个图像块添加边框。

注意::: 块

数 任何大小,例如:4X4 块

img=round(100*rand(4,4));
[n,m]=size(img);
x=zeros(n+2,m+2);
%%%%%Applying zero padding to the image
for i=1:n+2
    for j=1:m+2
        if i==1 || i==n+2 || j==1 || j==m+2
            x(i,j)=0;
        else
            x(i,j)=img(i-1,j-1);
        end
    end
end
x

我需要在每个块周围获得这样的输出 一两个零

 0     0     0     0    0    0     0   0  
 0    84    80     0    0   65    85   0
 0    29    19     0    0   23    77   0
 0     0     0     0    0    0     0   0
 0     0     0     0    0    0     0   0
 0    84    80     0    0   66    74   0
 0    29    19     0    0   36    80   0
 0     0     0     0    0    0     0   0
%n: Size of original matrix
n=size(img,1)
%prealloc new matrix
img2=zeros(n+3,n+3)
%p indicates indices to store img at
p=[(2:n/2+1),(n/2+3:n+2)]
%Copy img to the correct positions
img2(p,p)=img

最新更新