source: trunk/SRC/Interpolation/quadrilateral2square.pro @ 186

Last change on this file since 186 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1;+
2;
3; @file_comments
4; 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; @categories
16; Picture, Grid
17;
18; @param x0in {in}{required}
19; @param y0in {in}{required}
20; @param x1in {in}{required}
21; @param y1in {in}{required}
22; @param x2in {in}{required}
23; @param y2in {in}{required}
24; @param x3in {in}{required}
25; @param y3in  {in}{required}
26; the coordinates of the quadrilateral
27; (see above for correspondence 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;
31; @param xxin {in}{required}
32; the coordinates of the point(s) for which we want to do the mapping.
33; Can be scalar or array.
34;
35; @param yyin {in}{required}
36; the coordinates of the point(s) for which we want to do the mapping.
37; Can be scalar or array.
38;
39; @keyword PERF
40;
41; @returns
42;
43; (2,n) array: the new coordinates (xout, yout) of the (xin,yin) point(s) after
44; mapping.
45; If xin is a scalar, then n is equal to the number of elements of x0.
46; If xin is an array , then n is equal to the number of elements of xin.
47;
48; @restrictions
49; I think degenerated quadrilateral (e.g. flat of twisted) is not work.
50; This has to be tested.
51;
52; @examples
53;
54; IDL> splot,[0,5],[0,3],/nodata,xstyle=1,ystyle=1
55; IDL> tracegrille, findgen(11)*.1, findgen(11)*.1,color=indgen(12)*20
56; IDL> xin = (findgen(11)*.1)#replicate(1, 11)
57; IDL> yin = replicate(1, 11)#(findgen(11)*.1)
58; IDL> out = square2quadrilateral(2,1,3,0,5,1,2,3, xin, yin)
59; IDL> tracegrille, reform(out[0,*],11,11), reform(out[1,*],11,11),color=indgen(12)*20
60;
61; IDL> inorg=quadrilateral2square(2,1,3,0,5,1,2,3,out[0,*],out[1,*])
62; IDL> tracegrille, reform(inorg[0,*],11,11), reform(inorg[1,*],11,11),color=indgen(12)*20
63;
64; @history
65;      Sebastien Masson (smasson\@lodyc.jussieu.fr)
66;      August 2003
67;      Based on "Digital Image Warping" by G. Wolberg
68;      IEEE Computer Society Press, Los Alamitos, California
69;      Chapter 3, see p 52-56
70;
71;
72; @version $Id$
73;
74;-
75;------------------------------------------------------------
76;------------------------------------------------------------
77;------------------------------------------------------------
78FUNCTION quadrilateral2square, x0in, y0in, x1in, y1in, x2in, y2in, x3in, y3in, xxin, yyin, PERF = perf
79;
80;
81  compile_opt idl2, strictarrsubs
82;
83tempsone = systime(1)
84;
85; Warning, wrong definition of (x2,y2) and (x3,y3) at the bottom of
86; page 54 of Wolberg's book, see figure 3.7 page 56 for the good
87; definition.
88;
89  IF keyword_set(double) THEN BEGIN
90    x0 = double(x0in)
91    x1 = double(x1in)
92    x2 = double(x2in)
93    x3 = double(x3in)
94    y0 = double(y0in)
95    y1 = double(y1in)
96    y2 = double(y2in)
97    y3 = double(y3in)
98    xin = double(xxin)
99    yin = double(yyin)
100  ENDIF ELSE BEGIN
101    x0 = float(x0in)
102    x1 = float(x1in)
103    x2 = float(x2in)
104    x3 = float(x3in)
105    y0 = float(y0in)
106    y1 = float(y1in)
107    y2 = float(y2in)
108    y3 = float(y3in)
109    xin = float(xxin)
110    yin = float(yyin)
111  ENDELSE
112;
113; get the matrix A
114;
115  a = square2quadrilateral(x0in, y0in, x1in, y1in, x2in, y2in, x3in, y3in)
116;
117; compute the adjoint matrix
118;
119  IF keyword_set(double) THEN adj = dblarr(9, n_elements(x0)) $
120  ELSE adj = fltarr(9, n_elements(x0))
121;
122  adj[0, *] = a[4, *]        -a[7, *]*a[5, *]
123  adj[1, *] = a[7, *]*a[2, *]-a[1, *]
124  adj[2, *] = a[1, *]*a[5, *]-a[4, *]*a[2, *]
125  adj[3, *] = a[6, *]*a[5, *]-a[3, *]
126  adj[4, *] = a[0, *]        -a[6, *]*a[2, *]
127  adj[5, *] = a[3, *]*a[2, *]-a[0, *]*a[5, *]
128  adj[6, *] = a[3, *]*a[7, *]-a[6, *]*a[4, *]
129  adj[7, *] = a[6, *]*a[1, *]-a[0, *]*a[7, *]
130  adj[8, *] = a[0, *]*a[4, *]-a[3, *]*a[1, *]
131;
132  IF n_elements(xin) EQ 1 THEN BEGIN
133    xin = replicate(xin, n_elements(x0))
134    yin = replicate(yin, n_elements(x0))
135  ENDIF
136;
137; compute xprime, yprime and wprime
138;
139  IF n_elements(x0) EQ 1 THEN BEGIN
140    wpr = 1./(adj[6]*xin + adj[7]*yin + adj[8])
141  ENDIF ELSE BEGIN
142    wpr = 1./(adj[6, *]*xin + adj[7, *]*yin + adj[8, *])
143  ENDELSE
144  xpr = xin*wpr
145  ypr = yin*wpr
146;
147  IF keyword_set(double) THEN res = dblarr(2, n_elements(xin)) $
148  ELSE res = fltarr(2, n_elements(xin))
149;
150  IF n_elements(x0) EQ 1 THEN BEGIN
151    res[0, *] = xpr*adj[0] + ypr*adj[1] +wpr*adj[2]
152    res[1, *] = xpr*adj[3] + ypr*adj[4] +wpr*adj[5]
153  ENDIF ELSE BEGIN
154    res[0, *] = xpr*adj[0, *] + ypr*adj[1, *] +wpr*adj[2, *]
155    res[1, *] = xpr*adj[3, *] + ypr*adj[4, *] +wpr*adj[5, *]
156  ENDELSE
157;
158  IF keyword_set(perf) THEN print, 'time quadrilateral2square', systime(1)-tempsone
159
160  RETURN, res
161END
Note: See TracBrowser for help on using the repository browser.