楼主
分享一段双线性插值法缩放矩阵或图像MATLAB代码
467
0
发表于 2023-3-5 23:14:31
- %----------------------双线性插值法缩放矩阵或图像---------------------------
- % Input:
- % I:图像文件名或矩阵(整数值(0~255))
- % zmf:缩放因子,即缩放的倍数
- % Output:
- % 缩放后的图像矩阵 ZI
- % Usage:
- % ZI = SSELMHSIC('ImageFileName',zmf)
- % 对图像I进行zmf倍的缩放并显示
- % Or:
- % ZI = SSELMHSIC(I,zmf)
- % 对矩阵I进行zmf倍的缩放并显示
- %-------------------------------------------------------------------
- %%%% Authors: Zhi Liu
- %%%% XiDian University Student
- %%%% DATE: 16-12-2013
- % 确实能实现把图片放大,但是不失真太多,比把图片直接放大分辨率更高
- clc;clear all;close all;
- %% Step1 对数据进行预处理
- zmf = 2; % 缩放因子,即缩放的倍数
- [I,M] = imread("78.png");
- if zmf <= 0
- error('缩放倍数 zmf的值应该大于0!');
- end
- %% Step2 通过原始图像和缩放因子得到新图像的大小,并创建新图像。
- [IH,IW,ID] = size(I);
- ZIH = round(IH*zmf); % 计算缩放后的图像高度,最近取整
- ZIW = round(IW*zmf); % 计算缩放后的图像宽度,最近取整
- ZI = zeros(ZIH,ZIW,ID); % 创建新图像
- %% Step3 扩展矩阵I边缘
- IT = zeros(IH+2,IW+2,ID); % 小图像
- IT(2:IH+1,2:IW+1,:) = I; % 把原图像赋值过去
- IT(1,2:IW+1,:)=I(1,:,:);
- IT(IH+2,2:IW+1,:)=I(IH,:,:);
- IT(2:IH+1,1,:)=I(:,1,:);
- IT(2:IH+1,IW+2,:)=I(:,IW,:);
- IT(1,1,:) = I(1,1,:);
- IT(1,IW+2,:) = I(1,IW,:);
- IT(IH+2,1,:) = I(IH,1,:);
- IT(IH+2,IW+2,:) = I(IH,IW,:);
- %% Step4 由新图像的某个像素(zi,zj)映射到原始图像(ii,jj)处,并插值。
- for zj = 1:ZIW % 对图像进行按列逐元素扫描
- for zi = 1:ZIH
- ii = (zi-1)/zmf; jj = (zj-1)/zmf;
- i = floor(ii); j = floor(jj); % 向下取整
- u = ii - i; v = jj - j;
- i = i + 1; j = j + 1;
- 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,:);
- end
- end
- ZI = uint8(ZI);
- %% 以图像的形式显示同现矩阵P
- figure
- imshow(I,M);
- axis on
- title(['原图像(大小: ',num2str(IH),'*',num2str(IW),'*',num2str(ID),')']);
- figure
- imshow(ZI,M);
- axis on
- title(['缩放后的图像(大小: ',num2str(ZIH),'*',num2str(ZIW),'*',num2str(ID)',')']);
- % -----------------
- % function R = bicubic(src, scale)
- %% 双三次插值
- src = imread("78.png");
- src = double(src) / 255;
- scale = 2;
- % 判断是灰度图还是RGB图像
- if ismatrix(src)
- R = zeros(floor(size(src) * scale));
- else
- R = zeros([floor(size(src, 1, 2) * scale), 3]);
- end
- [dstM, dstN, ~] = size(R);
- % 扩展原图像
- misrc = zeros([size(src, 1, 2) + 2 * floor(scale), size(R, 3)]);
- for i = 1 : size(R, 3)
- tmp = padarray(src(:, :, i), [floor(scale), floor(scale)], 'symmetric');
- misrc(:, :, i) = tmp;
- end
- %逐像素点赋值
- for dstX = 1 : dstM
- for dstY = 1 : dstN
- srcX = floor((dstX + 0.5) / scale - 0.5);
- srcY = floor((dstY + 0.5) / scale - 0.5);
- u = ((dstX + 0.5) / scale - 0.5) - srcX;
- v = ((dstY + 0.5) / scale - 0.5) - srcY;
- X1 = zeros(4, 4);
- X2 = zeros(4, 4);
- W1 = ones(4, 4);
- W2 = ones(4, 4);
- % Bicubic基函数
- for i = 1 : 4
- for j = 1 : 4
- X1(i, j) = abs(u - i + 2);
- X2(i, j) = abs(v - j + 2);
- if X1(i, j) <= 1
- W1(i, j) = 1.5 * (X1(i, j)) ^ 3 - 2.5 * (X1(i, j)) ^ 2 + 1;
- else
- if X1(i, j) < 2
- W1(i, j) = (-0.5) * (X1(i, j)) ^ 3 + 2.5 * (X1(i, j)) ^ 2 - 4 * X1(i, j) + 2;
- else
- W1(i, j) = 0;
- end
- end
- if X2(i, j) <= 1
- W2(i, j) = 1.5 * (X2(i, j)) ^ 3 - 2.5 * (X2(i, j)) ^ 2 + 1;
- else
- if X2(i, j) < 2
- W2(i, j) = (-0.5) * (X2(i, j)) ^ 3 + 2.5 * (X2(i, j)) ^ 2 - 4 * X2(i, j) + 2;
- else
- W2(i, j) = 0;
- end
- end
- end
- end
- W = W1 .* W2;
- Z = ones(4, 4); %16个源像素点矩阵
- O = ones(4, 4); %16个加权后的源像素点矩阵
- for dstC = 1 : size(R, 3)
- for i = 1 : 4
- for j = 1 : 4
- Z(i, j) = misrc(srcX - 2 + i + round(scale), srcY - 2 + j + round(scale), dstC);
- O(i, j) = W(i, j) .* Z(i,j);
- end
- end
- O1 = sum(sum(O));
- R(dstX, dstY, dstC) = O1;
- end
- end
- end
- figure,imshow(R);
插值后的图像为 502*788