annotate toolboxes/FullBNT-1.0.7/KPMtools/imresizeAspect.m @ 0:cc4b1211e677 tip

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