Created
March 28, 2020 15:13
-
-
Save Saafke/9a07fa30743c280ffd930c4e759e3c61 to your computer and use it in GitHub Desktop.
Matlab code for padding an image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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