[59] | 1 | ;+ |
---|
| 2 | ; NAME:quadrilateral2square |
---|
| 3 | ; |
---|
| 4 | ; PURPOSE:warm (or map) an arbitrary quadrilateral onto a unit square |
---|
| 5 | ; according to the 4-point correspondences: |
---|
| 6 | ; (x0,y0) -> (0,0) |
---|
| 7 | ; (x1,y1) -> (1,0) |
---|
| 8 | ; (x2,y2) -> (1,1) |
---|
| 9 | ; (x3,y3) -> (0,1) |
---|
| 10 | ; This is the inverse function of square2quadrilateral.pro |
---|
| 11 | ; The mapping is done using perspective transformation which preserve |
---|
| 12 | ; lines in all orientations and permit quadrilateral to quadrilateral |
---|
| 13 | ; mappings. see ref. bellow. |
---|
| 14 | ; |
---|
| 15 | ; CATEGORY:image/grid manipulation |
---|
| 16 | ; |
---|
| 17 | ; CALLING SEQUENCE: |
---|
| 18 | ; |
---|
| 19 | ; res = square2quadrilateral(x0,y0,x1,y1,x2,y2,x3,y3,xin,yin) |
---|
| 20 | ; |
---|
| 21 | ; INPUTS: |
---|
| 22 | ; |
---|
| 23 | ; x0,y0,x1,y1,x2,y2,x3,y3 the coordinates of the quadrilateral |
---|
| 24 | ; (see above for correspondance with the unit square). Can be |
---|
| 25 | ; scalar or array. (x0,y0), (x1,y1), (x2,y2) and (x3,y3) are |
---|
| 26 | ; given in the anticlockwise order. |
---|
| 27 | ; |
---|
| 28 | ; xin,yin:the coordinates of the point(s) for which we want to do the |
---|
| 29 | ; mapping. Can be scalar or array. |
---|
| 30 | ; |
---|
| 31 | ; KEYWORD PARAMETERS: |
---|
| 32 | ; |
---|
| 33 | ; /DOUBLE: use double precision to perform the computation |
---|
| 34 | ; |
---|
| 35 | ; OUTPUTS: |
---|
| 36 | ; |
---|
| 37 | ; (2,n) array: the new coodinates (xout, yout) of the (xin,yin) |
---|
| 38 | ; point(s) after mapping. |
---|
| 39 | ; If xin is a scalar, then n is equal to the number of elements of |
---|
| 40 | ; x0. If xin is an array , then n is equal to the number of |
---|
| 41 | ; elements of xin. |
---|
| 42 | ; |
---|
| 43 | ; COMMON BLOCKS:none |
---|
| 44 | ; |
---|
| 45 | ; SIDE EFFECTS: |
---|
| 46 | ; |
---|
| 47 | ; RESTRICTIONS: I think degenerated quadrilateral (e.g. flat of |
---|
| 48 | ; twisted) is not work. This has to be tested. |
---|
| 49 | ; |
---|
| 50 | ; EXAMPLE: |
---|
| 51 | ; |
---|
| 52 | ; IDL> splot,[0,5],[0,3],/nodata,xstyle=1,ystyle=1 |
---|
| 53 | ; IDL> tracegrille, findgen(11)*.1, findgen(11)*.1,color=indgen(12)*20 |
---|
| 54 | ; IDL> xin = (findgen(11)*.1)#replicate(1, 11) |
---|
| 55 | ; IDL> yin = replicate(1, 11)#(findgen(11)*.1) |
---|
| 56 | ; IDL> out = square2quadrilateral(2,1,3,0,5,1,2,3, xin, yin) |
---|
| 57 | ; IDL> tracegrille, reform(out[0,*],11,11), reform(out[1,*],11,11),color=indgen(12)*20 |
---|
| 58 | ; |
---|
| 59 | ; IDL> inorg=quadrilateral2square(2,1,3,0,5,1,2,3,out[0,*],out[1,*]) |
---|
| 60 | ; IDL> tracegrille, reform(inorg[0,*],11,11), reform(inorg[1,*],11,11),color=indgen(12)*20 |
---|
| 61 | ; |
---|
| 62 | ; MODIFICATION HISTORY: |
---|
| 63 | ; Sebastien Masson (smasson@lodyc.jussieu.fr) |
---|
| 64 | ; August 2003 |
---|
| 65 | ; Based on "Digital Image Warping" by G. Wolberg |
---|
| 66 | ; IEEE Computer Society Press, Los Alamitos, California |
---|
| 67 | ; Chapter 3, see p 52-56 |
---|
| 68 | ; |
---|
| 69 | ;- |
---|
| 70 | ;------------------------------------------------------------ |
---|
| 71 | ;------------------------------------------------------------ |
---|
| 72 | ;------------------------------------------------------------ |
---|
| 73 | FUNCTION quadrilateral2square, x0in, y0in, x1in, y1in, x2in, y2in, x3in, y3in, xxin, yyin, PERF = perf |
---|
| 74 | ; |
---|
| 75 | tempsone = systime(1) |
---|
| 76 | ; |
---|
| 77 | ; Warning, wrong definition of (x2,y2) and (x3,y3) at the bottom of |
---|
| 78 | ; page 54 of Wolberg's book, see figure 3.7 page 56 for the good |
---|
| 79 | ; definition. |
---|
| 80 | ; |
---|
| 81 | IF keyword_set(double) THEN BEGIN |
---|
| 82 | x0 = double(x0in) |
---|
| 83 | x1 = double(x1in) |
---|
| 84 | x2 = double(x2in) |
---|
| 85 | x3 = double(x3in) |
---|
| 86 | y0 = double(y0in) |
---|
| 87 | y1 = double(y1in) |
---|
| 88 | y2 = double(y2in) |
---|
| 89 | y3 = double(y3in) |
---|
| 90 | xin = double(xxin) |
---|
| 91 | yin = double(yyin) |
---|
| 92 | ENDIF ELSE BEGIN |
---|
| 93 | x0 = float(x0in) |
---|
| 94 | x1 = float(x1in) |
---|
| 95 | x2 = float(x2in) |
---|
| 96 | x3 = float(x3in) |
---|
| 97 | y0 = float(y0in) |
---|
| 98 | y1 = float(y1in) |
---|
| 99 | y2 = float(y2in) |
---|
| 100 | y3 = float(y3in) |
---|
| 101 | xin = float(xxin) |
---|
| 102 | yin = float(yyin) |
---|
| 103 | ENDELSE |
---|
| 104 | ; |
---|
| 105 | ; get the matrix A |
---|
| 106 | ; |
---|
| 107 | a = square2quadrilateral(x0in, y0in, x1in, y1in, x2in, y2in, x3in, y3in) |
---|
| 108 | ; |
---|
| 109 | ; compute the adjoint matrix |
---|
| 110 | ; |
---|
| 111 | IF keyword_set(double) THEN adj = dblarr(9, n_elements(x0)) $ |
---|
| 112 | ELSE adj = fltarr(9, n_elements(x0)) |
---|
| 113 | ; |
---|
| 114 | adj[0, *] = a[4, *] -a[7, *]*a[5, *] |
---|
| 115 | adj[1, *] = a[7, *]*a[2, *]-a[1, *] |
---|
| 116 | adj[2, *] = a[1, *]*a[5, *]-a[4, *]*a[2, *] |
---|
| 117 | adj[3, *] = a[6, *]*a[5, *]-a[3, *] |
---|
| 118 | adj[4, *] = a[0, *] -a[6, *]*a[2, *] |
---|
| 119 | adj[5, *] = a[3, *]*a[2, *]-a[0, *]*a[5, *] |
---|
| 120 | adj[6, *] = a[3, *]*a[7, *]-a[6, *]*a[4, *] |
---|
| 121 | adj[7, *] = a[6, *]*a[1, *]-a[0, *]*a[7, *] |
---|
| 122 | adj[8, *] = a[0, *]*a[4, *]-a[3, *]*a[1, *] |
---|
| 123 | ; |
---|
| 124 | IF n_elements(xin) EQ 1 THEN BEGIN |
---|
| 125 | xin = replicate(xin, n_elements(x0)) |
---|
| 126 | yin = replicate(yin, n_elements(x0)) |
---|
| 127 | ENDIF |
---|
| 128 | ; |
---|
| 129 | ; compute xprime, yprime and wprime |
---|
| 130 | ; |
---|
| 131 | IF n_elements(x0) EQ 1 THEN BEGIN |
---|
| 132 | wpr = 1./(adj[6]*xin + adj[7]*yin + adj[8]) |
---|
| 133 | ENDIF ELSE BEGIN |
---|
| 134 | wpr = 1./(adj[6, *]*xin + adj[7, *]*yin + adj[8, *]) |
---|
| 135 | ENDELSE |
---|
| 136 | xpr = xin*wpr |
---|
| 137 | ypr = yin*wpr |
---|
| 138 | ; |
---|
| 139 | IF keyword_set(double) THEN res = dblarr(2, n_elements(xin)) $ |
---|
| 140 | ELSE res = fltarr(2, n_elements(xin)) |
---|
| 141 | ; |
---|
| 142 | IF n_elements(x0) EQ 1 THEN BEGIN |
---|
| 143 | res[0, *] = xpr*adj[0] + ypr*adj[1] +wpr*adj[2] |
---|
| 144 | res[1, *] = xpr*adj[3] + ypr*adj[4] +wpr*adj[5] |
---|
| 145 | ENDIF ELSE BEGIN |
---|
| 146 | res[0, *] = xpr*adj[0, *] + ypr*adj[1, *] +wpr*adj[2, *] |
---|
| 147 | res[1, *] = xpr*adj[3, *] + ypr*adj[4, *] +wpr*adj[5, *] |
---|
| 148 | ENDELSE |
---|
| 149 | ; |
---|
| 150 | IF keyword_set(perf) THEN print, 'time quadrilateral2square', systime(1)-tempsone |
---|
| 151 | |
---|
| 152 | RETURN, res |
---|
| 153 | END |
---|