Elementary Projective Transformation

Transformation with DRAW-statements

Let A=(aij) be a matrix of 4 rows and 4 columns defined with
DIM A(4,4)
That is, A(i,j)=aij.
When a picture pict_a is drawn by a statement
DRAW pict_a WITH A
any point ( x, y ) is transformed to the point (x'/w', y'/w')
on which x', y' and w' are determined by a matrix operation




( x' y' z' w' ) = ( x y 0 1 )
(
a11 a12 a13 a14
a21 a22 a23 a24
a31 a32 a33 a34
a41 a42 a43 a44
)

This transformation is a projective one.
 

Example 1.

We want the figure drawn on the ground 10m ahead to seem to be drawn vertically, assuming the height of the viewpoint be 1.5m.
Let the coordinates of the plane standing vertically 10m ahead be (x,y), the coordinates of the horizontal plane 10m ahead be (x',y'), where the positive direction of x is horizontal, the positive direction of y is vertical, the positive direction of x' is identical with that of x, the positive direction of y' is away from the viewpoint.
Then,the proportion   10+y'y'=1.5:y   yields that

y' =     10y  
1.5 - y

Also the proportion   xx'=10:10+y'   and the above yields that

x' =     1.5x    
1.5 - y


Hence the transformation is the projective transformation determined by the matrix



A =
(
1.5  0  0      0
0    10  0 -1
0  0     0  0
0  0  0 1.5
)

This transformation can be done with a program such as following.

(1)Transformation of a figure

100 SET WINDOW -20,20,0,40 
110 DRAW grid0                           ! One box is = 1m×1m
120 DIM m(4,4)                         
130 DATA 1.5,  0,  0,   0
140 DATA   0, 10,  0,  -1
150 DATA   0,  0,  0,   0
160 DATA   0,  0,  0, 1.5    
170 MAT READ m                          
180 DRAW HalfHouse WITH m                
190 PICTURE HalfHouse
200    DRAW house WITH SCALE(0.5)
210 END  PICTURE
220 PICTURE House   
230    SET AREA COLOR 15                           
240    PLOT AREA:  0,1; 0,0; 2,0; 2,1                            
250    SET AREA COLOR 2                   
260    PLOT AREA: -0.6,1; 2.6,1; 2,2; 0,2              
270    SET AREA COLOR 10
280    PLOT AREA: 0.1,0; 0.1,0.8; 0.5,0.8; 0.5,0                       
290    SET AREA COLOR 5
300    PLOT AREA: 1.4,0.4; 1.9,0.4; 1.9,0.8; 1.4,0.8     
310    SET AREA COLOR 12
320    PLOT AREA: 1.7,2; 1.7,2.3; 1.5,2.3; 1.5,2            
330 END PICTURE
340 END


(2)Transformation of an Image
This program handles letters as an image and make it an object of transformation.
This program needs Decimal BASIC 7.4.2 or later.

100 SET WINDOW -10,10,0,20   
110 DRAW grid0                      ! One box is 1m×1m
120 DIM m(4,4)                   
130 DATA 1.5,  0,  0,   0
140 DATA   0, 10,  0,  -1
150 DATA   0,  0,  0,   0
160 DATA   0,  0,  0, 1.5    
170 MAT READ m                          
180 DRAW pict1 WITH m    
190 PICTURE pict1
200    SET TEXT HEIGHT 0.8               
210    PLOT TEXT ,AT -1,0:"BASIC"    
220 END PICTURE
230 END



Example 2.

When we view far away, objects are seen smaller as they are placed more remotely.
This is a converse relation to Example 1.
Assume the height of the viewpoint be 1.5m.
When we copy down the scenery onto the screen placed vertically at the ground 0.5m ahead, Any point (x, y) on the ground will be mapped to the point (x', y') on the screen determined by the following formula, where the origin is placed at the point where the center of the screen and the ground come in contact.

y' =    1.5y     =    (1.5/0.5)y  
0.5 + y 1 + y / 0.5
x' =    0.5x     =         x      
0.5 + y 1+ y / 0.5

Hence, this transformation (x, y) → (x', y') is a projective transformation determined by a matrix



A =
(
0.5  0  0      0
0    1.5  0  1
0  0     0  0
0  0  0 1.5
)

This transformation can be done with a program such as following.

