source: trunk/SRC/Interpolation/square2quadrilateral.pro @ 114

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

  • Property svn:executable set to *
File size: 7.2 KB
Line 
1;+
2;
3; @file_comments warm (or map) a unit square onto an arbitrary quadrilateral
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;
13; @categories image, grid manipulation
14;
15; @examples
16; IDL>  res = square2quadrilateral(x0,y0,x1,y1,x2,y2,x3,y3[,xin,yin])
17;
18;     @param x0in {in}{required}  the coordinates of the quadrilateral
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.
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.
50;
51;     @param xxin {in}{required} the coordinates of the point(s) for which we want to do the
52;     mapping. Can be scalar or array.
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.
55;
56; @returns
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;
67; @restrictions I think degenerated quadrilateral (e.g. flat of
68; twisted) is not work. This has to be tested.
69;
70; @examples
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;
79; @history
80;      Sebastien Masson (smasson\@lodyc.jussieu.fr)
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;------------------------------------------------------------
90FUNCTION 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;
97  compile_opt idl2, strictarrsubs
98;
99  IF keyword_set(double) THEN BEGIN
100    x0 = double(x0in)
101    x1 = double(x1in)
102    x2 = double(x2in)
103    x3 = double(x3in)
104    y0 = double(y0in)
105    y1 = double(y1in)
106    y2 = double(y2in)
107    y3 = double(y3in)
108    IF arg_present(xxin) THEN BEGIN
109      xin = double(xxin)
110      yin = double(yyin)
111    ENDIF
112  ENDIF ELSE BEGIN
113    x0 = float(x0in)
114    x1 = float(x1in)
115    x2 = float(x2in)
116    x3 = float(x3in)
117    y0 = float(y0in)
118    y1 = float(y1in)
119    y2 = float(y2in)
120    y3 = float(y3in)
121    IF arg_present(xxin) THEN BEGIN
122      xin = float(xxin)
123      yin = float(yyin)
124    ENDIF
125  ENDELSE
126;
127  IF keyword_set(double) THEN a = dblarr(8, n_elements(x0)) $
128  ELSE a = fltarr(8, n_elements(x0))
129;
130  delx3 = x0-x1+x2-x3
131  dely3 = y0-y1+y2-y3
132;
133  affinemap = where(delx3 EQ 0 AND dely3 EQ 0)
134  IF affinemap[0] NE -1 THEN BEGIN
135    xx0 = x0[affinemap]
136    xx1 = x1[affinemap]
137    xx2 = x2[affinemap]
138    yy0 = y0[affinemap]
139    yy1 = y1[affinemap]
140    yy2 = y2[affinemap]
141;
142    a[0, affinemap] = xx1-xx0
143    a[1, affinemap] = xx2-xx1
144    a[2, affinemap] = xx0
145    a[3, affinemap] = yy1-yy0
146    a[4, affinemap] = yy2-yy1
147    a[5, affinemap] = yy0
148    a[6, affinemap] = 0
149    a[7, affinemap] = 0
150  ENDIF
151;
152  projectivemap = where(delx3 NE 0 OR dely3 NE 0)
153  IF projectivemap[0] NE -1 THEN BEGIN
154    xx0 = x0[projectivemap]
155    xx1 = x1[projectivemap]
156    xx2 = x2[projectivemap]
157    xx3 = x3[projectivemap]
158    yy0 = y0[projectivemap]
159    yy1 = y1[projectivemap]
160    yy2 = y2[projectivemap]
161    yy3 = y3[projectivemap]
162;   
163    delx1 = xx1-xx2
164    dely1 = yy1-yy2
165    delx2 = xx3-xx2
166    dely2 = yy3-yy2
167    delx3 = delx3[projectivemap]
168    dely3 = dely3[projectivemap]
169;
170    div = delx1*dely2-dely1*delx2
171    zero = where(div EQ 0)
172    IF zero[0] NE -1 THEN BEGIN
173      stop
174    ENDIF
175    a13 = (delx3*dely2-dely3*delx2)/div
176    a23 = (delx1*dely3-dely1*delx3)/div
177;
178    a[0, projectivemap] = xx1-xx0+a13*xx1
179    a[1, projectivemap] = xx3-xx0+a23*xx3
180    a[2, projectivemap] = xx0
181    a[3, projectivemap] = yy1-yy0+a13*yy1
182    a[4, projectivemap] = yy3-yy0+a23*yy3
183    a[5, projectivemap] = yy0
184    a[6, projectivemap] = a13
185    a[7, projectivemap] = a23
186  ENDIF
187;   
188  IF NOT arg_present(xxin) THEN return, a
189;
190  IF n_elements(xin) EQ 1 THEN BEGIN
191    xin = replicate(xin, n_elements(x0))
192    yin = replicate(yin, n_elements(x0))
193  ENDIF
194;
195  IF keyword_set(double) THEN res = dblarr(2, n_elements(xin)) $
196  ELSE res = fltarr(2, n_elements(xin))
197  IF n_elements(x0) EQ 1 THEN BEGIN
198    div = a[6]*xin[*] + a[7]*yin[*] + 1
199    zero = where(div EQ 0)
200    IF zero[0] NE -1 THEN BEGIN
201      stop
202    ENDIF
203    res[0, *] = (a[0]*xin[*] + a[1]*yin[*] + a[2])/div
204    res[1, *] = (a[3]*xin[*] + a[4]*yin[*] + a[5])/div
205  ENDIF ELSE BEGIN
206    div = a[6, *]*xin +a[7, *]*yin + 1
207    zero = where(div EQ 0)
208    IF zero[0] NE -1 THEN BEGIN
209      stop
210    ENDIF
211    res[0, *] = (a[0, *]*xin[*] + a[1, *]*yin[*] + a[2, *])/div
212    res[1, *] = (a[3, *]*xin[*] + a[4, *]*yin[*] + a[5, *])/div
213  ENDELSE
214;
215  RETURN, res
216END
Note: See TracBrowser for help on using the repository browser.