常用的像素灰度内插法:最近邻元法、双线性内插法、三次内插法
%%像素灰度内插
factor = 0.75;%缩放比
u = 0.6;v = 0.7;
itp1 = uint8(zeros(ceil(h/factor),ceil(w/factor)));
itp2 = uint8(zeros(ceil(h/u),ceil(w/v)));
itp3 = uint8(zeros(ceil(h/u),ceil(w/v)));
下述的J3为待插值灰度图像,itp为插值图像
1.最近邻元法
在待求像素的四邻像素中,将距离这点最近的邻像素灰度赋给待求像素。
%最近邻元法
for i = :h/factor
for j = :w/factor
itp1(i,j) = J3(floor(i*factor+0.5),floor(j*factor+0.5));
end
end
2.双线性内插法
%双线性内插法
for i = :h/u-
for j = :w/v-
itp2(i,j) = (-(i*u-floor(i*u)))*(-(j*v-floor(j*v)))*J3(floor(i*u),floor(j*v))...
+(-(i*u-floor(i*u)))*(j*v-floor(j*v))*J3(floor(i*u),floor(j*v)+)...
+(i*u-floor(i*u))*(-(j*v-floor(j*v)))*J3(floor(i*u)+,floor(j*v))...
+(i*u-floor(i*u))*(j*v-floor(j*v))*J3(floor(i*u)+,floor(j*v)+); end
end
3.三次内插法
function [f] = S(x)
if (0<=abs(x))&&(abs(x)<1)
f = 1-2*(abs(x))^2+(abs(x))^3;
elseif (1<=abs(x))&&(abs(x)<2)
f = 4-8*abs(x)+5*(abs(x))^2-(abs(x))^3;
else
f = 0;
end
end %三次内插法
f = double(J3);
u = 0.6;v = 0.7;
for i = ceil(/u):h/u-
for j = ceil(/v):w/v-
A = [S(+(j*v-floor(j*v))) S((j*v-floor(j*v))) S(-(j*v-floor(j*v))) S(-(j*v-floor(j*v)))];
C = [S(+(i*u-floor(i*u)));S((i*u-floor(i*u)));S(-(i*u-floor(i*u)));S(-(i*u-floor(i*u)))];
i1 = floor(i*u); j1 = floor(j*v);
B = [f(i1-,j1-) f(i1-,j1) f(i1-,j1+) f(i1-,j1+);
f(i1,j1-) f(i1,j1) f(i1,j1+) f(i1,j1+);
f(i1+,j1-) f(i1+,j1) f(i1+,j1+) f(i1+,j1+);
f(i1+,j1-) f(i1+,j1) f(i1+,j1+) f(i1+,j1+);];
itp3(i,j) = A*B*C;
end
end
上述的内插法处理的结果出现了黑边,可以通过将黑边区域赋予原始图像的对应区域的像素灰度值。
可通过以下循环实现。
%去黑边
[m,n] = size(J3);%原始图像的行列数 for i = :floor(1/u)
for j = :floor(n/v)
itp(i,j) = J3(ceil(i*u),ceil(j*v));
itp(floor(m/u),j) = J3(m,ceil(j*v));
end
end
for j = :floor(1/v)
for i = :floor(m/u)
itp(i,j) = J3(ceil(i*u),ceil(j*v));
itp(i,floor(n/v)) = J3(ceil(i*u),n);
end
end