-
Notifications
You must be signed in to change notification settings - Fork 11
/
pad_image_symmetric.m
33 lines (26 loc) · 1.35 KB
/
pad_image_symmetric.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function img_padded = pad_image_symmetric(img, padding)
%PAD_IMAGE_SYMMETRIC Pad img by padding semmetrically filling in new pixels
% constant padding
if all(size(padding) == 1)
padding = [padding padding padding padding];
end
% expand padding
pad_top = padding(1);
pad_right = padding(1);
pad_bottom = padding(3);
pad_left = padding(4);
% get current dimensions
[height, width, channels] = size(img);
img_padded = zeros(height + pad_top + pad_bottom, width + pad_left + pad_right, channels);
img_padded(pad_top + 1:pad_top + height, pad_left + 1:pad_left + width, :) = img;
% fill in sides
img_padded(1:pad_top, pad_left + 1:pad_left + width, :) = img(pad_top:-1:1, :, :);
img_padded(pad_top + height:end, pad_left + 1:pad_left + width, :) = img(end:-1:end - pad_bottom, :, :);
img_padded(pad_top + 1:pad_top + height, 1:pad_left, :) = img(:, pad_left:-1:1, :);
img_padded(pad_top + 1:pad_top + height, pad_left + width:end, :) = img(:, end:-1:end - pad_right, :);
% fill in corners
img_padded(1:pad_top, 1:pad_left, :) = img(pad_top:-1:1, pad_left:-1:1, :);
img_padded(1:pad_top, pad_left + width:end, :) = img(pad_top:-1:1, end:-1:end - pad_right, :);
img_padded(pad_top + height:end, 1:pad_left, :) = img(end:-1:end - pad_bottom, pad_left:-1:1, :);
img_padded(pad_top + height:end, pad_left + width:end, :) = img(end:-1:end - pad_bottom, end:-1:end - pad_right, :);
end