Update the changelog
[opencv] / apps / vmdemotk / DataandMorphing.cpp
1 #include "DataandMorphing.h"
2 #include "VMDemoTk.h"
3 #include "Globals.h"
4 #include <highgui.h>
5
6 //Static vars
7 static CvMatrix3   matrix;
8 IplImage* datal = 0;
9 IplImage* datar = 0;
10 static IplImage* datares = 0;
11
12
13 static int*    runs_1;
14 static int*    runs_2;
15
16 static uchar*  buffer_1;
17 static uchar*  buffer_2;
18 static uchar*  morph_buffer;
19
20
21 static int*    num_runs_1;
22 static int*    num_runs_2;
23
24 static int*    corr_1;
25 static int*    corr_2;
26
27 static int*    lens_1, *lens_2;
28
29 static int*    morph_lens;
30
31
32 ///////////////////////////////////////////////////////////////////////////////
33 int MemoryAllocation()
34 {
35     int numlines;
36     cvMakeScanlines( &matrix, imgsize, 0, 0, 0, 0, &numlines);
37
38     scanlines_1 = (int*)(calloc( numlines * 2, sizeof(int) * 4));
39     scanlines_2 = (int*)(calloc( numlines * 2, sizeof(int) * 4));
40     scanlines_a = (int*)(calloc( numlines * 2, sizeof(int) * 4));
41
42     lens_1 = (int*)(calloc( numlines * 2, sizeof(int)*4));
43     lens_2 = (int*)(calloc( numlines * 2, sizeof(int)*4));
44     morph_lens = (int*)(calloc( numlines * 2, sizeof(int)*4));
45
46     runs_1 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
47     runs_2 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
48
49     corr_1 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
50     corr_2 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
51
52     num_runs_1 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
53     num_runs_2 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
54
55     buffer_1 = (uchar*)(malloc(imgsize.width * (imgsize.height+1) * 2 * sizeof(int)));
56
57     buffer_2 = (uchar*)(malloc(imgsize.width * (imgsize.height+1) * 2 * sizeof(int)));
58
59     morph_buffer = (uchar*)(calloc(imgsize.width * (imgsize.height+1), 2 * sizeof(int)));
60     return 0;
61 }
62
63 ///////////////////////////////////////////////////////////////////////////////////
64 int loaddata (Tcl_Interp  *interp)
65 {
66     int   tx, tx1, ty, ty1;
67
68     //datal = cvvLoadImage(
69
70     tx = datal->width;
71     tx1 = datar->width;
72     ty = datal->height;
73     ty1 = datar->height;
74
75     if( datares )
76         cvReleaseImage( &datares );
77
78     datares = cvCreateImage( cvSize( tx, ty ), IPL_DEPTH_8U, 3 );
79
80     if(tx !=tx1 || ty != ty1) return 2;
81
82     Tcl_Eval(interp, "readini $var FundMatrix {F[0][0]}");
83     (matrix).m[0][0] = (float)atof( (*interp).result );
84
85     Tcl_Eval(interp, "readini $var FundMatrix {F[0][1]}");
86     (matrix).m[0][1] = (float)atof( (*interp).result );
87
88     Tcl_Eval(interp, "readini $var FundMatrix {F[0][2]}");
89     (matrix).m[0][2] = (float)atof( (*interp).result );
90
91     Tcl_Eval(interp, "readini $var FundMatrix {F[1][0]}");
92     (matrix).m[1][0] = (float)atof( (*interp).result );
93
94     Tcl_Eval(interp, "readini $var FundMatrix {F[1][1]}");
95     (matrix).m[1][1] = (float)atof( (*interp).result );
96
97     Tcl_Eval(interp, "readini $var FundMatrix {F[1][2]}");
98     (matrix).m[1][2] = (float)atof( (*interp).result );
99
100     Tcl_Eval(interp, "readini $var FundMatrix {F[2][0]}");
101     (matrix).m[2][0] = (float)atof( (*interp).result );
102
103     Tcl_Eval(interp, "readini $var FundMatrix {F[2][1]}");
104     (matrix).m[2][1] = (float)atof( (*interp).result );
105
106     Tcl_Eval(interp, "readini $var FundMatrix {F[2][2]}");
107     (matrix).m[2][2] = (float)atof( (*interp).result );
108
109     return 0;
110 }
111
112 ///////////////////////////////////////////////////////////////////////////////
113 int OnMakeMorphing(float sk,Tcl_Interp  *interp)
114 {
115     CvMatrix3  matrix1;
116     IplImage *image1, *image2, *imageRes;
117
118     matrix1 = matrix;
119
120     image1   = datal;
121     image2   = datar;
122     imageRes = datares;
123
124     cvMakeScanlines( &matrix1, imgsize, 0, 0, 0, 0, &numScanlines);
125
126     if( !HaveMorph )
127     {
128         cvMakeScanlines( &matrix1, imgsize, scanlines_1,
129                          scanlines_2, lens_1, lens_2, &numScanlines);
130
131         cvPreWarpImage( numScanlines, image1, buffer_1, 
132                         lens_1, scanlines_1);
133
134         /* Prewarp second image */
135         cvPreWarpImage( numScanlines, image2, 
136                         buffer_2, lens_2, scanlines_2);
137
138         cvFindRuns( numScanlines, buffer_1,   /* prewarp image                */
139                     buffer_2, lens_1,     /* prewarp line lens in pixels  */
140                     lens_2, runs_1,     /* first runs                   */
141                     runs_2,
142                     num_runs_1, /* number of first runs         */
143                     num_runs_2);
144
145         cvDynamicCorrespondMulti(
146                          numScanlines,         // number of scanlines
147                          runs_1,         // s0|w0|s1|w1|...
148                          num_runs_1,    // numbers of runs
149                          runs_2,
150                          num_runs_2,
151                          corr_1,    // s0'|e0'|s1'|e1'|...
152                          corr_2 );
153
154
155         HaveMorph   = true;
156     } // if(!HaveMorph)
157
158
159     cvMakeAlphaScanlines(    scanlines_1,
160                              scanlines_2,
161                              scanlines_a,
162                              morph_lens,
163                              numScanlines,
164                              sk);
165
166
167     cvMorphEpilinesMulti(
168                         numScanlines,       /* number of lines                              */
169                              buffer_1,       /* raster epilines from the first image         */
170                              lens_1,         /* number of pixel in first line                */
171                              buffer_2,       /* raster epilines from the second image        */
172                              lens_2,         /* number of pixel in second line               */
173                              morph_buffer ,   /* raster epiline from the destination image    */
174                                         /* (it's an output parameter)                   */
175                              morph_lens,     /* numbers of pixel in output line              */
176                              sk,          /* relative position of camera                  */
177                              runs_1,         /* first sequence of runs                       */
178                              num_runs_1,     /* it's length                                  */
179                              runs_2,         /* second sequence of runs                      */
180                              num_runs_2,     /*                                              */
181                              corr_1,         /* correspond information for the 1st seq       */
182                              corr_2);        /* correspond information for the 2nd seq       */
183
184     cvPostWarpImage(
185                         numScanlines,                       /* number of scanlines  */
186                              morph_buffer,                   /* source buffers       */
187                              morph_lens,                     /* lens of buffers      */
188                              imageRes,       /* dest image           */
189                              scanlines_a                     /* scanline             */
190                                     );
191
192     cvDeleteMoire(  imageRes);
193     cvvShowImage( "result", imageRes );
194
195     return 0;
196 }
197
198
199
200 //////////////////////////////////////////////////////////////////////////////////
201
202 int FreeMemory()
203 {
204     if (scanlines_1     != 0)   free (scanlines_1);
205     if (scanlines_2     != 0)   free (scanlines_2);
206     if (scanlines_a     != 0)   free (scanlines_a);
207
208     if (lens_1          != 0)   free (lens_1);
209     if (lens_2          != 0)   free (lens_2);
210     if (morph_lens      != 0)   free (morph_lens);
211
212     if (buffer_1        != 0)   free (buffer_1);
213     if (buffer_2        != 0)   free (buffer_2);
214     if (morph_buffer    != 0)   free (morph_buffer);
215
216     if (runs_1          != 0)   free (runs_1);
217     if (runs_2          != 0)   free (runs_2);
218
219     if (corr_1          != 0)   free (corr_1);
220     if (corr_2          != 0)   free (corr_2);
221
222     if (num_runs_1      != 0)   free (num_runs_1);
223     if (num_runs_2      != 0)   free (num_runs_2);
224
225
226     scanlines_1     = 0;
227     scanlines_2     = 0;
228     scanlines_a     = 0;
229
230     lens_1          = 0;
231     lens_2          = 0;
232     morph_lens      = 0;
233
234     runs_1          = 0;
235     runs_2          = 0;
236
237     buffer_1        = 0;
238     buffer_2        = 0;
239     morph_buffer    = 0;
240
241     num_runs_1      = 0;
242     num_runs_2      = 0;
243
244     corr_1          = 0;
245     corr_2          = 0;
246
247     return 0;
248 }