Homework 2 Ex. 1 Let u = [u 1, u 2, 1] T and v = [v 1, v 2, 1] T denote the homogeneous coordinates of points in planes P and Q, respectively. Matching point pairs are related by a projection matrix H as v i = Hu i, i = 1, 2,..., n (a) A transform from P to Q in the form of a translation by (x 0, x 1 ) then a rotation by θ then a scaling by (s 0, s 1 ) is described by the matrix 0.951623 0.443749 6.97686 H = 0.401487 0.860992 2.29753 0 0 1 Determine the values of (x 0, x 1, s 0, s 1, θ). (b) A transform from P to Q in the form of a translation by (x 0, x 1 ) then a scaling by (s 0, s 1 ) then a rotation by θ is described by the matrix 0.951623 0.401487 6.76555 H = 0.443749 0.860992 2.08622 0 0 1 Determine the values of (x 0, x 1, s 0, s 1, θ) for this order transformations. Answer (ex. 1) (a) An algebraic expression for the transformation can be formed from the product of transformation matrices. H = SRT = s 0 0 0 cos θ sin θ 0 0 s 1 0 sin θ cos θ 0 1 0 x 0 0 1 x 1 = s 0 cos θ s 0 sin θ s 0 x 0 cos θ + s 0 x 1 sin θ s 1 sin θ s 1 cos θ s 1 x 1 cos θ s 1 x 0 sin θ 0 0 1 0 0 1 0 0 1 0 0 1 Therefore s 0 = ( H 2 1,1 + H 2 1,2) 1/2 = ( 0.951623 2 + 0.401487 2) 1/2 = 1.05 s 1 = ( H 2 2,1 + H 2 2,2) 1/2 = ( 0.401487 2 + 0.860992 2) 1/2 = 0.95 x 0 = H 1,3 cos θ/s 0 H 2,3 sin θ/s 1 = 5 x 1 = H 1,3 sin θ/s 0 + H 2,3 cos θ/s 1 = 5 θ = arctan(h 1,2, H 1,1 ) 180/π = 25 (b) A similar algebraic expansion for this order of transformation is cos θ sin θ 0 H = RST = sin θ cos θ 0 s 0 0 0 0 s 1 0 1 0 x 0 0 1 x 1 = s 0 cos θ s 1 sin θ s 0 x 0 cos θ + s 1 x 1 sin θ s 0 sin θ s 1 cos θ s 1 x 1 cos θ s 0 x 0 sin θ 0 0 1 0 0 1 0 0 1 0 0 1 The parameters can be found by operations similar to those in (a). The results are the same numerical values given in the solution of (a). Ex. 2 Matching pairs of points from P and Q are given by the columns of the arrays U and V below. 21.630 28.280 31.870 46.150 28.230 36.530 24.540 31.940 U = 59.260 62.040 65.310 36.440 50.510 60.200 37.740 57.080 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 17.536 23.468 26.280 46.697 25.960 31.992 25.144 28.163 V = 52.587 56.753 60.691 36.368 45.732 56.870 32.699 52.848 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000
(a) Find a matrix H that maps points from P to Q. (b) Assume that the mapping is a translation, a scaling, and a rotation in that order. Find the parameters of the individual transformations. Answer (ex. 2) (a) The relationship is of the form V = HU. Use singular value decomposition (SVD) to find orthogonal matrices Q and R and a diagonal matrix D such that U = QDR T Then (by definition of an orthogonal matrix, QQ T = I and R T R = I). Hence, V = H ( QDR T ) VR = HQDR T R = HQD VRD 1 = HQ VRD 1 Q T = H VU = H where U = RD 1 Q T is the pseudo-inverse of U. We can find the SVD of U by using the SVDC routine in IDL or with a similar tool in other languages. The call is SVDC,U,W,Q,R. For the given matrix, 0.494630 0.868995 0.013726 0.000000 0.000000 0.000000 0.000000 Q = 0.868960 0.494774 0.010402 0.000000 0.000000 0.000000 0.000000 0.000000 = [ A 0 ] 0.015831 0.006782 0.999852 0.000000 0.000000 0.000000 0.000000 0.000000 where A is a square orthogonal matrix, which implies that AA T = A T A = I. 176.969620 0 0 0 0 0 0 0 0 25.849873 0 0 0 0 0 0 0 0 0.347170 0 0 0 0 0 D = diag(w) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The square matrix S contains the non-zero singular values on its diagonal. [ ] S 0 0 0 0.351525 0.406854 0.249213 0.805477 0.000000 0.000000 0.000000 0.000000 0.383762 0.236511 0.097002 0.256932 0.442195 0.329696 0.518618 0.384923 0.409852 0.178415 0.336917 0.164745 0.155032 0.322509 0.713437 0.149986 R = 0.308007 0.854216 0.036460 0.308334 0.032379 0.256078 0.039941 0.104058 0.327007 0.017505 0.250450 0.229042 0.817219 0.004531 0.320200 0.084731 0.397786 0.076047 0.368045 0.021316 0.078106 0.823297 0.025273 0.124559 0.253990 0.102870 0.778969 0.299897 0.303097 0.182773 0.317916 0.042801 0.369637 0.018537 0.093088 0.141878 0.116474 0.102318 0.127321 0.891058 We can now write U = [ A 0 ] [ ] [ ] S 0 R T a = ASR T 0 0 a where R = [R a R b ] partitions R into a part R a that has the same number of columns as S and a part R b that we don t actually care about. We can now write R T b V = HU = ASR T a H = VR a S 1 A T = VU
where we now have another expression for U. We would not have to do this partitioning, but it is instructive to see how the pseudo-inverse works. 0.351525 0.406854 0.249213 0.383762 0.236511 0.097002 0.409852 0.178415 0.336917 R a = 0.308007 0.854216 0.036460 0.327007 0.017505 0.250450 0.397786 0.076047 0.368045 0.253990 0.102870 0.778969 0.369637 0.018537 0.093088 0.494630 0.868995 0.013726 0.005651 0.000000 A = 0.868960 0.494774 0.010402 S 1 = 0.000000 0.038685 0.000000 0.015831 0.006782 0.999852 0.000000 0.000000 2.880430 0.984108 0.220440 9.313249 H = VR a S 1 A T = 0.227215 0.954889 8.914172 0.000000 0.000000 1.000000 (b) The H matrix has the same form as Problem (1b). The solution is s 0 = H1,1 2 + H2 2,1 = 1.01 s 1 H1,2 2 + H2 2,2 = 0.98 θ = arctan(h 2,1, H 1,1 ) = 13 x 0 = (H 1,3 cos θ H 2,3 sin θ)/s 0 = 7.0 x 1 = (H 1,3 sin θ + H 2,3 cos θ)/s 1 = 11.0 Ex. 3 Images LWIR087c.png, MWIR087c.png, SWIR087c.png are provided in the images directory http://www.cis.rit.edu/class/simg782/images (a) Find a transformation matrix H 1 that maps the MW image to the LW image. (b) Find a transformation H 2 that maps the SW image to the LW image. (c) Construct two greyscale images by mapping the MW and SW images. (d) Construct an RGB image by building an array that has the mapped SW, the mapped MW and the original LW as array layers. This is a raw image array that can be displayed with IDL or other tools. Answer (ex. 3) The IDL script below does the calculations. Point matches were constructed in ENVI and saved as text files. The SWIR image was rotated 180 before the matching points were located. The image files that were produced are shown below.
Mapped MWIR image Rotated and mapped SWIR image LW (Red) and MW (Green) overlaid LW (Red) and SW (Blue) overlaid Overlaid images LWIR (Red), MWIR (green), SWIR (Blue)
Program Listing ;Open LWIR to MWIR matching points from an ENVI ground control points table, LWIR to MWIR matching points from file L2Mpoints.txt pname= L2Mpoints.txt openr,1,pname ;Read and 5 header lines s= for k=0,4 do begin & readf,1,s &,s & end,format= (3x,3(A3,4x),A3,3x,2(A4,3x),2(A2,5x),A3),$ [ LWx, LWy, MWx, MWy, L2Mx, L2My, Rx, Ry, R ] np=10 pts=fltarr(9,np) readf,1,pts,pts,format= (9(f6.2,1x)) close,1 ;Construct the matrix of LWIR points. ;Note: The y-coordinates from ENVI are from top ;so we need to subtract from nrows to make them ;measure from the bottom. P=transpose([pts[0:1,*],replicate(1.,1,np)]) P[*,1]=510-P[*,1], Matrix P,P,format= (10(f6.2,1x)) Q=transpose([pts[2:3,*],replicate(1.,1,np)]) Q[*,1]=510-Q[*,1], Matrix Q,Q,format= (10(f6.2,1x)) ;Print the projective transform matrix that ;projects backwards from P to Q so we can find ;the LWIR pixel locations in the MWIR image. H=Q##Pseudo_inverse(P), Projective transform matrix, H1,H,format= (3(f8.4,1x)) ;Get the images LWIR=read_image( LWIR087c.png ) MWIR=read_image( MWIR087c.png ) sz=size(lwir,/dim) ;Construct a coordinate array for the pixels coordinate_grid,x2,y2,sz[0],sz[1] ;Project from Q to P. The array W contains the ;(X2,Y2) coordinates as columns in homogeneous form W=[[X2[*]],[Y2[*]],[replicate(1,sz[0]*sz[1])]]
;Z contains the projected coordinates in homogeneous form Z=H##W ;Find the coordinate values that fall within the image k=where(z[*,0] GE 0 AND Z[*,0] LT sz[0] AND Z[*,1] GE 0 AND Z[*,1] LT sz[1]) ;Set up an array to hold the projection A=bytarr(sz[0],sz[1]) ;Find the values in the MWIR grid and associate them ;with the LWIR image. A[k]=MWIR[Z[k,0],Z[k,1]] ;Set up a color array to hold the results B=bytarr(3,640,510) B[0,*,*]=LWIR ;original LWIR array B[1,*,*]=A ;Projected MWIR array write_png, M2L.png,B disp_image,a,xpos=0,ypos=0,title= Mapped MWIR disp_image,b,true=1,xpos=640,ypos=0,title= MWIR (G) projected to LWIR (R) ;======================================================== ;Match SWIR to LWIR Section ;========================================================, Matching SWIR to LWIR ;Open LWIR to SWIR matching points from an ENVI ground control points table pname= L2Spoints.txt openr,1,pname ;Read and 5 header lines s= for k=0,4 do begin & readf,1,s &,s & end,format= (3x,3(A3,4x),A3,3x,2(A4,3x),2(A2,5x),A3),$ [ LWx, LWy, SWx, SWy, L2Sx, L2Sy, Rx, Ry, R ] np=10 pts=fltarr(9,np) readf,1,pts,pts,format= (9(f6.2,1x)) close,1 ;Construct the matrix of LWIR points. ;Note: The y-coordinates from ENVI are from top ;so we need to subtract from nrows to make them ;measure from the bottom. P1=transpose([pts[0:1,*],replicate(1.,1,np)]) P1[*,1]=510-P1[*,1], Matrix P1,P1,format= (10(f6.2,1x))
Q1=transpose([pts[2:3,*],replicate(1.,1,np)]) Q1[*,1]=510-Q1[*,1], Matrix Q1,Q1,format= (10(f6.2,1x)) ;Print the projective transform matrix that ;projects backwards from P to Q so we can find ;the LWIR pixel locations in the MWIR image. H2=Q1##Pseudo_inverse(P1), Projective transform matrix, H2,H2,format= (3(f8.4,1x)) SWIR=read_image( SWIR087cr.png ) ;Project from Q to P. The array W contains the ;(X2,Y2) coordinates as columns in homogeneous form W=[[X2[*]],[Y2[*]],[replicate(1,sz[0]*sz[1])]] ;Z1 contains the projected coordinates in homogeneous form Z=H2##W ;Find the coordinate values that fall within the image k=where(z[*,0] GE 0 AND Z[*,0] LT sz[0] AND Z[*,1] GE 0 AND Z[*,1] LT sz[1]) ;Set up an array to hold the projection A1=bytarr(sz[0],sz[1]) ;Find the values in the MWIR grid and associate them ;with the LWIR image. A1[k]=SWIR[Z[k,0],Z[k,1]] ;Set up a color array to hold the results B2=bytarr(3,640,510) B2[0,*,*]=LWIR ;original LWIR array B2[2,*,*]=A1 ;Projected SWIR array write_png, S2L.png,B2 disp_image,a1,xpos=0,ypos=520,title= Mapped SWIR disp_image,b2,true=1,xpos=640,ypos=520,title= SWIR(B) projected to LWIR(R) B2[1,*,*]=A ;Projected MWIR array disp_image,b2,true=1,xpos=320,ypos=255,title= SWIR (B), MWIR (G), projected to LWIR (R) write_png, SML087.png,B2
Program Output LWIR to MWIR matching points from file L2Mpoints.txt ; ENVI Ground Control Points Table ; ENVI Image to Image GCP Table ; Base (x,y), Warp (x,y), Predict (x,y), Error (x,y), RMS Error ; Total RMS Error: 0.797829 ; LWx LWy MWx MWy L2Mx L2My Rx Ry R 64.33 46.67 40.33 51.17 40.78 51.27 0.45 0.10 0.46 491.17 340.83 473.00 344.25 473.18 344.08 0.18-0.17 0.25 253.33 470.17 235.25 476.75 235.20 476.65-0.05-0.10 0.11 36.83 324.33 15.17 331.83 15.85 331.84 0.68 0.01 0.68 64.33 46.67 40.33 51.17 40.78 51.27 0.45 0.10 0.46 288.67 67.83 267.83 71.67 266.59 71.24-1.24-0.43 1.31 604.17 77.17 583.33 78.50 583.97 78.65 0.64 0.15 0.66 577.00 266.50 558.00 268.67 558.73 268.70 0.73 0.03 0.73 326.17 235.83 307.67 240.00 306.06 240.04-1.61 0.04 1.61 358.17 348.33 339.67 352.67 339.44 352.92-0.23 0.25 0.34 Matrix P 64.33 491.17 253.33 36.83 64.33 288.67 604.17 577.00 326.17 358.17 463.33 169.17 39.83 185.67 463.33 442.17 432.83 243.50 274.17 161.67 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 Matrix Q 40.33 473.00 235.25 15.17 40.33 267.83 583.33 558.00 307.67 339.67 458.83 165.75 33.25 178.17 458.83 438.33 431.50 241.33 270.00 157.33 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 Projective transform matrix, H1 1.0059-0.0103-19.1916 0.0077 1.0070-8.5930 0.0000-0.0000 1.0000 Matching SWIR to LWIR ; ENVI Ground Control Points Table ; ENVI Image to Image GCP Table ; Base (x,y), Warp (x,y), Predict (x,y), Error (x,y), RMS Error ; Total RMS Error: 0.963595 ; LWx LWy SWx SWy L2Sx L2Sy Rx Ry R 283.25 330.75 260.75 380.00 260.05 380.32-0.70 0.32 0.77 274.00 450.50 252.00 500.75 251.89 501.35-0.11 0.60 0.61 604.25 86.50 582.25 131.00 582.21 131.36-0.04 0.36 0.36 350.75 235.25 328.75 283.00 327.35 283.26-1.40 0.26 1.42 120.25 60.25 92.00 108.50 92.51 108.32 0.51-0.18 0.54 490.75 342.00 470.50 390.25 470.00 389.40-0.50-0.85 0.99 598.00 247.00 575.75 292.50 577.50 292.73 1.75 0.23 1.77 158.50 181.50 132.25 230.25 132.40 230.70 0.15 0.45 0.48 483.75 80.25 460.75 126.50 460.30 125.94-0.45-0.56 0.72 97.75 415.25 72.50 468.50 73.30 467.87 0.80-0.63 1.02
Matrix P1 283.25 274.00 604.25 350.75 120.25 490.75 598.00 158.50 483.75 97.75 179.25 59.50 423.50 274.75 449.75 168.00 263.00 328.50 429.75 94.75 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 Matrix Q1 260.75 252.00 582.25 328.75 92.00 470.50 575.75 132.25 460.75 72.50 130.00 9.25 379.00 227.00 401.50 119.75 217.50 279.75 383.50 41.50 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 Projective transform matrix, H2 1.0113-0.0100-24.5975 0.0089 1.0096-53.8679-0.0000 0.0000 1.0000