Padding an image in MATLAB

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.


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;