双线性插值缩放

bi-linear interpolation zoom

在这里,我想使用双线性插值法放大我的图像(灰色),但我无法正确生成最终输出图像,这是 matlab 代码:

I = imread('house.png');              %image i want to zoom in
fact = 1.5;
[rows,cols] = size(I);
Jrows = fact * rows;
Jcols = fact * cols;
J = zeros(floor(fact * size(I)));

I = [I(1,:);I];
I = [I;I(end,:)];                       %replicating boundaries.
I = [I(:,1) ,I];
I = [I,I(:,end)];
I = double(I);

for i = 2: Jrows-2
for j = 2: Jcols-2
x1 = floor(i/fact);  
x2 =  ceil(i/fact); 
y1 = floor(j/fact);                %finding neighbours
y2 =  ceil(j/fact);
if x1==0 
x1=1;
end
if y1==0
y1=1;
end
J(i-1,j-1) = (I(x1,y1)*(x2-i)*(y2-j) + I(x2,y1)*(i-x1)*(y2-j)+ I(x1,y2)*(x2-i)*(j-y1) + I(x2,y2)*(i-x1)*(j-y1));
end
end
figure,imshow(uint8(J)),title('zoom in image');    #final image

在这段代码中,我得到了缩放图像,但我丢失了一些像素值,而且我没有得到正确的图像。

为此有一个出色的内置 matlab 函数:imresize。在此您可以指定几个细节,如插值方法和过滤器内核。在 matlab gui 中选择 imresize 时,只需按 F1。

fact = 1.5;

% read image and make greyscale if necessary
I = double(imread('house.png'));
if size(I, 3) == 3
   I = I(:, :, 1)*0.299 + I(:, :, 2)*0.587 + I(:, :, 1)*0.114;
end

% one-liner??
J = uint8(imresize(I, fact, 'bilinear'));

这样您可以节省一些手动编写的时间。