ece 661 computer vision homework 2 - purdue university · in order to output the rgb value array...

34
ECE 661 Computer Vision Homework 2 Yandong Guo School of Electrical Engineering [email protected] September 6, 2012 1 Problem The projective distorted image pairs are given and the projective distortion need to be removed. After this, the similarity transformation need to estimated within each of the corrected (metric) image pairs. Base on the similarity transformation estimation, the image alignment is performed. The corresponding points are marked manually. 2 Solution 2.1 Homography calculation The homogeneous coordinate for a dot in the world plane is denoted by x w R 3×1 , while the homogeneous coordinate for a dot in the image plane is denoted by x i R 3×1 . Their correlation is modeled using the following formula. x i = h 11 h 12 h 13 h 21 h 21 h 23 h 31 h 31 h 33 x w (1) Where the transformation matrix H is called projective transformation matrix. Since H has 8 degrees of freedom, we need at least 4 points to calculate H . Assuming that we have 4 points, and for the k th point we have its homogeneous coordinate (x w k ,y w k ,a w k ) T in the world plane and (x i k ,y i k ,a i k ) T in the image plane, then we have the following equations. x i k = x w k h 11 + y w k h 12 + h 13 x i k x w k h 31 x i k y w k h 32 y i k = x w k h 21 + y w k h 22 + h 23 y i k x w k h 31 y i k y w k h 32 (2) With all the 4 points we can get eight equations and can solve the value of matrix H . 1

Upload: others

