wolffd@0
|
1 function img = imresizeAspect(img, maxSize)
|
wolffd@0
|
2 % function img = imresizeAspect(img, maxSize)
|
wolffd@0
|
3 % If image is larger than max size, reduce size, preserving aspect ratio of input.
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % If size(input) = [y x] and maxSize = [yy xx],
|
wolffd@0
|
6 % then size(output) is given by the following (where a=y/x)
|
wolffd@0
|
7 % if y<yy & x<xx then [y x]
|
wolffd@0
|
8 % else if a<1 then [a*xx xx]
|
wolffd@0
|
9 % else [yy yy/a]
|
wolffd@0
|
10
|
wolffd@0
|
11 % For why we use bilinear,
|
wolffd@0
|
12 % See http://nickyguides.digital-digest.com/bilinear-vs-bicubic.htm
|
wolffd@0
|
13
|
wolffd@0
|
14 if isempty(maxSize), return; end
|
wolffd@0
|
15
|
wolffd@0
|
16 [y x c] = size(img);
|
wolffd@0
|
17 a= y/x;
|
wolffd@0
|
18 yy = maxSize(1); xx = maxSize(2);
|
wolffd@0
|
19 if y <= yy & x <= xx
|
wolffd@0
|
20 % no-op
|
wolffd@0
|
21 else
|
wolffd@0
|
22 if a < 1
|
wolffd@0
|
23 img = imresize(img, ceil([a*xx xx]), 'bilinear');
|
wolffd@0
|
24 else
|
wolffd@0
|
25 img = imresize(img, ceil([yy yy/a]), 'bilinear');
|
wolffd@0
|
26 end
|
wolffd@0
|
27 fprintf('resizing from %dx%d to %dx%d\n', y, x, size(img,1), size(img,2));
|
wolffd@0
|
28 end
|
wolffd@0
|
29
|
wolffd@0
|
30
|
wolffd@0
|
31 %test
|
wolffd@0
|
32 if 0
|
wolffd@0
|
33 maxSize = [240 320];
|
wolffd@0
|
34 %img = imread('C:\Images\Wearables\web_static_office\8.jpg');
|
wolffd@0
|
35 %img = imread('C:\Images\Wearables\web_static_office\billandkirsten.jpg');
|
wolffd@0
|
36 %img = imread('C:\Images\Wearables\web_static_street_april\p5.jpg');
|
wolffd@0
|
37 img = imread('C:\Images\Wearables\Database_static_street\art11.jpg');
|
wolffd@0
|
38 img2 = imresizeAspect(img, maxSize);
|
wolffd@0
|
39 figure(1); clf; imshow(img)
|
wolffd@0
|
40 figure(2); clf; imshow(img2)
|
wolffd@0
|
41 fprintf('%dx%d (%5.3f) to %dx%d (%5.3f)\n', ...
|
wolffd@0
|
42 size(img,1), size(img,2), size(img,1)/size(img,2), ...
|
wolffd@0
|
43 size(img2,1), size(img2,2), size(img2,1)/size(img2,2));
|
wolffd@0
|
44 end
|