You need to perform interpolation. There are many ways to do this. Use imresize
(e.g. imgOut=imresize(img,scale,method);
), or if you do not have the Image Processing Toolbox, consider the following code:
function imres = resizeim(I,outsize,interpalg)
if nargin<3 || isempty(interpalg),
interpalg='cubic';
end
rows=outsize(1);
cols=outsize(2);
vscale = size(I,1) / rows;
hscale = size(I,2) / cols;
imgClass = class(I);
imres = interp2(double(I), (1:cols)*hscale + 0.5 * (1 - hscale), ...
(1:rows)'*vscale + 0.5 * (1 - vscale), ...
interpalg);
imres = cast(imres,imgClass);
Note: This is a rough start. You many need to perform pre-filtering, or other transformations. Also, this example only supports 2D (grayscale) images. For RGB, adapt this to process each color plane, or simple process each plane in a loop. Again, this is just an example.
Aside from edge handling, this gives the same results as imresize
with anti-aliasing turned off (i.e. imresize(...,'Antialiasing',false)
).
Regarding edge handling, see the documentation for interp2
for information on the extrapval
parameter. The code gets ugly, but you can either patch the min/max elements in the interpolation points (interp2
inputs) to simply map exactly to the edges, or you can use NaN
for extrapval
, and post-process imres
to replace the NaN
s with its neighbor, etc. Note that simply interpolating at points such as linspace(1,size(I,1),rows)
will not give the expected scale change.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…