100 DIM a(4,4)
110 DATA 0.5,   0,  0,   0
120 DATA   0, 1.5,  0,   1
130 DATA   0,   0,  0,   0
140 DATA   0,   0,  0, 0.5
150 MAT READ a
160 SET WINDOW -1,1,0,2             
170 DRAW pict1 WITH a             
180 PICTURE pict1
190    SET TEXT HEIGHT 0.8               
200    PLOT TEXT ,AT -1,0:"BASIC"   ! The object to be transformed is a text.
210 END PICTURE
220 END


The nature of this transformation may be seen more precisely when we draw a grid on the ground.

100 SET WINDOW -1,1,0,2    
110 DIM m(4,4)                       
120 DATA 0.5,   0,  0,   0
130 DATA   0, 1.5,  0,   1
140 DATA   0,   0,  0,   0
150 DATA   0,   0,  0, 0.5
160 MAT READ m
170 DRAW pict1 WITH m               
180 PICTURE pict1
190    FOR y=0 TO 10
200       PLOT LINES:-100,y; 100,y
210    NEXT y
220    FOR x=-5 TO 5
230       PLOT LINES:x,0;x,1000
240    NEXT x
250 END PICTURE
260 END 


The following program draws a grid slanted by 45°.

100 SET WINDOW -1,1,0,2         
110 DIM m(4,4)                      
120 DATA 0.5,   0,  0,   0
130 DATA   0, 1.5,  0,   1
140 DATA   0,   0,  0,   0
150 DATA   0,   0,  0, 0.5
160 MAT READ m
170 DRAW pict1 WITH m             
180 PICTURE pict1
190    FOR x=-10 TO 10
200       PLOT LINES:x,0; 50+x,50
210    NEXT x
220    FOR x=-10 TO 10
230       PLOT LINES:x,0;-50+x,50
240    NEXT x
250 END PICTURE
260 END 


Example 3.

We want to modify a picture of a vertical wall taken with elevation angle θ as if it was taken with no elevation angle.
The image taken from the point A in the figure comes out as if it exists on the bold line in the figure, where the figure is a side view.
We assumes the coordinates on the photo image with the x-axis lateral and the y-axis longitudinal, and the coordinates on the vertical wall with the x-axis the intersection with the horizontal plane and the y-axis vertical.
We want to find the formula from the coordinates (x, y) on the virtual image to the actual point (x', y') on the vertical wall.
Letting AO be d in the side view figure, we have
y' : y cosθ = OP' : QP = AO : AQ = d : d - y sinθ = 1 : 1 - y(sinθ / d)
and hence

y' =     y cosθ   
1 - y (sinθ / d)


With the top view figure, we have
x' : x = AO : AQ = d : d - y sinθ,
and hence

x' =       x     
1 - y (sinθ / d)


Therefore, this transformation is a projective transformation determined by a matrix



A =
(
1  0  1      0
0    cosθ   0 -(sinθ)/d
0  1     0  0
0  0  0  1
)


Letting α be the half of the vertical angle of view, b be the height of the photo image (=BC), we have
AH tanα = b / 2,   d cosθ = AH,
and hence

d =   b   1    
2  cosθ tanα

Furthermore, Letting OH=c, we have c = d sinθ.


This transformation can be done with a program such as following.
We assumed the width of the original image to be a, the height to be b, and the horizon to be the x-axis.

100 OPTION ARITHMETIC NATIVE
110 OPTION ANGLE DEGREES
120 LET t=16                      ! Elevation angle
130 LET v=33                      ! The half of the vertical angle of view
140 SET COLOR MODE "NATIVE"       
150 GLOAD "tokeidai.JPG" 
160 ASK PIXEL SIZE (0,0; 1,1) a,b   
170 DIM p(a,b)                      
180 ASK PIXEL ARRAY (0,1) p         
190 SET BITMAP SIZE 401,401         
200 CLEAR                           
210 SET WINDOW -200, 200, -90, 310    
220 DRAW grid(100,100)                  
230 LET d=b/2/TAN(v)/COS(t)
240 LET c=d*SIN(t) 
250 DIM m(4,4)                        
260 MAT m=IDN
270 LET m(2,2)=COS(t)
280 LET m(2,4)=-SIN(t)/d
290 DRAW pict1 WITH m                  
300 PICTURE pict1
310    MAT PLOT CELLS, IN  -a/2 + 1, c+b/2 ; a/2  , c-b/2 + 1 : p
320 END PICTURE
330 END 

 
     Original                     Transformed

gratitude The original figure was taken from sozai-free.com


Back