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

Last change on this file since 133 was 125, checked in by pinsard, 18 years ago

improvements of Interpolation/*.pro header

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