图像缩放

实验要求:实现一个图像缩放函数,可以对输入图像进行任意倍数的缩放;
                  1.采用双线性插值进行重采样;
                  2.X,Y方向的缩放倍数参函数参数的形式传入;
                  3.可以只考虑输入图像为3通道,8位深度的情况;
                  4.不能调用图像处理库的缩放函数来完成;


RGB = imread('H:/数字图像处理/ipe_data/009.jpg');
R = RGB(:,:,1);
G = RGB(:,:,2);
B = RGB(:,:,3);
[nrows,ncols,ncoms]=size(RGB);
% Next line is the scale-factor,the range is 1.0-9.0
K1 = str2double(inputdlg('请输入行缩放倍数', 'INPUT scale factor', 1, {'0.5'}));%行默认变为原来的0.5倍  
K2 = str2double(inputdlg('请输入列缩放倍数', 'INPUT scale factor', 1, {'0.4'}));%列默认变为原来的0.4倍 
width = K1 * ncols;                   
height = K2 * nrows;  
OUT = uint8(zeros(height,width,ncoms));
RR = uint8(zeros(height,width));
GG = uint8(zeros(height,width));
BB = uint8(zeros(height,width));
% width scalor and height scalor
widthScale = ncols/width;
heightScale = nrows/height;
% bilinear interpolate
for y = 1:height
   for x =1:width % this index range is to avoid exceeding the permitted matrix index
       xx = x * widthScale; % xx and yy are the source ordinate,while x and y are the destinate ordinate
       yy = y * heightScale;
       if (xx <= 1)
          xx = 1;
       end
       if (xx > ncols-1)
          xx = ncols-1;
       end
       if (yy <= 1)
          yy = 1;
       end
       if (yy > nrows-1)
          yy = nrows-1;
       end
       
       if (xx/double(uint16(xx)) == 1.0) && (yy/double(uint16(yy)) == 1.0) % if a and b is integer,then J(x,y) <- I(x,y)
           RR(y,x) = R(int16(yy),int16(xx));
           GG(y,x) = G(int16(yy),int16(xx));
           BB(y,x) = B(int16(yy),int16(xx));
       else % a or b is not integer
           a = double(uint16(yy)); % (a,b) is the base-dot
           b = double(uint16(xx));
           
           r11 = double(R(a,b));
           r12 = double(R(a,b+1));
           r21 = double(R(a+1,b));
           r22 = double(R(a+1,b+1));
           RR(y,x) = uint8( (b+1-xx) * ((yy-a)*r21 + (a+1-yy)*r11) + (xx-b) * ((yy-a)*r22 +(a+1-yy) * r12) );
           
           g11 = double(G(a,b));
           g12 = double(G(a,b+1));
           g21 = double(G(a+1,b));
           g22 = double(G(a+1,b+1));
           GG(y,x) = uint8( (b+1-xx) * ((yy-a)*g21 + (a+1-yy)*g11) + (xx-b) * ((yy-a)*g22 +(a+1-yy) * g12) );
           
           b11 = double(B(a,b));
           b12 = double(B(a,b+1));
           b21 = double(B(a+1,b));
           b22 = double(B(a+1,b+1));
           BB(y,x) = uint8( (b+1-xx) * ((yy-a)*b21 + (a+1-yy)*b11) + (xx-b) * ((yy-a)*b22 +(a+1-yy) * b12) );
       end
    end
end
OUT(:,:,1) = RR;
OUT(:,:,2) = GG;
OUT(:,:,3) = BB;
imshow(RGB),title('原图');
figure;
imshow(OUT),title('修改图');


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值