我试图在图像和标记的矩形上应用投影单应性。我的解决方案适用于所有仿射变换,但在投影变换的情况下,当我在循环中运行时,系统错误会不断增加。我似乎无法弄清楚原因。快速回答将非常感谢。请查看代码和屏幕截图。感谢
clc;clear;close all;
background_img_dir = 'D:/eyedeus/dataset/background_imgs/';
background_img = [background_img_dir '1.jpg'];
img = imread('D:/workspace/dataset/taj.jpg');
rect = [236 333 325 226; 304 303 441 440];
K = eye(3);
steps=pi/100; ang=-steps;
rotaxis=[0.5,0.5,1]; rotaxis=rotaxis./norm(rotaxis);
N = 100;
for i = 1:N
ang=ang+steps;
R=makehgtform('axisrotate',rotaxis,ang);
H = K * R(1:3,1:3) * inv(K);
Hp = H';
T = maketform('projective', Hp);
xsize=size(img,1);
ysize=size(img,2);
t_img = imtransform(img, T, 'UData',[0 1],'VData',[0 1]);
x = [rect ; ones(1,4)];
pts = H * x;
pts = pts(1:2, :) ./ repmat(x(3,:),2,1);
clf;
imshow(t_img); hold on;
line([pts(1,:) pts(1,1)], [pts(2,:) pts(2,1)], 'color', 'b', 'LineWidth', 2);
pause(0.1);
end
带有标记蓝色矩形的第一张图片:
应用单应性后:
答案 0 :(得分:1)
在单应性的分裂步骤中看起来像是一个错字。而不是:
pts = H * x;
pts = pts(1:2, :) ./ repmat(x(3,:),2,1);
你想要这个:
pts = H * x;
pts = pts(1:2, :) ./ repmat(pts(3,:),2,1);