Matlab使用角点

时间:2017-04-30 16:12:35

标签: matlab image-processing

我想用透视扭曲纠正图像。我有角点,我也有一个算法,可以执行我需要的但它执行得很慢。它具有'imtransform'和'maketform'功能,matlab可以为这些动作提供更快的功能。所以我试图替换它们,但我无法做到正确。任何帮助将不胜感激。

以下是使这个问题更加清晰的图像:

输入已知坐标(x,y)的图像:

Input Image With Known Coordinates

和期望的输出:

Oupput Image with (perspectival rectified)

这个过程以2秒的间隔执行,我需要通过新的matlab函数替换这个过程,但我无法做到。

老algorihm是:

%X has the clockwise X coordinates %Y has the clockwise Y coordinates    
A=zeros(8,8);
A(1,:)=[X(1),Y(1),1,0,0,0,-1*X(1)*x(1),-1*Y(1)*x(1)];
A(2,:)=[0,0,0,X(1),Y(1),1,-1*X(1)*y(1),-1*Y(1)*y(1)];

A(3,:)=[X(2),Y(2),1,0,0,0,-1*X(2)*x(2),-1*Y(2)*x(2)];
A(4,:)=[0,0,0,X(2),Y(2),1,-1*X(2)*y(2),-1*Y(2)*y(2)];

A(5,:)=[X(3),Y(3),1,0,0,0,-1*X(3)*x(3),-1*Y(3)*x(3)];
A(6,:)=[0,0,0,X(3),Y(3),1,-1*X(3)*y(3),-1*Y(3)*y(3)];

A(7,:)=[X(4),Y(4),1,0,0,0,-1*X(4)*x(4),-1*Y(4)*x(4)];
A(8,:)=[0,0,0,X(4),Y(4),1,-1*X(4)*y(4),-1*Y(4)*y(4)];

v=[x(1);y(1);x(2);y(2);x(3);y(3);x(4);y(4)];

u=A\v;
%transfer fonksiyonumuz

U=reshape([u;1],3,3)';

w=U*[X';Y';ones(1,4)];
w=w./(ones(3,1)*w(3,:));

T=maketform('projective',U');

%transform uygulayıp resmi düzleştiriyoruz
P2=imtransform(I,T,'XData',[1 n],'YData',[1 m]);

如果有帮助,这就是我如何生成“A”矩阵和U矩阵:

Out Link

1 个答案:

答案 0 :(得分:5)

使用内置的MATLAB函数(fitgeotransimref2dimwarp),以下代码在我的笔记本电脑上运行0.06秒:

% read the image
im = imread('paper.jpg');
tic
% set the moving points := the original image control points
x = [1380;2183;1282;422];
y = [727;1166;2351;1678];
movingPoints = [x,y];
% set the fixed points := the desired image control points
xfix = [1;1000;1000;1];
yfix = [1;1;1000;1000];
fixedPoints = [xfix,yfix];
% generate geometric transform
tform = fitgeotrans(movingPoints,fixedPoints,'projective');
% generate reference object (full desired image size)
R = imref2d([1000 1000]);
% warp image
outputImage = imwarp(im,tform,'OutputView',R);
toc
% show image
imshow(outputImage);

enter image description here