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