klu*_*rie 5 matlab computer-vision
我试图使用3D变换矩阵转换图像,并假设我的相机是正交的.
我使用平面诱导的单应公式H = Rt*n'/ d(d = Inf so H = R)定义我的单应性,如Hartley和Zisserman第13章中给出的那样.
令我感到困惑的是,当我使用相当适度的旋转时,图像似乎比我预期的扭曲得多(我确信我不会混淆弧度和度数).
这可能会出错?
我附上了我的代码和示例输出.
n = [0;0;-1];
d = Inf;
im = imread('cameraman.tif');
rotations = [0 0.01 0.1 1 10];
for ind = 1:length(rotations)
theta = rotations(ind)*pi/180;
R = [ 1 0 0 ;
0 cos(theta) -sin(theta);
0 sin(theta) cos(theta)];
t = [0;0;0];
H = R-t*n'/d;
tform = maketform('projective',H');
imT = imtransform(im,tform);
subplot(1,5,ind) ;
imshow(imT)
title(['Rot=' num2str(rotations(ind)) 'deg']);
axis square
end
Run Code Online (Sandbox Code Playgroud)
公式H = Rt*n'/ d有一个假设,在您的情况下不符合:
此公式表示您使用焦距= 1的针孔相机模型
但在您的情况下,为了使您的相机更真实并且您的代码能够正常工作,您应该将焦距设置为远大于1的正数.(焦距是从相机中心到图像平面的距离)
为此,您可以定义处理焦距的校准矩阵K. 您只需要将公式更改为 H = KR inv(K) - 1/d K tn'inv(K) 其中K是3乘3的单位矩阵,其沿对角线的两个第一个元素设置为焦点长度(例如f = 300).如果你假设一个投影相机,可以很容易地推导出公式.
下面是您的代码的更正版本,其中角度是有意义的.
n = [0;0;-1];
d = Inf;
im = imread('cameraman.tif');
rotations = [0 0.01 0.1 30 60];
for ind = 1:length(rotations)
theta = rotations(ind)*pi/180;
R = [ 1 0 0 ;
0 cos(theta) -sin(theta);
0 sin(theta) cos(theta)];
t = [0;0;0];
K=[300 0 0;
0 300 0;
0 0 1];
H=K*R/K-1/d*K*t*n'/K;
tform = maketform('projective',H');
imT = imtransform(im,tform);
subplot(1,5,ind) ;
imshow(imT)
title(['Rot=' num2str(rotations(ind)) 'deg']);
axis square
end
Run Code Online (Sandbox Code Playgroud)
您可以在下图中看到结果:
您还可以围绕其中心旋转图像.为了实现这一点,您应该将图像平面原点设置为图像的中心,我认为使用matlab(maketform)的方法是不可能的.您可以使用以下方法.
imT=imagehomog(im,H','c');
Run Code Online (Sandbox Code Playgroud)
请注意,如果使用此方法,则必须更改n,d,t和R中的某些设置以获得适当的结果.该方法可在以下网址找到:https://github.com/covarep/covarep/blob/master/external/voicebox/imagehomog.m
具有imagehomog的程序的结果以及n,d,t和R的一些变化如下所示,这看起来更真实.
新设置是:
n = [0 0 1]';
d = 2;
t = [1 0 0]';
R = [cos(theta), 0, sin(theta);
0, 1, 0;
-sin(theta), 0, cos(theta)];
Run Code Online (Sandbox Code Playgroud)