Padding an image in MATLAB

流过昼夜 提交于 2019-12-01 16:42:58

You can divide your padarray instruction in two calls:

K_pad = padarray(K, [floor((p-m)/2) floor((q-n)/2)], 'replicate','post');
K_pad = padarray(K_pad, [ceil((p-m)/2) ceil((q-n)/2)], 'replicate','pre');

But you may want to check what is happening in the corners of the image to see if it is ok with what you want to do with it.

abcd

Here's another way of padding it without using padarray.

imgSize=size(img); %#img is your image matrix
finalSize=392;   
padImg=zeros(finalSize);

padImg(finalSize/2+(1:imgSize(1))-floor(imgSize(1)/2),...
    finalSize/2+(1:imgSize(2))-floor(imgSize(2)/2))=img;

You can try this function:

function out1 = myresize(in1)
%% Sa1habibi@gmail.com
%% resize an image to closest power of 2

[m,n] = size(in1);

if(rem(m,2)~=0)
    in1(1,:)=[];
end

if(rem(n,2)~=0)
    in1(:,1)=[];
end

[m,n] = size(in1);

a = max(m,n);

if(log2(a)~=nextpow2(a) || m~=n)

    s1 = 2^nextpow2(a);
    n_row = (s1 - m)/2;
    n_col = (s1 - n)/2;

    dimension = [n_row,n_col];

    out1 = padarray(in1,dimension);

end
end

for example:

A = ones(2,8);
out1 = myresize(A);

first it finds the maximum of rows and columns, then paddarray the matrix in both direction.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!