Matlab中的梯形失真 - 在地板上投射一个正方形

时间:2017-12-08 09:03:37

标签: matlab math

因此,作为实验的一部分,我需要一种可以使用投影仪在地板上投射物体的功能。投影机是倾斜的,因此Keystone效果可以播放,我想纠正它。

我尝试通过计算单应矩阵H来做到这一点。我在投影区域的底部投影一个正方形,测量其属性(它在地面上显示为梯形)并计算矩阵。然后我用这个想法来反转这个过程,使用单应矩阵H(它是逆的)来计算和投影地面上看起来像原始正方形而不是梯形的形状。

但我没有成功。我计算了一个较短的四边形,仍然在地面上得到一个空中飞人。我很确定我的计算有问题,但我不确定。我是Matlab的新手:)请求帮助。下面有很多评论的代码。

function keystoning()

projArea = 100;                         % 100%
testSquare = 20;                        % test square dimension

%% Drawing a "what's supposed to be a square" on the floor 

% black background, red "sqare" dimensions 20x20, attached to the base 
% of the projection area so the base dimension of the "sqare" don't change 
% and minimal measuring is required

testX=[40 40+testSquare 40+testSquare 40];
testY=[0 0 testSquare testSquare];                 

scrsz = get(groot,'screensize');

f = figure('position',[0 0 scrsz(3) scrsz(4)],...
        'OuterPosition',[0 0 scrsz(3) scrsz(4)],...
        'toolbar','none','menubar','none','name','none',...
        'units','normalized',...
        'Color','black','Resize','Off'...
        );

figure(f);                              % focus window

fill(testX, testY,'r');

axis ([0 projArea 0 projArea])
pbaspect([1 1 1])
drawnow 

disp('*** PLACE THE NEAR PART OF THE ELEMENT AT THE END OF THE TREADMILL  ***');

%% Measuring of the shape in order to calculate the homography matrix H

disp('*** Measure the width of the base of the projection area in centimeters.  ***');
prompt = 'Enter the measured distance:  ';
projAreaM = input(prompt);

projAreaCoef=projAreaM/projArea;    % coefficient between the axes scale and the projection area

disp('*** Measure the distance between the paralel lines of the trapezoid in centimeters.  ***');
prompt = 'Enter the measured distance:  ';
trapYm = input(prompt)/projAreaCoef

disp('*** Measure the length of the longer paralel line of the trapezoid in centimeters.  ***');
prompt = 'Enter the measured length:  ';
trapXm = input(prompt)/projAreaCoef

%% Calculating the homography matrix H

% trapezoid definition

% coordinates of the first two points for the trapezoid and the square
% are the same since its at the start of the projection area

difX=(trapXm-testSquare)/2;             % length of the "pertrusions"

trapX=[testX(1) testX(2) testX(3)+difX testX(4)-difX];         
trapY=[testY(1) testY(2) trapYm trapYm];      

% variable definition

P=[testX;testY;ones(size(testX))];      % test rectangle
Q=[trapX;trapY;ones(size(trapX))];      % shown trapezoid

% homography matrix H calculation

H=Q/P;                                  % solution to the equation HP = Q

%% Testing the homography matrix H
% we want to show the rectangle on the ground

Pnew=inv(H)*P;                          
fill(Pnew(1,:),Pnew(2,:),'r');
axis ([0 projArea 0 projArea])
pbaspect([1 1 1])
drawnow 

0 个答案:

没有答案