Post on 21-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

  • ECE 661 Computer VisionHomework 2

    Yandong GuoSchool of Electrical Engineering

    [email protected]

    September 6, 2012

    1 Problem

    The projective distorted image pairs are given and the projective distortion need to beremoved. After this, the similarity transformation need to estimated within each of thecorrected (metric) image pairs. Base on the similarity transformation estimation, the imagealignment is performed. The corresponding points are marked manually.

    2 Solution

    2.1 Homography calculation

    The homogeneous coordinate for a dot in the world plane is denoted by xw ∈ R3×1, whilethe homogeneous coordinate for a dot in the image plane is denoted by xi ∈ R3×1. Theircorrelation is modeled using the following formula.

    xi =

    ⎛⎝

    h11 h12 h13h21 h21 h23h31 h31 h33

    ⎞⎠xw (1)

    Where the transformation matrix H is called projective transformation matrix. Since Hhas 8 degrees of freedom, we need at least 4 points to calculate H. Assuming that we have 4points, and for the kth point we have its homogeneous coordinate (xwk , y

    wk , a

    wk )

    T in the worldplane and (xik, y

    ik, a

    ik)

    T in the image plane, then we have the following equations.

    xik = xwk h11 + y

    wk h12 + h13 − xikxwk h31 − xikywk h32

    yik = xwk h21 + y

    wk h22 + h23 − yikxwk h31 − yikywk h32 (2)

    With all the 4 points we can get eight equations and can solve the value of matrix H.

    1

  • 2.2 Projective distortion removal

    The corrected images (metric image) are obtained based on the projective transformation H.First of all, we get an array of rgb values for all the points in the world plane. For each of thepoint in the world plane, we can calculate its corresponding coordinate in the image planeby E.q. (1). Then the rgb value of the point in the world plane is set to be the rgb value atthe point’s corresponding coordinate in the image. Since the corresponding coordinate maynot be integer, the bilinear interpolation is used to calculate the rgb value of the point.

    In order to output the rgb value array into a corrected image, we initialize an image(corrected image) of which the size is the same as the array. Then the rgb values of thecorrected image are assigned by one to one mapping from the array to the image.

    The results are shown in the experiment section.

    2.3 Similarity transform estimation

    The similarity transformation matrix has 4 degrees of freedom.

    xo =

    ⎛⎝

    a −b h13b a h230 0 1

    ⎞⎠xi (3)

    Therefore, we use 2 points to calculate the matrix. The equations based on the kth pointare

    xok = xika− yikb+ h13

    yok = xikb+ y

    ika+ h23 . (4)

    The similarity transformation matrix estimation results are shown in the second subsec-tion of the experiment section.

    2.4 Similar image alignment

    The image alignment is achieved by the same strategy discussed in the previous subsectionbut the similarity transformation matrix is used. The image alignment results are shown inthe second subsection of the experiment section.

    2

  • 3 Experiment result

    3.1 Projective distortion removal

    3.2 Similarity transformation estimation and image alignment

    The similarity transformation from the corrected adams1.jpg to adams2.jpg is

    ⎛⎝

    0.64 0 16.790 0.64 39.290 0 1

    ⎞⎠

    The similarity transformation from the corrected board1.jpg to board2.jpg is

    ⎛⎝

    1.40 0 −508.000 1.40 −283.000 0 1

    ⎞⎠

    The similarity transformation from the corrected door1.jpg to door2.jpg is

    ⎛⎝

    0.62 0 41.970 0.62 137.470 0 1

    ⎞⎠

    The similarity transformation from the corrected tree1.jpg to tree2.jpg is

    ⎛⎝

    1.53 0 −51.110 1.53 −42.650 0 1

    ⎞⎠

    The similarity transformation from the corrected marker1.jpg to marker2.jpg is

    ⎛⎝

    0.96 0 −489.180 0.96 −103.470 0 1

    ⎞⎠

    4 Source code

    Listing 1: test.c

    #include#include#include

    #include ” p r o j e c t i o n e s t ima t i o n . h”#include ” image backpro j ec t i on . h”

    3

  • void mat pr in t f (CvMat ∗mat , int w, int h , int i n t e g e r ){

    int i , j ;i f ( i n t e g e r == 0){

    for ( i = 0 ; i < h ; i++){

    for ( j = 0 ; j < w; j++){

    p r i n t f ( ”%.3 f \ t ” , cvmGet (mat , i , j ) ) ;}p r i n t f ( ”\n” ) ;

    }}

    }

    int main ( int argc , char∗ argv [ ] ){

    char ∗ input img1 name ;char ∗ input img2 name ;char ∗output img1 name ;char ∗output img2 name ;IplImage ∗ input img1 ;IplImage ∗ input img2 ;IplImage ∗output img1 ;IplImage ∗output img2 ;IplImage ∗merge img12 ;FILE ∗ po i n t s c o o rd i na t e ;

    CvPoint2D32f pt s input img1 [ 4 ] ;CvPoint2D32f pt s input img2 [ 4 ] ;CvPoint2D32f pt s wor ld [ 4 ] ;CvPoint2D32f pts s im img1 [ 4 ] ;CvPoint2D32f pts s im img2 [ 4 ] ;

    CvMat ∗Hmat1 , ∗Hmat2 , ∗Hmats ;

    int i , j , k ;

    /∗ I n i t i a l i z a t i o n ∗/p r i n t f ( ”∗∗∗∗∗∗∗ I n i t i a l i z a t i o n ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗\n” ) ;p r i n t f ( ”\ tLoad the data and a l l o c a t e the memory\n” ) ;i f ( argc < 4){

    p r i n t f ( ” Please s p e c i f y the image name and c o r r e l a t e d

    4

  • po int coo rd ina t e s in the argument .\n” ) ;return 1 ;

    } else{

    input img1 name = argv [ 1 ] ;i f ( ( input img1 = cvLoadImage ( input img1 name , 1) ) ==0){

    p r i n t f ( ”Could not open or f i nd the image 1\n” ) ;return 2 ;

    }input img2 name = argv [ 2 ] ;i f ( ( input img2 = cvLoadImage ( input img2 name , 1) ) ==0){

    p r i n t f ( ”Could not open or f i nd the image 2\n” ) ;return 2 ;

    }i f ( argc == 4){

    po i n t s c o o rd i na t e = fopen ( argv [ 3 ] , ” r ” ) ;f s c a n f ( po in t s coo rd ina t e , ”%f , %f , %f , %f , %f , %

    f , %f , %f \n” ,&(pts input img1 [ 0 ] . x ) , &(

    pt s input img1 [ 0 ] . y ) , &(pt s input img1 [ 1 ] . x ) , &(pt s input img1 [ 1 ] . y ) ,

    &(pt s input img1 [ 2 ] . x ) , &(pt s input img1 [ 2 ] . y ) , &(pt s input img1 [ 3 ] . x ) , &(pt s input img1 [ 3 ] . y ) ) ;

    f s c a n f ( po in t s coo rd ina t e , ”%f , %f , %f , %f , %f , %f , %f , %f \n” ,

    &(pts input img2 [ 0 ] . x ) , &(pt s input img2 [ 0 ] . y ) , &(pt s input img2 [ 1 ] . x ) , &(pt s input img2 [ 1 ] . y ) ,

    &(pt s input img2 [ 2 ] . x ) , &(pt s input img2 [ 2 ] . y ) , &(pt s input img2 [ 3 ] . x ) , &(pt s input img2 [ 3 ] . y ) ) ;

    f s c a n f ( po in t s coo rd ina t e , ”%f , %f , %f , %f , %f , %f , %f , %f \n” ,

    &(pts wor ld [ 0 ] . x ) , &(pts wor ld [ 0 ] . y ) ,&(pts wor ld [ 1 ] . x ) , &(pts wor ld [ 1 ] . y ),

    &(pts wor ld [ 2 ] . x ) , &(pts wor ld [ 2 ] . y ) ,&(pts wor ld [ 3 ] . x ) , &(pts wor ld [ 3 ] . y )) ;

    5

  • }}

    Hmat1 = cvCreateMat (3 , 3 , CV 64FC1) ;Hmat2 = cvCreateMat (3 , 3 , CV 64FC1) ;Hmats = cvCreateMat (3 , 3 , CV 64FC1) ;

    p r i n t f ( ”\n” ) ;/∗TOPIC I : P ro j e c t i v e d i s t o r t i o n removal ∗/p r i n t f ( ”∗∗∗∗∗∗∗ Pro j e c t i v e d i s t o r t i o n removal ∗∗∗∗∗∗∗∗∗∗∗∗∗\n” ) ;

    p r i n t f ( ”\tThe f i r s t image\n” ) ;p r i n t f ( ”\ tStep 1 : Homography Ca l cu l a t i on \n” ) ;p r o j e c t i o n e s t ima t i o n (Hmat1 , pts input img1 , pts wor ld , 4) ;p r i n t f ( ”\ tStep 2 : Corrected image render ing \n” ) ;output img1 = image backpro j ec t i on ( input img1 , Hmat1) ;p r i n t f ( ”\ tStep 3 : Output image sav ing \n” ) ;cvSaveImage ( ” c o r r e c t ed 1 . png” , output img1 ) ;p r i n t f ( ”\ tSucceed !\n” ) ;

    p r i n t f ( ”\n” ) ;p r i n t f ( ”\tThe second image\n” ) ;p r i n t f ( ”\ tStep 1 : Homography Ca l cu l a t i on \n” ) ;p r o j e c t i o n e s t ima t i o n (Hmat2 , pts input img2 , pts wor ld , 4) ;p r i n t f ( ”\ tStep 2 : Corrected image render ing \n” ) ;output img2 = image backpro j ec t i on ( input img2 , Hmat2) ;p r i n t f ( ”\ tStep 3 : Output image sav ing \n” ) ;cvSaveImage ( ” c o r r e c t ed 2 . png” , output img2 ) ;p r i n t f ( ”\ tSucceed !\n” ) ;

    /∗TOPIC I I : S im i l a r i t y e s t ima t ion and image a l ignment ∗/p r i n t f ( ”\n” ) ;p r i n t f ( ”∗∗∗∗∗∗∗ S im i l a r i t y trans form es t imat i on ∗∗∗∗∗∗∗∗∗∗∗∗∗\n” )

    ;

    p r i n t f ( ”\ tStep 1 : A l l o ca t e the a s s o c i a t ed po in t s \n” ) ;p o i n t p r o j e c t i o n (Hmat1 , pts s im img1 , pts input img1 , 4) ;p o i n t p r o j e c t i o n (Hmat2 , pts s im img2 , pts input img2 , 4) ;p r i n t f ( ”\ tStep 2 : S im i l a r i t y trans form c a l c u l a t i o n \n” ) ;p r o j e c t i o n e s t ima t i o n (Hmats , pts s im img1 , pts s im img2 , 2) ;

    mat pr in t f (Hmats , 3 , 3 , 0) ;

    merge img12= image merge ( output img1 , output img2 , Hmats ) ;cvSaveImage ( ”Merge . png” , merge img12 ) ;

    6

  • cvReleaseImage ( input img1 ) ;cvReleaseImage ( input img2 ) ;cvReleaseImage ( output img1 ) ;cvReleaseImage ( output img2 ) ;cvReleaseImage ( merge img12 ) ;

    f c l o s e ( po i n t s c o o rd i na t e ) ;cvReleaseMat (Hmat1) ;cvReleaseMat (Hmat2) ;cvReleaseMat (Hmats ) ;

    return 0 ;}

    Listing 2: projection estimation.c

    // HW c. cpp : Def ines the entry po in t f o r the conso l e a p p l i c a t i o n .//#include ” p r o j e c t i o n e s t ima t i o n . h”

    void p r o j e c t i o n e s t ima t i o n (CvMat ∗Hmat , CvPoint2D32f∗ pts input img ,CvPoint2D32f ∗pts wor ld , int po in t s no )

    {// suppor t two k inds o f p r o j e c t i o n sCvMat ∗Pmat , ∗pvec , ∗hvec ;

    pvec = cvCreateMat ( po in t s no ∗2 , 1 , CV 64FC1) ;hvec = cvCreateMat ( po in t s no ∗2 , 1 , CV 64FC1) ;

    i f ( po in t s no == 4){

    Pmat = cvCreateMat ( po in t s no ∗2 , 8 , CV 64FC1) ;

    l i n e a r s y s t em con s t r u c t i o n (Pmat , pvec , pts input img ,pts wor ld , po in t s no ) ;

    //ma t p r in t f (Pmat , 8 , 8 , 0) ; //Debug onlycvSolve (Pmat , pvec , hvec , CV LU) ;vec2matrix (Hmat , hvec , 3 , 3 , po in t s no ∗2) ;//ma t p r in t f (Hmat , 3 , 3 , 0) ; //Debug only

    } else i f ( po in t s no == 2){

    Pmat = cvCreateMat ( po in t s no ∗2 , 4 , CV 64FC1) ;

    l i n e a r s y s t em con s t r u c t i o n (Pmat , pvec , pts input img ,pts wor ld , po in t s no ) ;

    //ma t p r in t f (Pmat , 8 , 8 , 0) ; //Debug onlycvSolve (Pmat , pvec , hvec , CV LU) ;

    7

  • cvmSet (Hmat , 0 , 0 , cvmGet ( hvec , 0 , 0) ) ;cvmSet (Hmat , 0 , 1 , −cvmGet ( hvec , 1 , 0) ) ;cvmSet (Hmat , 0 , 2 , cvmGet ( hvec , 2 , 0) ) ;cvmSet (Hmat , 1 , 0 , cvmGet ( hvec , 1 , 0) ) ;cvmSet (Hmat , 1 , 1 , cvmGet ( hvec , 0 , 0) ) ;cvmSet (Hmat , 1 , 2 , cvmGet ( hvec , 3 , 0) ) ;cvmSet (Hmat , 2 , 0 , 0) ;cvmSet (Hmat , 2 , 1 , 0) ;cvmSet (Hmat , 2 , 2 , 1) ;

    } else{

    p r i n t f ( ”At t h i s moment we only support two kinds o ft rans f o rmat i ons : the gene ra l p r o j e c t i v a ands im i l a r i t y . \n” ) ;

    }}

    void l i n e a r s y s t em con s t r u c t i o n (CvMat ∗Pmat , CvMat ∗pvec , CvPoint2D32f ∗pts input img , CvPoint2D32f ∗pts wor ld , int po in t s no )

    {int i ;i f ( po in t s no == 4){

    for ( i = 0 ; i

  • cvmSet ( pvec , i ∗2 , 0 , p t s input img [ i ] . x ) ;cvmSet ( pvec , i ∗2+1 , 0 , p t s input img [ i ] . y ) ;

    }} else i f ( po in t s no == 2){

    for ( i = 0 ; i k ){

    // p r i n t f (” In t h i s case , we w i l l padding the r e s t o f desmatrix wi th number 1\n”) ;

    }for ( i = 0 ; i < m; i++){

    for ( j = 0 ; j < n ; j++){

    i f ( i ∗n+j < k ){

    cvmSet ( des , i , j , cvmGet ( src , i ∗n+j , 0)

    9

  • ) ;} else{

    cvmSet ( des , i , j , 1) ;}

    }}

    }

    void po i n t p r o j e c t i o n (CvMat ∗Hmat , CvPoint2D32f ∗pts output ,CvPoint2D32f ∗ pts input , int po in t s no )

    {int i ;CvMat ∗HC pts input ;CvMat ∗HC pts output ;CvMat ∗Hinv ;

    HC pts input = cvCreateMat (3 , 4 , CV 64FC1) ;HC pts output = cvCreateMat (3 , 4 , CV 64FC1) ;Hinv = cvCreateMat (3 , 3 , CV 64FC1) ;

    for ( i = 0 ; i

  • {int i , j , k , phy s i c a l h e i gh t , phys i ca l w id th ;double x , y , z ;double w min , w max , h min , h max , w tmp , h tmp ;double zoom rat io , a s p e c t r a t i o ;unsigned char rgb [ 3 ] ;IplImage ∗ co r r ec t ed img ;

    CvPoint2D32f coor po in t img ;CvMat ∗bound input img ;CvMat ∗ bound corrected img ;CvMat ∗Hinv ;

    bound input img = cvCreateMat (3 , 4 , CV 64FC1) ;bound corrected img = cvCreateMat (3 , 4 , CV 64FC1) ;Hinv = cvCreateMat (3 , 3 , CV 64FC1) ;

    cvmSet ( bound input img , 0 , 0 , 0) ;cvmSet ( bound input img , 1 , 0 , 0) ;cvmSet ( bound input img , 2 , 0 , 1) ;cvmSet ( bound input img , 0 , 1 , input img−>width−1) ;cvmSet ( bound input img , 1 , 1 , 0) ;cvmSet ( bound input img , 2 , 1 , 1) ;cvmSet ( bound input img , 0 , 2 , input img−>width−1) ;cvmSet ( bound input img , 1 , 2 , input img−>height −1) ;cvmSet ( bound input img , 2 , 2 , 1) ;cvmSet ( bound input img , 0 , 3 , 0) ;cvmSet ( bound input img , 1 , 3 , input img−>height −1) ;cvmSet ( bound input img , 2 , 3 , 1) ;

    cv Inver t (Hmat , Hinv , CV LU) ;cvMatMul (Hinv , bound input img , bound corrected img ) ;

    w min = 1e10 ;h min = 1e10 ;w max = 0 ;h max = 0 ;for ( i = 0 ; i

  • }i f (w tmp > w max){

    w max = w tmp ;}i f ( h tmp < h min ){

    h min = h tmp ;}i f ( h tmp > h max ){

    h max = h tmp ;}

    }

    a s p e c t r a t i o = (w max − w min ) /(h max − h min ) ;phy s i c a l h e i g h t = input img−>he ight ;phys i ca l w id th = phy s i c a l h e i g h t ∗ a s p e c t r a t i o ;zoom rat io = phy s i c a l h e i g h t /(h max − h min ) ;

    co r r e c t ed img = cvCreateImage ( cvS i z e ( phys i ca l w idth ,phy s i c a l h e i g h t ) , IPL DEPTH 8U , 3) ;

    cvmSet (Hmat , 0 , 2 , (w min ∗ cvmGet (Hmat , 0 , 0 ) + h min ∗ cvmGet (Hmat , 0 , 1 ) + cvmGet (Hmat , 0 , 2 ) ) ∗ zoom rat io ) ;

    cvmSet (Hmat , 1 , 2 , (w min ∗ cvmGet (Hmat , 1 , 0 ) + h min ∗ cvmGet (Hmat , 1 , 1 ) + cvmGet (Hmat , 1 , 2 ) ) ∗ zoom rat io ) ;

    cvmSet (Hmat , 2 , 2 , (w min ∗ cvmGet (Hmat , 2 , 0 ) + h min ∗ cvmGet (Hmat , 2 , 1 ) + cvmGet (Hmat , 2 , 2 ) ) ∗ zoom rat io ) ;

    for ( i = 0 ; iwidth −1 | | coor po in t img . y < 0 | |coor po in t img . y> input img−>height −1)

    12

  • {for ( k=0;kimageData + i ∗corrected img−>widthStep ) ) [ j∗3+k ] = 127 ;

    }} else{

    ima g e i n t e r p o l a t i o n c h 3 b i l i n e a r (input img , coor po int img , rgb ) ;

    for ( k=0;kimageData + i ∗corrected img−>widthStep ) ) [ j∗3+k ] = rgb [ k ] ;

    }

    }

    }}

    return co r r ec t ed img ;

    }

    void ima g e i n t e r p o l a t i o n c h 3 b i l i n e a r ( IplImage ∗ img , CvPoint2D32f coor ,unsigned char ∗ value )

    {int x , y , k ;double fx , fy ;x = ( int ) coor . x ;y = ( int ) coor . y ;fx = coor . x − x ;fy = coor . y − y ;

    for ( k = 0 ; k

  • ∗( va lue+k) += (1 . 0 −fx ) ∗ ( 1 . 0 − fy )∗ ( (unsigned char ∗) ( img−>imageData + img−>

    widthStep ∗ y ) ) [ x∗3+k ] ;∗( va lue+k) += ( fx ) ∗ ( 1 . 0 − fy )

    ∗ ( (unsigned char ∗) ( img−>imageData + img−>widthStep ∗ y ) ) [ ( x+1)∗3+k ] ;

    ∗( va lue+k) += (1 . 0 −fx ) ∗ ( fy )∗ ( (unsigned char ∗) ( img−>imageData + img−>

    widthStep ∗ ( y+1) ) ) [ x∗3+k ] ;∗( va lue+k) += ( fx ) ∗ ( fy )

    ∗ ( (unsigned char ∗) ( img−>imageData + img−>widthStep ∗ ( y+1) ) ) [ ( x+1)∗3+k ] ;

    }}

    IplImage ∗ image merge ( IplImage ∗ input img1 , IplImage ∗ input img2 ,CvMat ∗Hmat)

    {int i , j , k , a l i gned he i gh t , a l i gned he i gh t2 , a l i gned width ;double x , y , z ;double w min , w max , h min , h max , w tmp , h tmp ;int w o , h o ;double zoom rat io , a s p e c t r a t i o ;unsigned char rgb [ 3 ] ;IplImage ∗ a l i gned imgs ;

    CvPoint2D32f coor po in t img ;CvMat ∗bound input img ;CvMat ∗ bound corrected img ;CvMat ∗Hinv ;

    bound input img = cvCreateMat (3 , 4 , CV 64FC1) ;bound corrected img = cvCreateMat (3 , 4 , CV 64FC1) ;Hinv = cvCreateMat (3 , 3 , CV 64FC1) ;

    cvmSet ( bound input img , 0 , 0 , 0) ;cvmSet ( bound input img , 1 , 0 , 0) ;cvmSet ( bound input img , 2 , 0 , 1) ;cvmSet ( bound input img , 0 , 1 , input img1−>width−1) ;

    14

  • cvmSet ( bound input img , 1 , 1 , 0) ;cvmSet ( bound input img , 2 , 1 , 1) ;cvmSet ( bound input img , 0 , 2 , input img1−>width−1) ;cvmSet ( bound input img , 1 , 2 , input img1−>height −1) ;cvmSet ( bound input img , 2 , 2 , 1) ;cvmSet ( bound input img , 0 , 3 , 0) ;cvmSet ( bound input img , 1 , 3 , input img1−>height −1) ;cvmSet ( bound input img , 2 , 3 , 1) ;

    cv Inver t (Hmat , Hinv , CV LU) ;cvMatMul (Hinv , bound input img , bound corrected img ) ;

    w min = 1e10 ;h min = 1e10 ;w max = 0 ;h max = 0 ;

    for ( i = 0 ; i w max){

    w max = w tmp ;}i f ( h tmp < h min ){

    h min = h tmp ;}i f ( h tmp > h max ){

    h max = h tmp ;}

    }

    // a s p e c t r a t i o = (w max − w min) /( h max − h min ) ; //Got to beone

    // p h y s i c a l h e i g h t = ( h max − h min ) ;// ph y s i c a l w i d t h = p h y s i c a l h e i g h t ∗ a s p e c t r a t i o ;// zoom rat io = p h y s i c a l h e i g h t /( h max − h min ) ; //Got to be one

    15

  • , again ˜ !

    i f (w min = input img2−>width ){

    a l i gned width = w max − w min ;} else i f (w min width ){

    a l i gned width = input img2−>width − w min ;} else i f (w min > 0 && w max >= input img2−>width ){

    a l i gned width = w max − 0 ;} else i f (w min > 0 && w max < input img2−>width ){

    a l i gned width = input img2−>width − 0 ;}

    i f ( h min = input img2−>he ight ){

    a l i g n ed h e i gh t = h max − h min ;} else i f ( h min he ight ){

    a l i g n ed h e i gh t = input img2−>he ight − h min ;} else i f ( h min > 0 && h max >= input img2−>he ight ){

    a l i g n ed h e i gh t = h max − 0 ;} else i f ( h min > 0 && h max < input img2−>he ight ){

    a l i g n ed h e i gh t = input img2−>he ight − 0 ;}

    a l i gn ed he i gh t 2 = a l i gn ed h e i gh t ∗ 2 ;

    a l i gned imgs = cvCreateImage ( cvS i ze ( a l igned width ,a l i gn ed he i gh t 2 ) , IPL DEPTH 8U , 3) ;

    cvmSet (Hmat , 0 , 2 , (w min ∗ cvmGet (Hmat , 0 , 0 ) + h min ∗ cvmGet (Hmat , 0 , 1 ) + cvmGet (Hmat , 0 , 2 ) ) ) ;

    cvmSet (Hmat , 1 , 2 , (w min ∗ cvmGet (Hmat , 1 , 0 ) + h min ∗ cvmGet (Hmat , 1 , 1 ) + cvmGet (Hmat , 1 , 2 ) ) ) ;

    cvmSet (Hmat , 2 , 2 , (w min ∗ cvmGet (Hmat , 2 , 0 ) + h min ∗ cvmGet (Hmat , 2 , 1 ) + cvmGet (Hmat , 2 , 2 ) ) ) ;

    i f ( h min < 0){

    h o = 0 ;} else{

    16

  • h o = h min ;}

    i f (w min < 0){

    w o = 0 ;} else{

    w o = w min ;}

    for ( i = 0 ; iwidth −1 | | coor po in t img . y < 0| | coor po in t img . y> input img1−>height −1)

    {for ( k=0;kimageData + ( i+h o ) ∗a l igned imgs−>widthStep ) ) [ ( j+w o )∗3+k ] = 127 ;

    }} else{

    ima g e i n t e r p o l a t i o n c h 3 b i l i n e a r (input img1 , coor po int img , rgb ) ;

    for ( k=0;kimageData + ( i+h o ) ∗a l igned imgs−>widthStep ) ) [ ( j+w o )∗3+k ] = rgb [ k ] ;

    17

  • }

    }

    }}

    i f ( h min < 0){

    h o = −h min ;} else{

    h o = 0 ;}

    i f (w min < 0){

    w o = 0 − w min ;} else{

    w o = 0 ;}

    for ( i = 0 ; ihe ight ; i ++){

    for ( j = 0 ; jwidth ; j++){

    for ( k=0;kimageData + ( i+h o+a l i g n ed h e i gh t ) ∗a l igned imgs−>widthStep ) ) [ ( j+w o )∗3+k ]

    = ( (unsigned char ∗) ( input img2−>imageData + i ∗ input img2−>widthStep )) [ j ∗3+k ] ;

    }}

    }

    return a l i gned imgs ;

    }

    void g r ay image i n t e rpo l a t i on ( IplImage ∗ img , CvPoint2D32f coor ,

    18

  • unsigned char ∗ value ){

    int x , y ;x = ( int ) coor . x ;y = ( int ) coor . y ;

    ∗ value = ( (unsigned char ∗) ( img−>imageData + img−>widthStep∗ y ) ) [ x∗3+0] ;

    ∗( va lue+1) = ( (unsigned char ∗) ( img−>imageData + img−>widthStep∗ y ) ) [ x∗3+1] ;

    ∗( va lue+2) = ( (unsigned char ∗) ( img−>imageData + img−>widthStep∗ y ) ) [ x∗3+2] ;

    }

    19

  • (a) The projective distorted image with dots

    (b) The corrected image

    Figure 1: The experimental results for the test image adams1.jpg.

    20

  • (a) The projective distorted image with dots

    (b) The corrected image

    Figure 2: The experimental results for the test image adams2.jpg.

    21

  • (a) The projective distorted image with dots

    (b) The corrected image

    Figure 3: The experimental results for the test image board1.jpg.

    22

  • (a) The projective distorted image with dots

    (b) The corrected image

    Figure 4: The experimental results for the test image board2.jpg.

    23

  • (a) The projective distorted image with dots

    (b) The corrected image

    Figure 5: The experimental results for the test image door1.jpg.

    24

  • (a) The projective distorted image with dots

    (b) The corrected image

    Figure 6: The experimental results for the test image door2.jpg.

    25

  • (a) The projective distorted image with dots

    (b) The corrected image

    Figure 7: The experimental results for the test image tree1.jpg.

    26

  • (a) The projective distorted image with dots

    (b) The corrected image

    Figure 8: The experimental results for the test image tree2.jpg.

    27

  • (a) The projective distorted image with dots

    (b) The corrected image

    Figure 9: The experimental results for the test image marker1.jpg.

    28

  • (a) The projective distorted image with dots

    (b) The corrected image

    Figure 10: The experimental results for the test image marker2.jpg.

    29

  • Figure 11: The results for the test images adams1.jpg and adams2.jpg.

    30

  • Figure 12: The results for the test images board1.jpg and board2.jpg.

    31

  • Figure 13: The results for the test images door1.jpg and door2.jpg.

    32

  • Figure 14: The results for the test images tree1.jpg and tree2.jpg.

    33

  • Figure 15: The results for the test images marker1.jpg and marker2.jpg.

    34