Professional Documents
Culture Documents
September 6, 2012
ximage = H.xworld
⎡ i⎤ ⎡ ⎤ ⎡ w⎤
x1 h11 h12 h13 x1
⎣ y1 i ⎦ = ⎣h21 h22 h23 ⎦ ⎣ y1 w ⎦
z1 i h31 h32 1 1
i i
where ( xz11i , yz11 i ) gives the actual coordinates of the point in 2D image
plane.
Given a point x in the world plane, we can easily identify its counter-
part on the image plane (xi ). Further solving the above equation we
end up with.,
x1 i = h11 x1 w + h12 y1 w + h13
y1 i = h21 x1 w + h22 y1 w + h23
z1 i = h31 x1 w + h32 y1 w + 1
1
These set of equations can be written in form of Ax = b where A is a
coefficient matrix (8x8), x consists of the variables and b(8x1) contains
the constant terms.
⎡ ⎤ ⎡ ⎤ ⎡ i⎤
x1 w y1 w 1 0 0 0 −x1 w x1 i −y1 w x1 i h11 x1
⎢ 0 0 0 x1 w y1 w 1 −x1 w y1 i −y1 y1 ⎥ ⎢h12 ⎥ ⎢ y1 i ⎥
w i ⎥ ⎢ ⎥ ⎢
⎢ w ⎥
⎢x2 y2 w 1 0 0 0 −x2 w x2 i −y2 w x2 i ⎥ ⎢h13 ⎥ ⎢x2 i ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢ 0 0 0 x2 w y2 w 1 −x2 w y2 i −y2 w y2 i ⎥ ⎢h21 ⎥ ⎢ y2 i ⎥
⎢ ⎥⎢ ⎥ = ⎢ ⎥
⎢x3 w y3 w −x3 w x3 i −y3 w x3 i ⎥ ⎢ ⎥ ⎢ i⎥
⎢ 1 0 0 0 ⎥ ⎢h22 ⎥ ⎢x3 ⎥
⎢ 0 x3 w y3 w −x3 w y3 i −y3 y3 ⎥ ⎢ ⎥ ⎥ ⎢ i⎥
⎢h23 ⎥ ⎢ y3 ⎥
0 0 1 w i
⎢
⎣x4 w y4 w 1 0 0 0 −x4 w x4 i −y4 w x4 i ⎦ ⎣h31 ⎦ ⎣x4 i ⎦
0 0 0 x4 w y4 w 1 −x4 w y4 i −y4 w y4 i h32 y4 i
Figure 1: Interpolation
2
The coordinates on the world plane are Real but the pixel coordinates
on the image plane are integers. So there can’t be an one to one
correspondence. To get around this predicament, we can use bilinear
interpolation to estimate the pixel value on world plane. Bilinear In-
terpolation works as follows, Suppose a point P is surrounded by the
points (x1 , y1 ), (x2 , y1 ), (x1 , y2 ), (x2 , y2 ) then the value of a function f
at the point P(x, y) is approximated from the values at the surrounding
points in 2D space.
3
the second image and overlapping it onto the latter image. This is
done by applying the transform matrix S on each and every pixel in
the first image to find the corresponding pixel in the second image.
Then the pixels of the first image are aligned with the second. This
gives an overlap effect with the second image on the back ground and
the first image on foreground.( I overlapped them by calculating the
mean of pixel values at each common pixel to retain the information
of both images).
4
4. Results: Projective Distortion Elimination
Adams1.jpg
Figure 3: output
5
Adams2.jpg
Figure 5: output
6
board1.jpg
Figure 7: output
7
board2.jpg
Figure 9: output
8
door1.jpg
9
door2.jpg
10
tree1.jpg
11
tree2.jpg
12
My pictures : Apartment door
13
My pictures : Box
14
My pictures : Box2
15
5. Results of similarity tranformation
Figure 24: input: Corrected adams2 Figure 25: input: Corrected adams1
16
Overlapped boards
Figure 27: input: Corrected board1 Figure 28: input: Corrected board2
17
Overlapped Doors
Figure 30: input: Corrected door2 Figure 31: input: Corrected door1
18
Overlapped Trees
Figure 33: input: Corrected tree1 Figure 34: input: Corrected tree2
19
Overlapped Boxs
Figure 36: input: Corrected box1 Figure 37: input: Corrected box2
20
Figure 38: Overlapped boxs
21
6. Source Code: Elimination of Projective Distortion (homog-
raphy.py)
#!/ u s r / b i n / python
import sys , o s
import cv
import numpy
d e f main ( ) :
w o r l d i n p u t = numpy . z e r o s ( 8 , f l o a t )
i m a g e i n p u t = numpy . z e r o s ( 8 , i n t )
p r i n t ”@Author : S . k a r t h i k badam”
i f l e n ( s y s . argv ) > 1 :
f i l e n a m e = s y s . argv [ 1 ]
im = cv . LoadImageM ( f i l e n a m e , cv .CV LOAD IMAGE UNCHANGED
)
i f im == 0 :
p r i n t ” e n t e r a v a l i d i m a g e f i l e path ”
exit ()
else :
p r i n t ” e n t e r a v a l i d i m a g e f i l e path ”
#r e a d i n p u t from u s e r
f o r i in range (4) :
p r i n t ” Enter t h e World c o o r d i n a t e x , y ” , i
w o r l d i n p u t [ 2 ∗ i ] , w o r l d i n p u t [ 2 ∗ i +1] = r a w i n p u t ( ) . s p l i t
(” ,”)
p r i n t ”The world c o o r d i n a t e s o f p o i n t ” , i , ” a r e ” ,
w o r l d i n p u t [ 2 ∗ i ] , ” , ” , w o r l d i n p u t [ 2 ∗ i +1]
p r i n t ” Enter t h e c o r r e s p o n d i n g Image c o o r d i n a t e ”
i m a g e i n p u t [ 2 ∗ i ] , i m a g e i n p u t [ 2 ∗ i +1] = r a w i n p u t ( ) . s p l i t
(” ,”)
p r i n t ”The image c o o r d i n a t e s o f p o i n t a r e ” , i m a g e i n p u t
[ 2 ∗ i ] , ” , ” , i m a g e i n p u t [ 2 ∗ i +1]
p r i n t ”−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−”
#c r e a t e t h e m a t r i c e s A and B t o s o l v e Ax = B
A m a t r i x a s v e c t o r = numpy . z e r o s ( 6 4 , f l o a t )
B v e c t o r = numpy . z e r o s ( 8 , f l o a t )
f o r i in range (4) :
#even rows i n terms o f i
A m a t r i x a s v e c t o r [ 1 6 ∗ i +0] = world input [2∗ i ]
A m a t r i x a s v e c t o r [ 1 6 ∗ i +1] = w o r l d i n p u t [ 2 ∗ i +1]
A m a t r i x a s v e c t o r [ 1 6 ∗ i +2] = 1
A m a t r i x a s v e c t o r [ 1 6 ∗ i +3] = 0
22
A matrix a s v e c t o r [ 1 6 ∗ i +4] = 0
A matrix a s v e c t o r [ 1 6 ∗ i +5] = 0
A matrix a s v e c t o r [ 1 6 ∗ i +6] = −i m a g e i n p u t [ 2 ∗ i ] ∗
world input [2∗ i ]
A matrix a s v e c t o r [ 1 6 ∗ i +7] = −i m a g e i n p u t [ 2 ∗ i ] ∗
world i n p u t [ 2 ∗ i +1]
#odd rows
A matrix as vector [8∗(2∗ i +1)+0] = 0
A matrix as vector [8∗(2∗ i +1)+1] = 0
A matrix as vector [8∗(2∗ i +1)+2] = 0
A matrix as vector [8∗(2∗ i +1)+3] = world input [2∗ i ]
A matrix as vector [8∗(2∗ i +1)+4] = w o r l d i n p u t [ 2 ∗ i +1]
A matrix as vector [8∗(2∗ i +1)+5] = 1
A matrix as vector [8∗(2∗ i +1)+6] = −i m a g e i n p u t [ 2 ∗ i +1]∗
world input [2∗ i ]
A matrix as vector [8∗(2∗ i +1)+7] = −i m a g e i n p u t [ 2 ∗ i +1]∗
world i n p u t [ 2 ∗ i +1]
#f i l l vector B
B vector [2∗ i ] = image input [2∗ i ]
B v e c t o r [ 2 ∗ i +1] = i m a g e i n p u t [ 2 ∗ i +1]
#S o l v e I n p u t t o g e t Homography H
A = cv . CreateMatHeader ( 8 , 8 , cv . CV 64FC1 )
cv . SetData (A, A m a t r i x a s v e c t o r , cv .CV AUTOSTEP)
B = cv . CreateMatHeader ( 8 , 1 , cv . CV 64FC1 )
cv . SetData (B, B v e c t o r , cv .CV AUTOSTEP)
x = cv . CreateMat ( 8 , 1 , cv . CV 64FC1 )
cv . S o l v e (A, B, x , cv . CV LU)
H = cv . CreateMat ( 3 , 3 , cv . CV 64FC1 )
f o r i in range (3) :
f o r j in range (3) :
i f ( i ∗3+ j ) ! = 8 :
cv . mSet (H, i , j , cv . mGet( x , i ∗3 + j , 0 ) )
else :
cv . mSet (H, 2 , 2 , 1 )
H i n v e r s e = cv . CreateMat ( 3 , 3 , cv . CV 64FC1 )
cv . I n v e r t (H, H i n v e r s e , cv . CV LU)
#p r i n t i n g t h e H Matrix
t e m p a r r a y = numpy . a s a r r a y (H [ : , : ] )
p r i n t ”Homography H i s : ”
23
p r i n t temp array
cv . MatMul ( H i n v e r s e , b o u n d a r i e s i m a g e , b o u n d a r i e s w o r l d )
x min = 1 e10
y min = 1 e10
x max = 0
y max = 0
f o r i in range (4) :
x = cv . mGet( b o u n d a r i e s w o r l d , 0 , i ) / cv . mGet(
boundaries world , 2 , i )
y = cv . mGet( b o u n d a r i e s w o r l d , 1 , i ) / cv . mGet(
boundaries world , 2 , i )
i f x < x min :
x min = x
i f x > x max :
x max = x
i f y < y min :
y min = y
i f y > y max :
y max = y
#s c a l e
s c a l e = f l o a t ( im . width / ( x max−x min ) )
f i n a l h e i g h t = i n t ( ( y max−y min ) ∗ s c a l e )
24
# f i l l t h e image u s i n g i n t e p o l a t i o n
w o r l d c o o r d i n a t e s = cv . CreateMat ( 3 , 1 , cv . CV 64FC1 )
i m a g e c o o r d i n a t e s = cv . CreateMat ( 3 , 1 , cv . CV 64FC1 )
cv . mSet ( w o r l d c o o r d i n a t e s , 2 , 0 , 1 )
x = cv . mGet( i m a g e c o o r d i n a t e s , 0 , 0 ) / cv . mGet(
image coordinates , 2 , 0)
y = cv . mGet( i m a g e c o o r d i n a t e s , 1 , 0 ) / cv . mGet(
image coordinates , 2 , 0)
temp = [ 0 , 0 , 0 ]
f o r k in range (3) :
temp [ k ] = 0
temp [ k ] = temp [ k ]+( x−i n t ( x ) ) ∗ ( y−i n t ( y ) ) ∗ ( im [ i n t ( y
+1) , i n t ( x+1) ] [ k ] )
temp [ k ] = temp [ k ]+(1.0 −( x−i n t ( x ) ) ) ∗ ( y−i n t ( y ) ) ∗ ( im
[ i n t ( y+1) , i n t ( x ) ] [ k ] )
temp [ k ] = temp [ k ]+( x−i n t ( x ) ) ∗(1.0 −( y−i n t ( y ) ) ) ∗ ( im
[ i n t ( y ) , i n t ( x+1) ] [ k ] )
temp [ k ] = temp [ k ]+(1.0 −( x−i n t ( x ) ) ) ∗(1.0 −( y−i n t ( y )
) ) ∗ ( im [ i n t ( y ) , i n t ( x ) ] [ k ] )
#w r i t e image t o f i l e
cv . SaveImage ( ” f i n a l i m a g e . png ” , new image )
25
7. Source Code: Similarity transformation (similarity.py)
#!/ u s r / b i n / python
import sys , o s
import cv
import numpy
d e f main ( ) :
i m a g e 1 i n p u t = numpy . z e r o s ( 8 , i n t )
i m a g e 2 i n p u t = numpy . z e r o s ( 8 , i n t )
p r i n t ”@Author : S . k a r t h i k badam”
i f l e n ( s y s . argv ) > 1 :
f i l e n a m e 1 = s y s . argv [ 1 ]
im1 = cv . LoadImageM ( f i l e n a m e 1 , cv .
CV LOAD IMAGE UNCHANGED)
i f im1 == 0 :
p r i n t ” e n t e r a v a l i d i m a g e f i l e path ”
exit ()
f i l e n a m e 2 = s y s . argv [ 2 ]
im2 = cv . LoadImageM ( f i l e n a m e 2 , cv .
CV LOAD IMAGE UNCHANGED)
i f im2 == 0 :
p r i n t ” e n t e r a v a l i d i m a g e f i l e path ”
exit ()
else :
p r i n t ” e n t e r a v a l i d i m a g e f i l e path ”
#r e a d i n p u t from u s e r
p r i n t ” Enter t h e c o r r e s p o n d i n g Image 2 c o o r d i n a t e ”
i m a g e 2 i n p u t [ 2 ∗ i ] , i m a g e 2 i n p u t [ 2 ∗ i +1] = r a w i n p u t ( ) .
s p l i t (” ,”)
p r i n t ”The image c o o r d i n a t e s o f p o i n t a r e ” ,
i m a g e 2 i n p u t [ 2 ∗ i ] , ” , ” , i m a g e 2 i n p u t [ 2 ∗ i +1]
p r i n t ”−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−”
#c r e a t e t h e m a t r i c e s A and B t o s o l v e Ax = B
26
A m a t r i x a s v e c t o r = numpy . z e r o s ( 6 4 , f l o a t )
B v e c t o r = numpy . z e r o s ( 8 , f l o a t )
f o r i in range (4) :
#even rows i n terms o f i
A m a t r i x a s v e c t o r [ 1 6 ∗ i +0] = image1 input [2∗ i ]
A m a t r i x a s v e c t o r [ 1 6 ∗ i +1] = i m a g e 1 i n p u t [ 2 ∗ i +1]
A m a t r i x a s v e c t o r [ 1 6 ∗ i +2] = 1
A m a t r i x a s v e c t o r [ 1 6 ∗ i +3] = 0
A m a t r i x a s v e c t o r [ 1 6 ∗ i +4] = 0
A m a t r i x a s v e c t o r [ 1 6 ∗ i +5] = 0
A m a t r i x a s v e c t o r [ 1 6 ∗ i +6] = −i m a g e 2 i n p u t [ 2 ∗ i ] ∗
image1 input [2∗ i ]
A m a t r i x a s v e c t o r [ 1 6 ∗ i +7] = −i m a g e 2 i n p u t [ 2 ∗ i ] ∗
i m a g e 1 i n p u t [ 2 ∗ i +1]
#odd rows
#f i l l vector B
B vector [2∗ i ] = image2 input [2∗ i ]
B v e c t o r [ 2 ∗ i +1] = i m a g e 2 i n p u t [ 2 ∗ i +1]
#S o l v e I n p u t t o g e t S i m i l a r i t y t r a n s f o r m S
A = cv . CreateMatHeader ( 8 , 8 , cv . CV 64FC1 )
cv . SetData (A, A m a t r i x a s v e c t o r , cv .CV AUTOSTEP)
B = cv . CreateMatHeader ( 8 , 1 , cv . CV 64FC1 )
cv . SetData (B, B v e c t o r , cv .CV AUTOSTEP)
x = cv . CreateMat ( 8 , 1 , cv . CV 64FC1 )
cv . S o l v e (A, B, x , cv . CV LU)
S = cv . CreateMat ( 3 , 3 , cv . CV 64FC1 )
f o r i in range (3) :
f o r j in range (3) :
27
i f ( i ∗3+ j ) ! = 8 :
cv . mSet ( S , i , j , cv . mGet( x , i ∗3 + j , 0 ) )
else :
cv . mSet ( S , 2 , 2 , 1 )
#p r i n t i n g t h e S i m i l a r i t y Matrix
t e m p a r r a y = numpy . a s a r r a y ( S [ : , : ] )
print ” S i m i l a r i t y transform i s :”
p r i n t temp array
S i n v e r s e = cv . CreateMat ( 3 , 3 , cv . CV 64FC1 )
cv . I n v e r t ( S , S i n v e r s e , cv . CV LU)
#o v e r l a p both images
i m a g e 1 c o o r d i n a t e s = cv . CreateMat ( 3 , 1 , cv . CV 64FC1 )
i m a g e 2 c o o r d i n a t e s = cv . CreateMat ( 3 , 1 , cv . CV 64FC1 )
cv . mSet ( i m a g e 1 c o o r d i n a t e s , 2 , 0 , 1 )
f o r i i n r a n g e ( im1 . width ) :
cv . mSet ( i m a g e 1 c o o r d i n a t e s , 0 , 0 , i )
f o r j i n r a n g e ( im1 . h e i g h t ) :
cv . mSet ( i m a g e 1 c o o r d i n a t e s , 1 , 0 , j )
cv . MatMul ( S , i m a g e 1 c o o r d i n a t e s , i m a g e 2 c o o r d i n a t e s )
x = cv . mGet( i m a g e 2 c o o r d i n a t e s , 0 , 0 ) / cv . mGet(
image2 coordinates , 2 , 0)
y = cv . mGet( i m a g e 2 c o o r d i n a t e s , 1 , 0 ) / cv . mGet(
image2 coordinates , 2 , 0)
temp = [ 0 , 0 , 0 ]
f o r k in range (3) :
i f im2 [ i n t ( y ) , i n t ( x ) ] [ k ] != 0 and im1 [ j , i ] [ k ] !=
0:
temp [ k ] = ( im1 [ j , i ] [ k ] + im2 [ i n t ( y ) , i n t ( x ) ] [ k ] )
/2
else :
temp [ k ] = im1 [ j , i ] [ k ]
i f temp ! = [ 0 , 0 , 0 ] :
im2 [ i n t ( y ) , i n t ( x ) ] = temp
#w r i t e image t o f i l e
cv . SaveImage ( ” f i n a l i m a g e . png ” , im2 )
28
CV LOAD IMAGE UNCHANGED)
cv . ShowImage ( ” R e s u l t ” , im )
cv . WaitKey ( )
29