c(x,b) = c[a+1]*(x-a)+c[a]*(1+a-x);
c(x,b+1) = c[a+1][b+1]*(x-a)+c[a][b+1]*(1+a-x);
好,现在已经知道c(x,b)和c(x,b+1)了,而根据假设c(x,b)到c(x,b+1)也是线性变化的,所以:
c(x,y) = c(x,b+1)*(y-b)+c(x,b)*(1+b-y) 这就是双线性插值
2、图像缩放的基本原理:
1) 根据已有的数字图像重建物理图像。
2) 对重建的物理图像以所需要的分辨率重采样。
四设计程序
应用matlab编程: clear;
I=imread('f.jpg'); Z=rgb2gray(I); [rows,cols]=size(Z);
K1 = str2double(inputdlg('请输入行缩放倍数', 'INPUT scale factor', 1, {'0.5'}));%行默认变为原来的0.5倍
K2 = str2double(inputdlg('请输入列缩放倍数', 'INPUT scale factor', 1, {'0.5'}));%列默认变为原来的0.5倍 width = K1 * rows; height = K2 * cols;
Out = uint8(zeros(width,height)); %创建输出图像矩阵 widthScale = rows/width; heightScale = cols/height;
for x = 6:width - 6 % 6是为了防止矩阵超出边界溢出 for y = 6:height - 6
oldX = x * widthScale; % oldX,oldY为原坐标,x,y为新标 oldY = y * heightScale;
if (oldX/double(uint16(oldX)) == 1.0) &
(oldY/double(uint16(oldY)) == 1.0)
Out(x,y) = Z(int16(oldX),int16(oldY));%若oldX,oldY为整数,直接赋值
else
a = double(uint16(oldX)); b = double(uint16(oldY));
x11 = double(Z(a,b)); % x11 赋值I(a,b) x12 = double(Z(a,b+1)); % x12 赋值I(a,b+1) x21 = double(Z(a+1,b)); % x21 赋值I(a+1,b)
x22 = double(Z(a+1,b+1)); % x22 赋I(a+1,b+1) Out(x,y) = uint8( (b+1-oldY) * ((oldX-a)*x(a+1-oldX)*x11)
+ (oldY-b) * ((oldX-a)*x22 +(a+1-oldX) * x12) ); % 用双线性插值计算公式计算 end end
end imshow(Z); figure;
五效果图对比
压缩前图像