⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bilinear.m

📁 tuxiangfangsuo 图像放缩功能
💻 M
字号:
% THIS PROGRAMME IS WRITTEN BY Rockins
% THE FEATURE IS BILINEAR-INTERPOLATE THE SOUCE-IMAGE TO GET A DESTINATE-IMAGE
% THE MAXIMUM SCALOR == 9.0, THE MINIMUM SCALOR == 1.0
% Copyright 2006-2007,All Copyrights(C) Reserved by Rockins
% You can redistibute this programme under the GNU Less GPL license
% If you have any question about this programme,please contact me via
% ybc2084@163.com

% read source image into memory,and get the primitive rows and cols
RGB = imread('FreeBSD.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
K = str2double(inputdlg('please input scale factor (must between 1.0 - 9.0)', 'INPUT scale factor', 1, {'5.0'}));

% Validating
if (K < 1.0) | (K > 9.0)
    errordlg('scale factor beyond permitted range(1.0 - 9.0)', 'ERROR');
    error('please input scale factor (must between 1.0 - 9.0)');
end

% display source image
imshow(RGB);

% output image width and height are both scaled by factor K
width = K * ncols;
height = K * nrows;
RR = uint8(zeros(height,width));
GG = uint8(zeros(height,width));
BB = uint8(zeros(height,width));
OUT = uint8(zeros(height,width,ncoms));

% width scalor and height scalor
widthScale = 1/K;
heightScale = 1/K;

% bilinear interpolate
for x = K:width-K                                   % this index range is to avoid exceeding the permitted matrix index
   for y = K:height-K
       xx = x * widthScale;                         % xx and yy are the source ordinate,while x and y are the destinate ordinate
       yy = y * heightScale;
       if (xx <= 1.0e-8)
          xx = 1;
       end
       if (xx > ncols - 1)
          xx = ncols - 1;
       end
       if (yy <= 1.0e-8)
          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(fix(yy));                     % (a,b) is the base-dot
           b = double(fix(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;
% show the interplated image
imwrite(OUT, 'FreeBSD2.jpg', 'jpg');
figure;
imshow(OUT);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -