MATLAB双线性插值 出错
以下是程序及程序结果报错,高手看下哪里出错
clear
close all
I=imread('lena.bmp');
imshow(I);
title('原图像')
[m,n]=size(I);
ang= str2double(inputdlg('请输入旋转角度', 'INPUT scale factor', 1, {'10'}));
row=n*sin(ang*pi/180)+m*cos(ang*pi/180);
col=n*cos(ang*pi/180)+m*sin(ang*pi/180);
Out = uint8(zeros(floor(row),floor(col))); %创建输出图像矩阵
for x=1:row-1
for y=1:col-1
oldX=floor((x-row/2)*cos(ang*pi/180)-(y-col/2)*sin(ang*pi/180)+m/2);
oldY=floor((y-row/2)*cos(ang*pi/180)+(x-col/2)*sin(ang*pi/180)+n/2);
if (oldX/double(uint16(oldX)) == 1.0) & (oldY/double(uint16(oldY)) == 1.0)
Out(x,y) = I(oldX,oldY);%若oldX,oldY为整数,直接赋值
else
a = double(uint16(oldX));
b = double(uint16(oldY));
x11 = double(I(a,b)); % x11 赋值为 I(a,b)
x12 = double(I(a,b+1)); % x12 赋值为 I(a,b+1)
x21 = double(I(a+1,b)); % x21 赋值为 I(a+1,b)
x22 = double(I(a+1,b+1)); % x22 赋值为 I(a+1,b+1)
Out(x,y) = uint8( (b+1-oldY) * ((oldX-a)*x21 + (a+1-oldX)*x11) + (oldY-b) * ((oldX-a)*x22 +(a+1-oldX) * x12) ); % 用双线性插值计算公式计算
end
end
end
figure;
imshow(Out);
title('按中心点旋转')
结果Warning: Divide by zero.
> In Untitledxuanzhuan1 at 15
??? Attempted to access I(8,0); index must be a positive integer or logical.
Error in ==> Untitledxuanzhuan1 at 20
x11 = double(I(a,b)); % x11 赋值为 I(a,b)
[解决办法]
看报错
14行计算oldY=0了,第15行做除法就除数为0的错
19行b=0,20行给x11赋值的时候下标I(a,b)时候出错了,matlab的下标是从1开始的
如果非要取整,要么floor要么ceil但是都得判断0或者超过宽度/高度的情况.程序才能健壮