matlab 中图的大小,在MATLAB中调整图像大小

I'm trying to create a function the scales an image based on a value (scale_zoom) for a homework assignment. I don't want to use the MATLAB built in function resize() in this function so I'm trying to interpolate. Any help would be greatly appreciated. This is what I have so far:

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

I think I'm interpolating it very incorrectly :/

解决方案

I had previously answered a question about scaling images using nearest-neighbor interpolation, and much of the code and explanations I used there apply here. The main difference is the final interpolation step. Here's how you can write your function, using INTERP2 and generalizing for a 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

And you can test it as follows:

img = imread('peppers.png'); %# Load the sample peppers image

newImage = scale_image(img,0.3); %# Scale it to 30%

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值