Skip to content

Instantly share code, notes, and snippets.

@Saafke
Created March 28, 2020 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Saafke/9a07fa30743c280ffd930c4e759e3c61 to your computer and use it in GitHub Desktop.
Save Saafke/9a07fa30743c280ffd930c4e759e3c61 to your computer and use it in GitHub Desktop.
Matlab code for padding an image.
% Code created by Xavier Weber
function outimg = padImage(I, pad)
%padImage Pads an image with black pixels (zero-values)
% Works with single or multi-channels
% Get input dimensions
img_size = size(I);
src_height = img_size(1);
src_width = img_size(2);
src_channels = 1;
if length(img_size) == 3
src_channels = img_size(3);
end
% round padding up
pad = ceil(pad);
% create destination image, single or multi-channel
outimg = zeros(src_height+2*pad, src_width+2*pad, src_channels);
for channel = 1:src_channels
for row = 1+pad:src_height+pad
for col = 1+pad:src_width+pad
outimg(row,col,channel) = I(row-pad,col-pad,channel);
end
end
outimg = uint8(outimg);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment