A simple geometrical construction with 2D homogeneous coordinates
Our goal is to complete the drawing of a cuboid. We use need six vertices on the contour of the cuboid.
Contents
Data input
im=imread('bluecube.jpg'); imshow(im); hold on; [x y]=getpts; plot(x,y,'or','MarkerSize',12); a=[x(1) y(1) 1]'; b=[x(2) y(2) 1]'; f=[x(3) y(3) 1]'; h=[x(4) y(4) 1]'; g=[x(5) y(5) 1]'; c=[x(6) y(6) 1]';
Warning: Image is too big to fit on screen; displaying at 50%
Finding and plotting vanishing points
vab=cross(cross(a,b),cross(h,g)); vac=cross(cross(a,c),cross(f,h)); vae=cross(cross(b,f),cross(c,g)); vab=vab/vab(3); vac=vac/vac(3); vae=vae/vae(3); plot(vab(1),vab(2),'x'); plot(vac(1),vac(2),'x'); plot(vae(1),vae(2),'x');
Finding unknown points
d=cross(cross(b,vac),cross(c,vab)); d=d/d(3); e=cross(cross(g,vac),cross(f,vab)); e=e/e(3);
Drawing
we can now finally draw the cube.
myline=[a';b';d';c';a']; line(myline(:,1),myline(:,2),'LineWidth',5); myline=[e';f';h';g';e']; line(myline(:,1),myline(:,2),'LineWidth',5); myline=[a';e']; line(myline(:,1),myline(:,2),'LineWidth',5); myline=[b';f']; line(myline(:,1),myline(:,2),'LineWidth',5); myline=[c';g']; line(myline(:,1),myline(:,2),'LineWidth',5); myline=[d';h']; line(myline(:,1),myline(:,2),'LineWidth',5);
Rectify a face
We can also transform the image so that a face becomes a square.
T = maketform('projective',[a(1:2)';b(1:2)';f(1:2)';e(1:2)'],[0 0;300,0;300,300;0,300]);
figure, imshow(imtransform(im,T));
Warning: Image is too big to fit on screen; displaying at 67%
Such transformation is called homography, and will be presented later during the Image Analysis and Synthesis course. Still, it is a linear transformation, so it has the same form of the simpler 2D transformations (translations and rotations) which have already been introduced, a 3x3 matrix:
T.tdata.T
ans =
0.4007 0.1674 -0.0001
-0.2914 0.4613 0.0001
-32.2264 -189.8333 1.0000