资源简介
matlab图像插值,包括最近邻法,双线性内插法,三次卷积内插法
代码片段和文件信息
% 直接读取RGB图像数据------------------------------------------------------------------------
RGB = imread(‘x.jpg‘);
R = RGB(::1);
G = RGB(::2);
B = RGB(::3);
[nrowsncolsncoms]=size(RGB);
% 键入放大的比例系数并输出原图像
K = str2double(inputdlg(‘请输入放大系数‘ ‘请输入放大系数‘ 1 {‘5.0‘}));
figure(‘Name‘‘原图‘)
imshow(RGB);
% 创建输出图像的空矩阵
width = K * ncols;
height = K * nrows;
RR = uint8(zeros(heightwidth));
GG = uint8(zeros(heightwidth));
BB = uint8(zeros(heightwidth));
OUT = uint8(zeros(heightwidthncoms));
% 映射比例,即原图放大倍数的倒数
widthScale = 1/K;
heightScale = 1/K;
%双线性内插*********************************-------------------------------------------------
for x = 1:width
for y = 1:height
xx = x * widthScale; %%坐标映射(x为放大后图像坐标,xx为映射之后原图像的坐标)
yy = y * heightScale;%坐标映射
if (xx <= 1.0)%防止左边界溢出
xx = 1;
end
if (xx >= ncols)%防止右边界溢出
xx = ncols-1;
end
if (yy <= 1.0)
yy = 1;
end
if (yy >= nrows )
yy = nrows-1 ;
end
if (xx/double(uint16(xx)) == 1.0) && (yy/double(uint16(yy)) == 1.0) % 如果映射后坐标为整数,就取映射后坐标的像元值
RR(yx) = R(int16(yy)int16(xx));
GG(yx) = G(int16(yy)int16(xx));
BB(yx) = B(int16(yy)int16(xx));
else
a = double(fix(yy));%放大后图像点(xy)所对应的原图像上的点(xx,yy)的向零取整近坐标(ab)
b = double(fix(xx));
r11 = double(R(ab));
r12 = double(R(ab+1));
r21 = double(R(a+1b));
r22 = double(R(a+1b+1));
RR(yx) = uint8( (b+1-xx) * ((yy-a)*r21 + (a+1-yy)*r11) + (xx-b) * ((yy-a)*r22 +(a+1-yy) * r12) );%双线性内插
g11 = double(G(ab));
g12 = double(G(ab+1));
g21 = double(G(a+1b));
g22 = double(G(a+1b+1));
GG(yx) = uint8( (b+1-xx) * ((yy-a)*g21 + (a+1-yy)*g11) + (xx-b) * ((yy-a)*g22 +(a+1-yy) * g12) );
b11 = double(B(ab));
b12 = double(B(ab+1));
b21 = double(B(a+1b));
b22 = double(B(a+1b+1));
BB(yx) = 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;
%在当前文件夹保存图像
imwrite(OUT ‘bilinear.jpg‘ ‘jpg‘);
figure(‘Name‘‘双线性内插‘)
%显示图像
imshow(OUT);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 7008 2017-10-28 19:56 interpolation.m
文件 1961 2017-10-28 17:55 Nearest.m
文件 402 2017-10-28 15:22 SinXDivX.m
文件 2733 2017-10-28 17:49 Three_convolution.m
文件 2600 2017-10-28 17:54 bilinear.m
----------- --------- ---------- ----- ----
14704 5
- 上一篇:配电网粒子群算法
- 下一篇:数字下变频MATLAB仿真程序
评论
共有 条评论