-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_background.m
36 lines (30 loc) · 960 Bytes
/
generate_background.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
34
35
36
%{
Given an image of size R x C, and target height H, target width W.
blur is the Gaussian blur sigma.
delta specifies an amount to crop from the top and bottom of the new image.
Upscale the image by a factor of H/R.
Apply blur.
Crop the center of the image to size H x W.
Note that H is used for the scaling, and W is used for the cropping.
%}
function img = generate_background(H, W, image, blur, delta)
R = size(image, 1);
C = size(image, 2);
if H > W
% Increase R to H + delta * 2.
ratio = (H + delta * 2) / R;
else
% Increase C to W.
ratio = W / C;
end
% Upscale the image.
img = imresize(image, ratio);
% Apply blur.
if blur > 0
img = imgaussfilt(img, blur);
end
% Crop the center of the image to size H x W.
left = size(img, 2) / 2 - W / 2;
top = delta + size(img, 1) / 2 - H / 2;
img = imcrop(img, [left, top, W, H]);
end