[59] | 1 | ;+ |
---|
| 2 | ; |
---|
[101] | 3 | ; @file_comments warm (or map) a unit square onto an arbitrary quadrilateral |
---|
[59] | 4 | ; according to the 4-point correspondences: |
---|
| 5 | ; (0,0) -> (x0,y0) |
---|
| 6 | ; (1,0) -> (x1,y1) |
---|
| 7 | ; (1,1) -> (x2,y2) |
---|
| 8 | ; (0,1) -> (x3,y3) |
---|
| 9 | ; The mapping is done using perspective transformation which preserve |
---|
| 10 | ; lines in all orientations and permit quadrilateral to quadrilateral |
---|
| 11 | ; mappings. see ref. bellow. |
---|
| 12 | ; |
---|
[101] | 13 | ; @categories image, grid manipulation |
---|
[59] | 14 | ; |
---|
[101] | 15 | ; @examples |
---|
[105] | 16 | ; IDL> res = square2quadrilateral(x0,y0,x1,y1,x2,y2,x3,y3[,xin,yin]) |
---|
[59] | 17 | ; |
---|
[101] | 18 | ; @param x0in {in}{required} the coordinates of the quadrilateral |
---|
[59] | 19 | ; (see above for correspondance with the unit square). Can be |
---|
| 20 | ; scalar or array. (x0,y0), (x1,y1), (x2,y2) and (x3,y3) are |
---|
| 21 | ; given in the anticlockwise order. |
---|
[101] | 22 | ; @param y0in {in}{required} the coordinates of the quadrilateral |
---|
| 23 | ; (see above for correspondance with the unit square). Can be |
---|
| 24 | ; scalar or array. (x0,y0), (x1,y1), (x2,y2) and (x3,y3) are |
---|
| 25 | ; given in the anticlockwise order. |
---|
| 26 | ; @param x1in {in}{required} the coordinates of the quadrilateral |
---|
| 27 | ; (see above for correspondance with the unit square). Can be |
---|
| 28 | ; scalar or array. (x0,y0), (x1,y1), (x2,y2) and (x3,y3) are |
---|
| 29 | ; given in the anticlockwise order. |
---|
| 30 | ; @param y1in {in}{required} the coordinates of the quadrilateral |
---|
| 31 | ; (see above for correspondance with the unit square). Can be |
---|
| 32 | ; scalar or array. (x0,y0), (x1,y1), (x2,y2) and (x3,y3) are |
---|
| 33 | ; given in the anticlockwise order. |
---|
| 34 | ; @param x2in {in}{required} the coordinates of the quadrilateral |
---|
| 35 | ; (see above for correspondance with the unit square). Can be |
---|
| 36 | ; scalar or array. (x0,y0), (x1,y1), (x2,y2) and (x3,y3) are |
---|
| 37 | ; given in the anticlockwise order. |
---|
| 38 | ; @param y2in {in}{required} the coordinates of the quadrilateral |
---|
| 39 | ; (see above for correspondance with the unit square). Can be |
---|
| 40 | ; scalar or array. (x0,y0), (x1,y1), (x2,y2) and (x3,y3) are |
---|
| 41 | ; given in the anticlockwise order. |
---|
| 42 | ; @param x3in {in}{required} the coordinates of the quadrilateral |
---|
| 43 | ; (see above for correspondance with the unit square). Can be |
---|
| 44 | ; scalar or array. (x0,y0), (x1,y1), (x2,y2) and (x3,y3) are |
---|
| 45 | ; given in the anticlockwise order. |
---|
| 46 | ; @param y3in {in}{required} the coordinates of the quadrilateral |
---|
| 47 | ; (see above for correspondance with the unit square). Can be |
---|
| 48 | ; scalar or array. (x0,y0), (x1,y1), (x2,y2) and (x3,y3) are |
---|
| 49 | ; given in the anticlockwise order. |
---|
[59] | 50 | ; |
---|
[101] | 51 | ; @param xxin {in}{required} the coordinates of the point(s) for which we want to do the |
---|
[59] | 52 | ; mapping. Can be scalar or array. |
---|
[101] | 53 | ; @param yyin {in}{required} the coordinates of the point(s) for which we want to do the |
---|
| 54 | ; mapping. Can be scalar or array. |
---|
[59] | 55 | ; |
---|
[101] | 56 | ; @returns |
---|
[59] | 57 | ; |
---|
| 58 | ; (2,n) array: the new coodinates (xout, yout) of the (xin,yin) |
---|
| 59 | ; point(s) after mapping. |
---|
| 60 | ; If xin is a scalar, then n is equal to the number of elements of |
---|
| 61 | ; x0. If xin is an array , then n is equal to the number of |
---|
| 62 | ; elements of xin. |
---|
| 63 | ; If xin and yin are omited, square2quadrilateral returns the |
---|
| 64 | ; matrix A which is used for the inverse transformation. |
---|
| 65 | ; |
---|
| 66 | ; |
---|
[101] | 67 | ; @restrictions I think degenerated quadrilateral (e.g. flat of |
---|
[59] | 68 | ; twisted) is not work. This has to be tested. |
---|
| 69 | ; |
---|
[101] | 70 | ; @examples |
---|
[59] | 71 | ; |
---|
| 72 | ; IDL> splot,[0,5],[0,3],/nodata,xstyle=1,ystyle=1 |
---|
| 73 | ; IDL> tracegrille, findgen(11)*.1, findgen(11)*.1,color=indgen(12)*20 |
---|
| 74 | ; IDL> xin = (findgen(11)*.1)#replicate(1, 11) |
---|
| 75 | ; IDL> yin = replicate(1, 11)#(findgen(11)*.1) |
---|
| 76 | ; IDL> out = square2quadrilateral(2,1,3,0,5,1,2,3, xin, yin) |
---|
| 77 | ; IDL> tracegrille, reform(out[0,*],11,11), reform(out[1,*],11,11),color=indgen(12)*20 |
---|
| 78 | ; |
---|
[101] | 79 | ; @history |
---|
| 80 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[59] | 81 | ; August 2003 |
---|
| 82 | ; Based on "Digital Image Warping" by G. Wolberg |
---|
| 83 | ; IEEE Computer Society Press, Los Alamitos, California |
---|
| 84 | ; Chapter 3, see p 52-56 |
---|
| 85 | ; |
---|
| 86 | ;- |
---|
| 87 | ;------------------------------------------------------------ |
---|
| 88 | ;------------------------------------------------------------ |
---|
| 89 | ;------------------------------------------------------------ |
---|
| 90 | FUNCTION square2quadrilateral, x0in, y0in, x1in, y1in, x2in, y2in, x3in, y3in, xxin, yyin |
---|
| 91 | ; |
---|
| 92 | ; Warning, wrong definition of (x2,y2) and (x3,y3) at the bottom of |
---|
| 93 | ; page 54 of Wolberg's book, see figure 3.7 page 56 for the good |
---|
| 94 | ; definition. |
---|
| 95 | ; |
---|
| 96 | IF keyword_set(double) THEN BEGIN |
---|
| 97 | x0 = double(x0in) |
---|
| 98 | x1 = double(x1in) |
---|
| 99 | x2 = double(x2in) |
---|
| 100 | x3 = double(x3in) |
---|
| 101 | y0 = double(y0in) |
---|
| 102 | y1 = double(y1in) |
---|
| 103 | y2 = double(y2in) |
---|
| 104 | y3 = double(y3in) |
---|
| 105 | IF arg_present(xxin) THEN BEGIN |
---|
| 106 | xin = double(xxin) |
---|
| 107 | yin = double(yyin) |
---|
| 108 | ENDIF |
---|
| 109 | ENDIF ELSE BEGIN |
---|
| 110 | x0 = float(x0in) |
---|
| 111 | x1 = float(x1in) |
---|
| 112 | x2 = float(x2in) |
---|
| 113 | x3 = float(x3in) |
---|
| 114 | y0 = float(y0in) |
---|
| 115 | y1 = float(y1in) |
---|
| 116 | y2 = float(y2in) |
---|
| 117 | y3 = float(y3in) |
---|
| 118 | IF arg_present(xxin) THEN BEGIN |
---|
| 119 | xin = float(xxin) |
---|
| 120 | yin = float(yyin) |
---|
| 121 | ENDIF |
---|
| 122 | ENDELSE |
---|
| 123 | ; |
---|
| 124 | IF keyword_set(double) THEN a = dlbarr(8, n_elements(x0)) $ |
---|
| 125 | ELSE a = fltarr(8, n_elements(x0)) |
---|
| 126 | ; |
---|
| 127 | delx3 = x0-x1+x2-x3 |
---|
| 128 | dely3 = y0-y1+y2-y3 |
---|
| 129 | ; |
---|
| 130 | affinemap = where(delx3 EQ 0 AND dely3 EQ 0) |
---|
| 131 | IF affinemap[0] NE -1 THEN BEGIN |
---|
| 132 | xx0 = x0[affinemap] |
---|
| 133 | xx1 = x1[affinemap] |
---|
| 134 | xx2 = x2[affinemap] |
---|
| 135 | yy0 = y0[affinemap] |
---|
| 136 | yy1 = y1[affinemap] |
---|
| 137 | yy2 = y2[affinemap] |
---|
| 138 | ; |
---|
| 139 | a[0, affinemap] = xx1-xx0 |
---|
| 140 | a[1, affinemap] = xx2-xx1 |
---|
| 141 | a[2, affinemap] = xx0 |
---|
| 142 | a[3, affinemap] = yy1-yy0 |
---|
| 143 | a[4, affinemap] = yy2-yy1 |
---|
| 144 | a[5, affinemap] = yy0 |
---|
| 145 | a[6, affinemap] = 0 |
---|
| 146 | a[7, affinemap] = 0 |
---|
| 147 | ENDIF |
---|
| 148 | ; |
---|
| 149 | projectivemap = where(delx3 NE 0 OR dely3 NE 0) |
---|
| 150 | IF projectivemap[0] NE -1 THEN BEGIN |
---|
| 151 | xx0 = x0[projectivemap] |
---|
| 152 | xx1 = x1[projectivemap] |
---|
| 153 | xx2 = x2[projectivemap] |
---|
| 154 | xx3 = x3[projectivemap] |
---|
| 155 | yy0 = y0[projectivemap] |
---|
| 156 | yy1 = y1[projectivemap] |
---|
| 157 | yy2 = y2[projectivemap] |
---|
| 158 | yy3 = y3[projectivemap] |
---|
| 159 | ; |
---|
| 160 | delx1 = xx1-xx2 |
---|
| 161 | dely1 = yy1-yy2 |
---|
| 162 | delx2 = xx3-xx2 |
---|
| 163 | dely2 = yy3-yy2 |
---|
| 164 | delx3 = delx3[projectivemap] |
---|
| 165 | dely3 = dely3[projectivemap] |
---|
| 166 | ; |
---|
| 167 | div = delx1*dely2-dely1*delx2 |
---|
| 168 | zero = where(div EQ 0) |
---|
| 169 | IF zero[0] NE -1 THEN BEGIN |
---|
| 170 | stop |
---|
| 171 | ENDIF |
---|
| 172 | a13 = (delx3*dely2-dely3*delx2)/div |
---|
| 173 | a23 = (delx1*dely3-dely1*delx3)/div |
---|
| 174 | ; |
---|
| 175 | a[0, projectivemap] = xx1-xx0+a13*xx1 |
---|
| 176 | a[1, projectivemap] = xx3-xx0+a23*xx3 |
---|
| 177 | a[2, projectivemap] = xx0 |
---|
| 178 | a[3, projectivemap] = yy1-yy0+a13*yy1 |
---|
| 179 | a[4, projectivemap] = yy3-yy0+a23*yy3 |
---|
| 180 | a[5, projectivemap] = yy0 |
---|
| 181 | a[6, projectivemap] = a13 |
---|
| 182 | a[7, projectivemap] = a23 |
---|
| 183 | ENDIF |
---|
| 184 | ; |
---|
| 185 | IF NOT arg_present(xxin) THEN return, a |
---|
| 186 | ; |
---|
| 187 | IF n_elements(xin) EQ 1 THEN BEGIN |
---|
| 188 | xin = replicate(xin, n_elements(x0)) |
---|
| 189 | yin = replicate(yin, n_elements(x0)) |
---|
| 190 | ENDIF |
---|
| 191 | ; |
---|
| 192 | IF keyword_set(double) THEN res = dblarr(2, n_elements(xin)) $ |
---|
| 193 | ELSE res = fltarr(2, n_elements(xin)) |
---|
| 194 | IF n_elements(x0) EQ 1 THEN BEGIN |
---|
| 195 | div = a[6]*xin[*] + a[7]*yin[*] + 1 |
---|
| 196 | zero = where(div EQ 0) |
---|
| 197 | IF zero[0] NE -1 THEN BEGIN |
---|
| 198 | stop |
---|
| 199 | ENDIF |
---|
| 200 | res[0, *] = (a[0]*xin[*] + a[1]*yin[*] + a[2])/div |
---|
| 201 | res[1, *] = (a[3]*xin[*] + a[4]*yin[*] + a[5])/div |
---|
| 202 | ENDIF ELSE BEGIN |
---|
| 203 | div = a[6, *]*xin +a[7, *]*yin + 1 |
---|
| 204 | zero = where(div EQ 0) |
---|
| 205 | IF zero[0] NE -1 THEN BEGIN |
---|
| 206 | stop |
---|
| 207 | ENDIF |
---|
| 208 | res[0, *] = (a[0, *]*xin[*] + a[1, *]*yin[*] + a[2, *])/div |
---|
| 209 | res[1, *] = (a[3, *]*xin[*] + a[4, *]*yin[*] + a[5, *])/div |
---|
| 210 | ENDELSE |
---|
| 211 | ; |
---|
| 212 | RETURN, res |
---|
| 213 | END |
---|