在这里,我想使用双线性插值方法放大我的图像(灰色),但我无法正确生成最终输出图像,这里是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
在这段代码中我得到缩放图像,但我丢失了一些像素值,我没有得到正确的图像。
答案 0 :(得分:0)
有一个很好的内置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'));
通过这种方式,您可以节省一些时间来手动编写。