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

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

add $ in Calendar, Grid, Interpolation, Obsolete and Postscript *.pro files, add svn:keywords Id to all these files, some improvements in header

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