图像像素灰度内插(Matlab实现)
常用的像素灰度内插法:最近邻元法、双线性内插法、三次内插法
%%像素灰度内插 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 = 1:h/factor for j = 1:w/factor itp1(i,j) = J3(floor(i*factor+0.5),floor(j*factor+0.5)); end end
2.双线性内插法
%双线性内插法 for i = 2:h/u-1 for j = 2:w/v-1 itp2(i,j) = (1-(i*u-floor(i*u)))*(1-(j*v-floor(j*v)))*J3(floor(i*u),floor(j*v))... +(1-(i*u-floor(i*u)))*(j*v-floor(j*v))*J3(floor(i*u),floor(j*v)+1)... +(i*u-floor(i*u))*(1-(j*v-floor(j*v)))*J3(floor(i*u)+1,floor(j*v))... +(i*u-floor(i*u))*(j*v-floor(j*v))*J3(floor(i*u)+1,floor(j*v)+1); 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(2/u):h/u-2 for j = ceil(2/v):w/v-2 A = [S(1+(j*v-floor(j*v))) S((j*v-floor(j*v))) S(1-(j*v-floor(j*v))) S(2-(j*v-floor(j*v)))]; C = [S(1+(i*u-floor(i*u)));S((i*u-floor(i*u)));S(1-(i*u-floor(i*u)));S(2-(i*u-floor(i*u)))]; i1 = floor(i*u); j1 = floor(j*v); B = [f(i1-1,j1-1) f(i1-1,j1) f(i1-1,j1+1) f(i1-1,j1+2); f(i1,j1-1) f(i1,j1) f(i1,j1+1) f(i1,j1+2); f(i1+1,j1-1) f(i1+1,j1) f(i1+1,j1+1) f(i1+1,j1+2); f(i1+2,j1-1) f(i1+2,j1) f(i1+2,j1+1) f(i1+2,j1+2);]; itp3(i,j) = A*B*C; end end
上述的内插法处理的结果出现了黑边,可以通过将黑边区域赋予原始图像的对应区域的像素灰度值。
可通过以下循环实现。
%去黑边
[m,n] = size(J3);%原始图像的行列数 for i = 1:floor(1/u) for j = 1: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 = 1:floor(1/v) for i = 1: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