使用您发布的数据:
P = [px(:) py(:)];
Q = [qx(:) qy(:)];
计算转换:
H = Q/P;
应用转换:
Q2 = H*P;
比较结果:
err = Q2-Q
输出:
err =
7.1054e-15 7.1054e-15
-3.5527e-15 -3.5527e-15
-1.4211e-14 -2.1316e-14
1.4211e-14 1.4211e-14
对于所有意图和目的而言,这都是零。
编辑:
因此,正如您在评论中指出的那样,上述方法不会计算 3x3 单应矩阵。它只是用提供的点数一样多的方程求解方程组:
H * A = B --> H = B*inv(A) --> H = B/A (mrdivide)
否则,MATLAB 在图像处理工具箱中有 CP2TFORM 函数。这是一个应用于所示图像的示例:
%# read illustration image
img = imread('http://i.stack.imgur.com/ZvaZK.png');
img = imcomplement(im2bw(img));
%# split into two equal-sized images
imgs{1} = img(:,fix(1:end/2));
imgs{2} = img(:,fix(end/2:end-1));
%# extract the four corner points A and B from both images
C = cell(1,2);
for i=1:2
%# some processing
I = imfill(imgs{i}, 'holes');
I = bwareaopen(imclearborder(I),200);
I = imfilter(im2double(I), fspecial('gaussian'));
%# find 4 corners
C{i} = corner(I, 4);
%# sort corners in a consistent way (counter-clockwise)
idx = convhull(C{i}(:,1), C{i}(:,2));
C{i} = C{i}(idx(1:end-1),:);
end
%# show the two images with the detected corners
figure
for i=1:2
subplot(1,2,i), imshow(imgs{i})
line(C{i}(:,1), C{i}(:,2), 'Color','r', 'Marker','*', 'LineStyle','none')
text(C{i}(:,1), C{i}(:,2), num2str((1:4)'), 'Color','r', ...
'FontSize',18, 'Horiz','left', 'Vert','bottom')
end
检测到角点后,现在我们可以获得空间变换:
%# two sets of points
[A,B] = deal(C{:});
%# infer projective transformation using CP2TFORM
T = cp2tform(A, B, 'projective');
%# 3x3 Homography matrix
H = T.tdata.T;
Hinv = T.tdata.Tinv;
%# align points in A into B
X = tformfwd(T, A(:,1), A(:,2));
%# show result of transformation
line(X([1:end 1],1), X([1:end 1],2), 'Color','g', 'LineWidth',2)
结果:
>> H = T.tdata.T
H =
0.74311 -0.055998 0.0062438
0.44989 -1.0567 -0.0035331
-293.31 62.704 -1.1742
>> Hinv = T.tdata.Tinv
Hinv =
-1.924 -0.42859 -0.0089411
-2.0585 -1.2615 -0.0071501
370.68 39.695 1
我们可以自己确认计算:
%# points must be in Homogenous coordinates (x,y,w)
>> Z = [A ones(size(A,1),1)] * H;
>> Z = bsxfun(@rdivide, Z, Z(:,end)) %# divide by w
Z =
152 57 1
219 191 1
62 240 1
92 109 1
映射到 B 中的点:
%# maximum error
>> max(max( abs(Z(:,1:2)-B) ))
ans =
8.5265e-14