【问题标题】:Resizing an Image in MATLAB在 MATLAB 中调整图像大小
【发布时间】:2012-12-07 01:17:18
【问题描述】:

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

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

我认为我的插值非常不正确:/

【问题讨论】:

  • 我假设你的意思是你不想使用imresize,即使你写了resize。因为我会为此使用imresize

标签: matlab image-processing


【解决方案1】:

我对@9​​87654321@ 提出了一个关于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%

【讨论】:

    猜你喜欢
    • 2012-09-13
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 2013-04-08
    • 1970-01-01
    相关资源
    最近更新 更多