Padding an image in MATLAB

Sista picture Sista · Jul 6, 2011 · Viewed 24.4k times · Source

I have an image of size 61x56 and I want to pad the image to size 392x392.

I am trying to use padarray but since I get a non-integer value I am unable to do this. Can anyone help me with this. Thanks a lot! I have attached what I want to do below.

K = imread('test.jpg');
K = rgb2gray(K);
[m n] = size(K);
p = 392;
q = 392;
K_pad = padarray(K, [(p-m)/2 (q-n)/2], 'replicate');

Answer

Aabaz picture Aabaz · Jul 6, 2011

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.