以下为双线性插值算法matlab实现的简单总结,以便之后复习。
Bilinear interpolation is a more important interpolation method, especially in the field of digital image processing. Now I introduce the algorithm derivation of bilinear interpolation, and use MATLAB to implement the algorithm. Bilinear interpolation is also called quadratic linear interpolation. Its main idea is to use the four pixels around a pixel to calculate floating-point coordinate pixel values.
As shown in FIG, the pixel values of f1 and f2 are respectively obtained by a linear interpolation, and then the pixel values of f are obtained by linear interpolation for f1 and f2. This is the principle of bilinear interpolation. We can use the following formula to show the process of solving:
The original image and interpolated resultant image are as follows:
参考代码见下:
1 function [ ZI ] = interpolation( I,zmf ) 2 3 %% ----------------------双线性插值法处理图像--------------------------- 4 % Input: 5 % I:图像文件名或矩阵(整数值(0~255)) 6 % zmf:缩放因子,即缩放的倍数 7 % Output: 8 % 对矩阵I进行zmf倍的缩放并显示 9 10 %% 命令行中输入以下命令运行即可: 11 % interpolation(\'flower2.png\', 4); 12 13 %% Step1 对数据进行预处理 14 if ~exist(\'I\',\'var\') || isempty(I) 15 error(\'输入图像I未定义或为空!\'); 16 end 17 if ~exist(\'zmf\',\'var\') || isempty(zmf) || numel(zmf) ~= 1 18 error(\'位移矢量zmf未定义或为空或zmf中的元素超过2!\'); 19 end 20 if ischar(I) 21 [I,M] = imread(I); 22 end 23 if zmf <= 0 24 error(\'缩放倍数zmf的值应该大于0!\'); 25 end 26 27 %% Step2 通过原始图像和缩放因子得到新图像的大小,并创建新图像。 28 [IH,IW,ID] = size(I); 29 ZIH = round(IH*zmf); % 计算缩放后的图像高度,最近取整 30 ZIW = round(IW*zmf); % 计算缩放后的图像宽度,最近取整 31 ZI = zeros(ZIH,ZIW,ID); % 创建新图像 32 33 %% Step3 扩展矩阵I边缘 34 %双线性插值是用待求值点周围四个点的值来计算的,当新图像上的像素点映射到原图像的边界时,边界扩展保证了计算能正确进行,而不会溢出。 35 IT = zeros(IH+2,IW+2,ID); 36 IT(2:IH+1,2:IW+1,:) = I; 37 IT(1,2:IW+1,:)=I(1,:,:); 38 IT(IH+2,2:IW+1,:)=I(IH,:,:); 39 IT(2:IH+1,1,:)=I(:,1,:); 40 IT(2:IH+1,IW+2,:)=I(:,IW,:); 41 IT(1,1,:) = I(1,1,:); 42 IT(1,IW+2,:) = I(1,IW,:); 43 IT(IH+2,1,:) = I(IH,1,:); 44 IT(IH+2,IW+2,:) = I(IH,IW,:); 45 46 %% Step4 由新图像的某个像素(zi,zj)映射到原始图像(ii,jj)处,并插值。 47 for zj = 1:ZIW % 对图像进行按列逐元素扫描 48 for zi = 1:ZIH 49 ii = (zi-1)/zmf; jj = (zj-1)/zmf; 50 i = floor(ii); j = floor(jj); % 向下取整 51 u = ii - i; v = jj - j; 52 i = i + 1; j = j + 1; 53 ZI(zi,zj,:) = (1-u)*(1-v)*IT(i,j,:) + (1-u)*v*IT(i,j+1,:) + u*(1-v)*IT(i+1,j,:) + u*v*IT(i+1,j+1,:); 54 end 55 end 56 57 ZI = uint8(ZI); 58 59 %% 显示处理前后的图像 60 figure; 61 imshow(I,M); 62 axis on; 63 title([\'original image(\',num2str(IH),\'*\',num2str(IW),\'*\',num2str(ID),\')\']); 64 figure; 65 imshow(ZI,M); 66 axis on; 67 title([\'interpolated resultant image(\',num2str(ZIH),\'*\',num2str(ZIW),\'*\',num2str(ID)\',\')\']); 68 end
请发表评论