8/14/2019 affine similarity metric rectification with c codes
1/24
8/14/2019 affine similarity metric rectification with c codes
2/24
ECE661 HW2 Report Rong Zhang
the vanishing line will be mapped into the line at infinity l = (0, 0, 1)T. This can be verifiedas,
HT1 =
1 0 l1/l30 1 l2/l30 0 1/l
3
, (3)
and HT1 l = (0, 0, 1)T.
In summary, three steps should be applied,
1. choose two sets of image lines which are physically parallel,
2. find the vanishing line l = (l1, l2, l3)T using the above two set of lines,
3. form matrix H1 and apply H1 to camera images Xc, i.e., Xa = H1Xc, where Xa is theaffinely rectified image.
2.2 Remove Affine Distortion
Now we get the affinely rectified image Xa, we want to find the affine transform matrix
H2 =
A t0 1
, (4)
such that Xa = H2Xs, where Xs is the scene image in the real world.Suppose we have a pair of physically orthogonal lines, l m. Let l, m be the transformed
lines under affine transformation H2 (eg, l = HT2 l), i.e., lines l
, m are from the affinelyrectified image Xa. By orthogonality, we know that
(l1/l3, l2/l3)(m1/m3, m2/m3)T = 0, (5)
then
l1m1 + l2m2 = lTC
m = 0, (6)
where C
=
1 0 00 1 0
0 0 0
is the dual degenerate conic. Since C
= H2C
HT2 , we have
lT
C
m = lT
H2H12 C
HT
2 HT
2 m
= lT
C
m
= 0. (7)
2
8/14/2019 affine similarity metric rectification with c codes
3/24
ECE661 HW2 Report Rong Zhang
Therefore,
lTC
m = lTH2C
HT2 m (8)
= lT
A t0 1
I 00 1
AT 0tT 1
m
= lT
AAT 00 0
m.
We have
(l1, l
2)AAT(m1, m
2)T = 0. (9)
In order to get A, let S = AAT, where S =
s11 s12s12 1
because S is symmetric. Note that
the last element of S is set as 1 considering the scale problem.
(l1m
1, l
1m
2 + l
2m
1)
s11s12
= l2m2 (10)
Therefore, a pair of orthogonal lines provides a equation. We need two pairs of orthogonallines l(1) m(1) and l(2) m(2) to solve the matrix S which has two unknown parameters.Note that these two pairs of lines should be non-parallel, otherwise, the two equations willbe equivalent.
Since the symmetric matrix S can be written as S = UDUT, where U1 = UT, we canget A = U
DUT. Now, H2 is available and the restored scene image is X = H
12 Xa.
3 Result
3
8/14/2019 affine similarity metric rectification with c codes
4/24
ECE661 HW2 Report Rong Zhang
Figure 1: pic1.jpg
6
8/14/2019 affine similarity metric rectification with c codes
5/24
ECE661 HW2 Report Rong Zhang
Figure 2: pic2.jpg
7
8/14/2019 affine similarity metric rectification with c codes
6/24
ECE661 HW2 Report Rong Zhang
Figure 3: pic3.jpg
8
8/14/2019 affine similarity metric rectification with c codes
7/24
ECE661 HW2 Report Rong Zhang
Figure 4: pic4.jpg
9
8/14/2019 affine similarity metric rectification with c codes
8/24
ECE661 HW2 Report Rong Zhang
Figure 5: sample.jpg
10
8/14/2019 affine similarity metric rectification with c codes
9/24
ECE661 HW2 Report Rong Zhang
4 Method 2
The second method is the metric rectification using C
. As stated in the textbook, the dualdegenerate conic C
contains all the information for a metric rectification. It determines
both the projective and affine components of a projective transformation H and leaves only
similarity distortions.Suppose we have a pair of physically orthogonal lines, l m. Since
C
=
1 0 00 1 00 0 0
(11)
and (l1, l2)(m1, m2)T = 0, we can write
lTC
m = 0. (12)
Under projective transformation H, we havel
= H
Tl, m
= H
T
m and C
= HC
H
T
.From equation 7, we know that
lTC
m = 0. (13)
Suppose C
has the general conic form
C
=
a b/2 d/2b/2 c e/2d/2 e/2 f
, (14)
we have
lTC
m = (l1, l
2, l
3)
a b/2 d/2b/2 c e/2d/2 e/2 f
m1m2m3
(15)
=
l1m
1,l
1m
2+l
2m
1
2, l2m
2,l
1m
3+l
3m
1
2,l
2m
3+l
3m
2
2, l3m
3
abcdef
= 0.
Considering scale problem, the conic has five degrees of freedom, i.e., we can let f = 1.Therefore, five pairs of orthogonal lines will provide five equations. The dual conic in thedistorted image C
is determined, which can be further decomposed into
C
= UDUT. (16)
Theoretically, the matrix D should have a element equals to zero. However, in practice,C
is usually solved from the five equations by least mean square method for example. It
4
8/14/2019 affine similarity metric rectification with c codes
10/24
ECE661 HW2 Report Rong Zhang
usually have the last eigenvalue 3 not exactly equals to zero but rather small compared tothe other eigenvalues. Let
D = diag(
1,
2, 10)diag(1, 1, 3/100)diag(
1,
2, 10) (17)
diag(
1,
2, 10)C
diag(
1,
2, 10), (18)
we get H = UD. Note that in the above equation, the eigenvalue 3 is scaled by a factor1/100 which will make diag(1, 1, 3/100) more close to C
. By applying X = H1Xc
pointwise, the distorted image is rectified to a similarity distorted one.
5 Results
6 Discussion
The first method uses two steps to rectify the distorted image while the second method is
a one-step approach. As we can seen from the results, both methods remove the projectiveand affine distortions while preserving the similarity. The first method is more robust byseparating into two steps, which involves only solving a 2-dimentional function.
The one-step method need to solving equations with 5 degrees of freedom where numer-ical stability causes problem. For example, the decomposition of C
usually have the last
eigenvalue not exactly equals to zero and therefore the rectification results may not be asgood as the first method. This method is so sensitive to the choice of orthogonal line pairsthat a single change of the selected pixel position value may degrade the reconstructionperformance (may also cause flipped images).
5
8/14/2019 affine similarity metric rectification with c codes
11/24
ECE661 HW2 Report Rong Zhang
Figure 6: pic1.jpg
11
8/14/2019 affine similarity metric rectification with c codes
12/24
ECE661 HW2 Report Rong Zhang
Figure 7: pic2.jpg
12
8/14/2019 affine similarity metric rectification with c codes
13/24
8/14/2019 affine similarity metric rectification with c codes
14/24
ECE661 HW2 Report Rong Zhang
Figure 9: pic4.jpg
14
8/14/2019 affine similarity metric rectification with c codes
15/24
ECE661 HW2 Report Rong Zhang
Figure 10: sample.jpg
15
8/14/2019 affine similarity metric rectification with c codes
16/24
Code for method 1
#include
#include
#include
#include
#include
#define CLIP2(minv, maxv, value) (min(maxv, max(minv, value)))
CvMat* cvmGetCol(CvMat * src, int iCol){
int i;
int nRow = src->rows;
CvMat *curCol = cvCreateMat(nRow,1,src->type);
for(i=0; iheight;
width = img_in->width;
step = img_in->widthStep;
channels = img_in->nChannels;
data_in = (uchar *)img_in->imageData;
printf("Processing a %d x %d image with %d channels\n",height,width,channels);
CvMat *check = cvCreateMat(height, width, CV_64FC1);
cvZero(check);
// allocate the output image and initialize
img_affine = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, channels);
img_scene = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, channels);
cvZero(img_affine);
cvZero(img_scene);
data_affine = (uchar *)img_affine->imageData;
8/14/2019 affine similarity metric rectification with c codes
17/24
data_scene = (uchar *)img_scene->imageData;
// remove projective distortion
CvMat * vetex = cvCreateMat(3,4,CV_64FC1);
// get the coordinate values of four vetexes of a square
// up-left, up-right, down-left, down-right
for(i=0; i
8/14/2019 affine similarity metric rectification with c codes
18/24
cvmSet(ptxp,0,0,(double)j);
cvmSet(ptxp,1,0,(double)i);
cvmSet(ptxp,2,0,1.0);
// compute X_a
cvMatMul(&H1, ptxp, ptx);
curpi = CLIP2(0, height-1, (int)(cvmGet(ptx,1,0)/cvmGet(ptx,2,0)));
curpj = CLIP2(0, width-1, (int)(cvmGet(ptx,0,0)/cvmGet(ptx,2,0)));
cvSet2D(img_affine,curpi,curpj,cvGet2D(img_in,i,j));
cvmSet(check,curpi,curpj,1);
}
}
// output reconstructed affine image
img_interp = cvCloneImage(img_affine);
data_interp = (uchar *)img_interp->imageData;
//interpolation
double count;
for (i=1; i
8/14/2019 affine similarity metric rectification with c codes
19/24
cvCrossProduct(cvmGetCol(vetex,0), cvmGetCol(vetex,1), l1);
cvCrossProduct(cvmGetCol(vetex,0), cvmGetCol(vetex,2), m1);
cvCrossProduct(cvmGetCol(vetex,0), cvmGetCol(vetex,3), l2);
cvCrossProduct(cvmGetCol(vetex,2), cvmGetCol(vetex,1), m2);
double l11, l12, m11, m12, l21, l22, m21, m22;
l11 = cvmGet(l1,0,0); l12 = cvmGet(l1,1,0);
l21 = cvmGet(l2,0,0); l22 = cvmGet(l2,1,0);
m11 = cvmGet(m1,0,0); m12 = cvmGet(m1,1,0);
m21 = cvmGet(m2,0,0); m22 = cvmGet(m2,1,0);
// M*x = b
double Mdata[] = {l11*m11, l11*m12+l12*m11, l21*m21, l21*m22+l22*m21};
double bdata[] = {-l12*m12, -l22*m22};
CvMat M = cvMat(2,2,CV_64FC1,Mdata);
CvMat b = cvMat(2,1,CV_64FC1,bdata);
CvMat *x = cvCreateMat(2,1,CV_64FC1);
cvSolve(&M,&b,x);
// Set matrix S
double Sdata[] = {cvmGet(x,0,0), cvmGet(x,1,0), cvmGet(x,1,0), 1.0};CvMat S = cvMat(2,2,CV_64FC1, Sdata);
// SVD S=UDV_T
CvMat* U = cvCreateMat(2,2,CV_64FC1);
CvMat* D = cvCreateMat(2,2,CV_64FC1);
CvMat* V = cvCreateMat(2,2,CV_64FC1);
cvSVD(&S, D, U, V, CV_SVD_U_T|CV_SVD_V_T);
//The flags cause U and V to be returned transposed (does not work well
without the transpose flags).
//Therefore, in OpenCV, S = U^T D V
CvMat* U_T = cvCreateMat(2,2,CV_64FC1);
CvMat* sqrtD = cvCreateMat(2,2,CV_64FC1);
CvMat* A = cvCreateMat(2,2,CV_64FC1);
cvPow(D, sqrtD, 0.5);
cvTranspose(U, U_T);
cvMatMul(U_T, sqrtD, A);
cvMatMul(A, V, A);
// Set H2
double t[] = {0, 0};
double H2data[] = {cvmGet(A,0,0),cvmGet(A,0,1),t[0],
cvmGet(A,1,0),cvmGet(A,1,1),t[1], 0,0,1};
CvMat H2 = cvMat(3,3,CV_64FC1, H2data);
CvMat *invH2 = cvCreateMat(3,3,CV_64FC1);
cvInvert(&H2, invH2);
cvZero(check);
for (i=0; i
8/14/2019 affine similarity metric rectification with c codes
20/24
cvSet2D(img_scene,curpi,curpj,cvGet2D(img_affine,i,j));
cvmSet(check,curpi,curpj,1);
}
}
// output reconstructed scene image
img_interp = cvCloneImage(img_scene);
data_interp = (uchar *)img_interp->imageData;
//interpolation
for (i=1; i
8/14/2019 affine similarity metric rectification with c codes
21/24
8/14/2019 affine similarity metric rectification with c codes
22/24
// get five pairs of orthogonal lines
// for each lines, two points needed
CvMat *A = cvCreateMat(5,5,CV_64FC1);
CvMat *x = cvCreateMat(5,1,CV_64FC1);
CvMat *b = cvCreateMat(5,1,CV_64FC1);
CvMat *l = cvCreateMat(3,1,CV_64FC1);
CvMat *m = cvCreateMat(3,1,CV_64FC1);
CvMat * vetex = cvCreateMat(3,20,CV_64FC1); // 4*5 columns representing 20
points
for(i=0; i
8/14/2019 affine similarity metric rectification with c codes
23/24
cvEigenVV(&C, U, E); //C=U^T D U
}
cvTranspose(U,U_T);
CvMat * ss = cvCreateMat(3,3,CV_64FC1);
cvZero(ss);
for(i=0; i
8/14/2019 affine similarity metric rectification with c codes
24/24
(cvmGet(check,i-1,j-1)==1)+(cvmGet(check,i-
1,j+1)==1)+
(cvmGet(check,i+1,j-
1)==1)+(cvmGet(check,i+1,j+1)==1);
if(count != 0){
for (k=0; k