在MATLAB中调整图像大小

时间:2011-05-31 04:58:24

标签: matlab image-processing

我正在尝试创建一个函数,根据作业作业的值(scale_zoom)缩放图像。我不想在这个函数中使用内置函数resize()的MATLAB,所以我试图进行插值。任何帮助将不胜感激。这就是我到目前为止所做的:

function pic_new=scale_image(pic,scale_zoom)
    [row, col]=size(pic)
    ht_scale=size(pic,1)/scale_zoom*col
    wid_scale=size(pic,2)/scale_zoom*row

    size(ht_scale)
    size(wid_scale)
    x=(0:scale_zoom)*wid_scale
    y=(0:scale_zoom)*ht_scale
    length(x)
    length(y)
    %plotvals=0:0.1:scale_zoom (this is not necessary i think)
    newimg=interp1(pic,x,y,'cubic')
    image(newimg)
end

我认为我插错的方式非常错误:/

1 个答案:

答案 0 :(得分:6)

我有previously answered关于scaling images using nearest-neighbor interpolation的问题,我在这里使用的大部分代码和解释都适用于此。主要区别在于最终插值步骤。以下是使用INTERP2编写函数并概括2-D grayscale or 3-D RGB image of any type的方法:

function pic_new = scale_image(pic,scale_zoom)

  oldSize = size(pic);                               %# Old image size
  newSize = max(floor(scale_zoom.*oldSize(1:2)),1);  %# New image size
  newX = ((1:newSize(2))-0.5)./scale_zoom+0.5;  %# New image pixel X coordinates
  newY = ((1:newSize(1))-0.5)./scale_zoom+0.5;  %# New image pixel Y coordinates
  oldClass = class(pic);  %# Original image type
  pic = double(pic);      %# Convert image to double precision for interpolation

  if numel(oldSize) == 2  %# Interpolate grayscale image

    pic_new = interp2(pic,newX,newY(:),'cubic');

  else                    %# Interpolate RGB image

    pic_new = zeros([newSize 3]);  %# Initialize new image
    pic_new(:,:,1) = interp2(pic(:,:,1),newX,newY(:),'cubic');  %# Red plane
    pic_new(:,:,2) = interp2(pic(:,:,2),newX,newY(:),'cubic');  %# Green plane
    pic_new(:,:,3) = interp2(pic(:,:,3),newX,newY(:),'cubic');  %# Blue plane

  end

  pic_new = cast(pic_new,oldClass);  %# Convert back to original image type

end

您可以按如下方式测试:

img = imread('peppers.png');      %# Load the sample peppers image
newImage = scale_image(img,0.3);  %# Scale it to 30%