b04cdd48ad1daa309ce5f02473771781fc2a201c
[platform/upstream/opencv.git] / modules / legacy / src / morphcontours.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43
44 #define PATH_TO_E       1
45 #define PATH_TO_SE      2
46 #define PATH_TO_S       3
47
48 #define K_S         2
49 #define E_S         2
50 #define C_S         .01
51 #define K_Z         5000
52 #define K_NM        50000
53 #define K_B         40
54 #define NULL_EDGE   0.001f
55 #define inf         DBL_MAX
56
57 typedef struct __CvWork
58 {
59     double w_east;
60     double w_southeast;
61     double w_south;
62     char path_e;
63     char path_se;
64     char path_s;
65 }_CvWork;
66
67
68 double _cvBendingWork(  CvPoint2D32f* B0,
69                         CvPoint2D32f* F0,
70                         CvPoint2D32f* B1,
71                         CvPoint2D32f* F1/*,
72                         CvPoint* K */);
73
74 double _cvStretchingWork(CvPoint2D32f* P1,
75                          CvPoint2D32f* P2);
76
77 void _cvWorkEast     (int i, int j, _CvWork** W, CvPoint2D32f* edges1, CvPoint2D32f* edges2);
78 void _cvWorkSouthEast(int i, int j, _CvWork** W, CvPoint2D32f* edges1, CvPoint2D32f* edges2);
79 void _cvWorkSouth    (int i, int j, _CvWork** W, CvPoint2D32f* edges1, CvPoint2D32f* edges2);
80
81 static CvPoint2D32f null_edge;
82
83 double _cvStretchingWork(CvPoint2D32f* P1,
84                          CvPoint2D32f* P2)
85 {
86     double L1,L2, L_min, dL;
87
88     L1 = sqrt( (double)P1->x*P1->x + P1->y*P1->y);
89     L2 = sqrt( (double)P2->x*P2->x + P2->y*P2->y);
90
91     L_min = MIN(L1, L2);
92     dL = fabs( L1 - L2 );
93
94     return K_S * pow( dL, E_S ) / ( L_min + C_S*dL );
95 }
96
97
98 ////////////////////////////////////////////////////////////////////////////////////
99 CvPoint2D32f Q( CvPoint2D32f q0, CvPoint2D32f q1, CvPoint2D32f q2, double t );
100 double angle( CvPoint2D32f A, CvPoint2D32f B );
101
102 double _cvBendingWork(  CvPoint2D32f* B0,
103                         CvPoint2D32f* F0,
104                         CvPoint2D32f* B1,
105                         CvPoint2D32f* F1/*,
106                         CvPoint* K*/)
107 {
108     CvPoint2D32f Q0, Q1, Q2;
109     CvPoint2D32f Q1_nm, Q2_nm;
110     double d0, d1, d2, des, t_zero;
111     double k_zero, k_nonmon;
112     CvPoint2D32f center;
113     double check01, check02;
114     char check_origin;
115     double d_angle, d_nm_angle;
116 /*
117     if( (B0->x==0) && (B0->y==0) )
118     {
119         if( (F0->x==0) && (F0->y==0) )
120         {
121             B1->x = -B1->x;
122             B1->y = -B1->y;
123
124             d_angle = acos( (B1->x*F1->x + B1->y*F1->y)/sqrt( (B1->x*B1->x + B1->y*B1->y)*(F1->x*F1->x + F1->y*F1->y) ) );
125             d_angle = CV_PI - d_angle;
126
127             B1->x = -B1->x;
128             B1->y = -B1->y;
129
130             //return d_angle*K_B;
131             return 100;
132         }
133         K->x = -K->x;
134         K->y = -K->y;
135         B1->x = -B1->x;
136         B1->y = -B1->y;
137
138         d_angle = acos( (B1->x*F1->x + B1->y*F1->y)/sqrt( (B1->x*B1->x + B1->y*B1->y)*(F1->x*F1->x + F1->y*F1->y) ) );
139         d_angle = d_angle - acos( (F0->x*K->x + F0->y*K->y)/sqrt( (F0->x*F0->x + F0->y*F0->y)*(K->x*K->x + K->y*K->y) ) );
140         d_angle = d_angle - CV_PI*0.5;
141         d_angle = fabs(d_angle);
142
143
144         K->x = -K->x;
145         K->y = -K->y;
146         B1->x = -B1->x;
147         B1->y = -B1->y;
148
149         //return d_angle*K_B;
150         return 100;
151     }
152
153
154     if( (F0->x==0) && (F0->y==0) )
155         {
156             K->x = -K->x;
157             K->y = -K->y;
158             B1->x = -B1->x;
159             B1->y = -B1->y;
160
161             d_angle = acos( (B1->x*F1->x + B1->y*F1->y)/sqrt( (B1->x*B1->x + B1->y*B1->y)*(F1->x*F1->x + F1->y*F1->y) ) );
162             d_angle = d_angle - acos( (B0->x*K->x + B0->y*K->y)/sqrt( (B0->x*B0->x + B0->y*B0->y)*(K->x*K->x + K->y*K->y) ) );
163             d_angle = d_angle - CV_PI*0.5;
164             d_angle = fabs(d_angle);
165
166             K->x = -K->x;
167             K->y = -K->y;
168             B1->x = -B1->x;
169             B1->y = -B1->y;
170
171             //return d_angle*K_B;
172             return 100;
173         }
174 ///////////////
175
176     if( (B1->x==0) && (B1->y==0) )
177     {
178         if( (F1->x==0) && (F1->y==0) )
179         {
180             B0->x = -B0->x;
181             B0->y = -B0->y;
182
183             d_angle = acos( (B0->x*F0->x + B0->y*F0->y)/sqrt( (B0->x*B0->x + B0->y*B0->y)*(F0->x*F0->x + F0->y*F0->y) ) );
184             d_angle = CV_PI - d_angle;
185
186             B0->x = -B0->x;
187             B0->y = -B0->y;
188
189             //return d_angle*K_B;
190             return 100;
191         }
192         K->x = -K->x;
193         K->y = -K->y;
194         B0->x = -B0->x;
195         B0->y = -B0->y;
196
197         d_angle = acos( (B0->x*F0->x + B0->y*F0->y)/sqrt( (B0->x*B0->x + B0->y*B0->y)*(F0->x*F0->x + F0->y*F0->y) ) );
198         d_angle = d_angle - acos( (F1->x*K->x + F1->y*K->y)/sqrt( (F1->x*F1->x + F1->y*F1->y)*(K->x*K->x + K->y*K->y) ) );
199         d_angle = d_angle - CV_PI*0.5;
200         d_angle = fabs(d_angle);
201
202         K->x = -K->x;
203         K->y = -K->y;
204         B0->x = -B0->x;
205         B0->y = -B0->y;
206
207         //return d_angle*K_B;
208         return 100;
209     }
210
211
212     if( (F1->x==0) && (F1->y==0) )
213         {
214             K->x = -K->x;
215             K->y = -K->y;
216             B0->x = -B0->x;
217             B0->y = -B0->y;
218
219             d_angle = acos( (B0->x*F0->x + B0->y*F0->y)/sqrt( (B0->x*B0->x + B0->y*B0->y)*(F0->x*F0->x + F0->y*F0->y) ) );
220             d_angle = d_angle - acos( (B1->x*K->x + B1->y*K->y)/sqrt( (B1->x*B1->x + B1->y*B1->y)*(K->x*K->x + K->y*K->y) ) );
221             d_angle = d_angle - CV_PI*0.5;
222             d_angle = fabs(d_angle);
223
224             K->x  = -K->x;
225             K->y  = -K->y;
226             B0->x = -B0->x;
227             B0->y = -B0->y;
228
229             //return d_angle*K_B;
230             return 100;
231         }
232
233 */
234
235 /*
236     B0->x = -B0->x;
237     B0->y = -B0->y;
238     B1->x = -B1->x;
239     B1->y = -B1->y;
240 */
241     Q0.x = F0->x * (-B0->x) + F0->y * (-B0->y);
242     Q0.y = F0->x * (-B0->y) - F0->y * (-B0->x);
243
244     Q1.x = 0.5f*( (F1->x * (-B0->x) + F1->y * (-B0->y)) + (F0->x * (-B1->x) + F0->y * (-B1->y)) );
245     Q1.y = 0.5f*( (F1->x * (-B0->y) - F1->y * (-B0->x)) + (F0->x * (-B1->y) - F0->y * (-B1->x)) );
246
247     Q2.x = F1->x * (-B1->x) + F1->y * (-B1->y);
248     Q2.y = F1->x * (-B1->y) - F1->y * (-B1->x);
249
250     d0 = Q0.x * Q1.y - Q0.y * Q1.x;
251     d1 = 0.5f*(Q0.x * Q2.y - Q0.y * Q2.x);
252     d2 = Q1.x * Q2.y - Q1.y * Q2.x;
253
254     // Check angles goes to zero
255     des = Q1.y*Q1.y - Q0.y*Q2.y;
256
257     k_zero = 0;
258
259     if( des >= 0 )
260     {
261         t_zero = ( Q0.y - Q1.y + sqrt(des) )/( Q0.y - 2*Q1.y + Q2.y );
262
263         if( (0 < t_zero) && (t_zero < 1) && ( Q(Q0, Q1, Q2, t_zero).x > 0 ) )
264         {
265             k_zero = inf;
266         }
267
268         t_zero = ( Q0.y - Q1.y - sqrt(des) )/( Q0.y - 2*Q1.y + Q2.y );
269
270         if( (0 < t_zero) && (t_zero < 1) && ( Q(Q0, Q1, Q2, t_zero).x > 0 ) )
271         {
272             k_zero = inf;
273         }
274     }
275
276     // Check nonmonotonic
277     des = d1*d1 - d0*d2;
278
279     k_nonmon = 0;
280
281     if( des >= 0 )
282     {
283         t_zero = ( d0 - d1 - sqrt(des) )/( d0 - 2*d1 + d2 );
284
285         if( (0 < t_zero) && (t_zero < 1) )
286         {
287             k_nonmon = 1;
288             Q1_nm = Q(Q0, Q1, Q2, t_zero);
289         }
290
291         t_zero = ( d0 - d1 + sqrt(des) )/( d0 - 2*d1 + d2 );
292
293         if( (0 < t_zero) && (t_zero < 1) )
294         {
295             k_nonmon += 2;
296             Q2_nm = Q(Q0, Q1, Q2, t_zero);
297         }
298     }
299
300     // Finde origin lie in Q0Q1Q2
301     check_origin = 1;
302
303     center.x = (Q0.x + Q1.x + Q2.x)/3;
304     center.y = (Q0.y + Q1.y + Q2.y)/3;
305
306     check01 = (center.x - Q0.x)*(Q1.y - Q0.y) + (center.y - Q0.y)*(Q1.x - Q0.x);
307     check02 = (-Q0.x)*(Q1.y - Q0.y) + (-Q0.y)*(Q1.x - Q0.x);
308     if( check01*check02 > 0 )
309     {
310         check01 = (center.x - Q1.x)*(Q2.y - Q1.y) + (center.y - Q1.y)*(Q2.x - Q1.x);
311         check02 = (-Q1.x)*(Q2.y - Q1.y) + (-Q1.y)*(Q2.x - Q1.x);
312         if( check01*check02 > 0 )
313         {
314             check01 = (center.x - Q2.x)*(Q0.y - Q2.y) + (center.y - Q2.y)*(Q0.x - Q2.x);
315             check02 = (-Q2.x)*(Q0.y - Q2.y) + (-Q2.y)*(Q0.x - Q2.x);
316             if( check01*check02 > 0 )
317             {
318                 check_origin = 0;
319             }
320         }
321     }
322
323     // Calculate angle
324     d_nm_angle = 0;
325     d_angle = angle(Q0,Q2);
326     if( k_nonmon == 0 )
327     {
328         if( check_origin == 0 )
329         {
330         }
331         else
332         {
333             d_angle = 2*CV_PI - d_angle;
334         }
335     }
336     else
337     {
338         if( k_nonmon == 1 )
339         {
340             d_nm_angle = angle(Q0,Q1_nm);
341             if(d_nm_angle > d_angle)
342             {
343                 d_nm_angle = d_nm_angle - d_angle;
344             }
345         }
346
347         if( k_nonmon == 2 )
348         {
349             d_nm_angle = angle(Q0,Q2_nm);
350             if(d_nm_angle > d_angle)
351             {
352                 d_nm_angle = d_nm_angle - d_angle;
353             }
354         }
355
356         if( k_nonmon == 3 )
357         {
358             d_nm_angle = angle(Q0,Q1_nm);
359             if(d_nm_angle > d_angle)
360             {
361                 d_nm_angle = d_nm_angle - d_angle;
362                 d_nm_angle = d_nm_angle + angle(Q0, Q2_nm);
363             }
364             else
365             {
366                 d_nm_angle = d_nm_angle + angle(Q2,Q2_nm);
367             }
368         }
369     }
370 /*
371     B0->x = -B0->x;
372     B0->y = -B0->y;
373     B1->x = -B1->x;
374     B1->y = -B1->y;
375 */
376     return d_angle*K_B + d_nm_angle*K_NM + k_zero*K_Z;
377     //return 0;
378 }
379
380
381 /////////////////////////////////////////////////////////////////////////////////
382 void _cvWorkEast(int i, int j, _CvWork** W, CvPoint2D32f* edges1, CvPoint2D32f* edges2)
383 {
384     double w1,w2;
385     CvPoint2D32f small_edge;
386
387     //W[i,j].w_east
388     w1 = W[i-1][j].w_east /*+ _cvBendingWork(   &edges1[i-2],
389                                             &edges1[i-1],
390                                             &null_edge ,
391                                             &null_edge,
392                                             NULL)*/;
393
394     small_edge.x = NULL_EDGE*edges1[i-1].x;
395     small_edge.y = NULL_EDGE*edges1[i-1].y;
396
397     w2 = W[i-1][j].w_southeast + _cvBendingWork(&edges1[i-2],
398                                                 &edges1[i-1],
399                                                 &edges2[j-1],
400                                                 /*&null_edge*/&small_edge/*,
401                                                 &edges2[j]*/);
402
403     if(w1<w2)
404     {
405         W[i][j].w_east = w1 + _cvStretchingWork( &edges1[i-1], &null_edge );
406         W[i][j].path_e = PATH_TO_E;
407     }
408     else
409     {
410         W[i][j].w_east = w2 + _cvStretchingWork( &edges1[i-1], &null_edge );
411         W[i][j].path_e = PATH_TO_SE;
412     }
413 }
414
415
416
417
418
419 ////////////////////////////////////////////////////////////////////////////////////
420 void _cvWorkSouthEast(int i, int j, _CvWork** W, CvPoint2D32f* edges1, CvPoint2D32f* edges2)
421 {
422     double w1,w2,w3;
423     CvPoint2D32f small_edge;
424
425     //W[i,j].w_southeast
426     small_edge.x = NULL_EDGE*edges1[i-2].x;
427     small_edge.y = NULL_EDGE*edges1[i-2].y;
428
429     w1 = W[i-1][j-1].w_east + _cvBendingWork(&edges1[i-2],
430                                             &edges1[i-1],
431                                             /*&null_edge*/&small_edge,
432                                             &edges2[j-1]/*,
433                                             &edges2[j-2]*/);
434
435     w2 = W[i-1][j-1].w_southeast + _cvBendingWork(  &edges1[i-2],
436                                                     &edges1[i-1],
437                                                     &edges2[j-2],
438                                                     &edges2[j-1]/*,
439                                                     NULL*/);
440
441     small_edge.x = NULL_EDGE*edges2[j-2].x;
442     small_edge.y = NULL_EDGE*edges2[j-2].y;
443
444     w3 = W[i-1][j-1].w_south + _cvBendingWork(  /*&null_edge*/&small_edge,
445                                                 &edges1[i-1],
446                                                 &edges2[j-2],
447                                                 &edges2[j-1]/*,
448                                                 &edges1[i-2]*/);
449
450     if( w1<w2 )
451     {
452         if(w1<w3)
453         {
454             W[i][j].w_southeast = w1 + _cvStretchingWork( &edges1[i-1], &edges2[j-1] );
455             W[i][j].path_se = PATH_TO_E;
456         }
457         else
458         {
459             W[i][j].w_southeast = w3 + _cvStretchingWork( &edges1[i-1], &edges2[j-1] );
460             W[i][j].path_se = 3;
461         }
462     }
463     else
464     {
465         if( w2<w3)
466         {
467             W[i][j].w_southeast = w2 + _cvStretchingWork( &edges1[i-1], &edges2[j-1] );
468             W[i][j].path_se = PATH_TO_SE;
469         }
470         else
471         {
472             W[i][j].w_southeast = w3 + _cvStretchingWork( &edges1[i-1], &edges2[j-1] );
473             W[i][j].path_se = 3;
474         }
475     }
476 }
477
478
479 //////////////////////////////////////////////////////////////////////////////////////
480 void _cvWorkSouth(int i, int j, _CvWork** W, CvPoint2D32f* edges1, CvPoint2D32f* edges2)
481 {
482     double w1,w2;
483     CvPoint2D32f small_edge;
484
485     //W[i,j].w_south
486
487     small_edge.x = NULL_EDGE*edges2[j-1].x;
488     small_edge.y = NULL_EDGE*edges2[j-1].y;
489
490     w1 = W[i][j-1].w_southeast + _cvBendingWork(&edges1[i-1],
491                                                 /*&null_edge*/&small_edge,
492                                                 &edges2[j-2],
493                                                 &edges2[j-1]/*,
494                                                 &edges1[i]*/);
495
496     w2 = W[i][j-1].w_south /*+ _cvBendingWork(  &null_edge ,
497                                             &null_edge,
498                                             &edges2[j-2],
499                                             &edges2[j-1],
500                                             NULL)*/;
501
502     if( w1<w2 )
503     {
504         W[i][j].w_south = w1 + _cvStretchingWork( &null_edge, &edges2[j-1] );
505         W[i][j].path_s = PATH_TO_SE;
506     }
507     else
508     {
509         W[i][j].w_south = w2 + _cvStretchingWork( &null_edge, &edges2[j-1] );
510         W[i][j].path_s = 3;
511     }
512 }
513
514
515 //===================================================
516 CvPoint2D32f Q(CvPoint2D32f q0,CvPoint2D32f q1,CvPoint2D32f q2,double t)
517 {
518     CvPoint2D32f q;
519
520     q.x = (float)(q0.x*(1-t)*(1-t) + 2*q1.x*t*(1-t) + q2.x*t*t);
521     q.y = (float)(q0.y*(1-t)*(1-t) + 2*q1.y*t*(1-t) + q2.y*t*t);
522
523     return q;
524 }
525
526 double angle(CvPoint2D32f A, CvPoint2D32f B)
527 {
528     return acos( (A.x*B.x + A.y*B.y)/sqrt( (double)(A.x*A.x + A.y*A.y)*(B.x*B.x + B.y*B.y) ) );
529 }
530 #if 0
531 /***************************************************************************************\
532 *
533 *   This function compute intermediate polygon between contour1 and contour2
534 *
535 *   Correspondence between points of contours specify by corr
536 *
537 *   param = [0,1];  0 correspondence to contour1, 1 - contour2
538 *
539 \***************************************************************************************/
540 static CvSeq* icvBlendContours(CvSeq* contour1,
541                         CvSeq* contour2,
542                         CvSeq* corr,
543                         double param,
544                         CvMemStorage* storage)
545 {
546     int j;
547
548     CvSeqWriter writer01;
549     CvSeqReader reader01;
550
551     int Ni,Nj;              // size of contours
552     int i;                  // counter
553
554     CvPoint* point1;        // array of first contour point
555     CvPoint* point2;        // array of second contour point
556
557     CvPoint point_output;   // intermediate storage of ouput point
558
559     int corr_point;
560
561     // Create output sequence.
562     CvSeq* output = cvCreateSeq(0,
563                                 sizeof(CvSeq),
564                                 sizeof(CvPoint),
565                                 storage );
566
567     // Find size of contours.
568     Ni = contour1->total + 1;
569     Nj = contour2->total + 1;
570
571     point1 = (CvPoint* )malloc( Ni*sizeof(CvPoint) );
572     point2 = (CvPoint* )malloc( Nj*sizeof(CvPoint) );
573
574     // Initialize arrays of point
575     cvCvtSeqToArray( contour1, point1, CV_WHOLE_SEQ );
576     cvCvtSeqToArray( contour2, point2, CV_WHOLE_SEQ );
577
578     // First and last point mast be equal.
579     point1[Ni-1] = point1[0];
580     point2[Nj-1] = point2[0];
581
582     // Initializes process of writing to sequence.
583     cvStartAppendToSeq( output, &writer01);
584
585     i = Ni-1; //correspondence to points of contour1
586     for( ; corr; corr = corr->h_next )
587     {
588         //Initializes process of sequential reading from sequence
589         cvStartReadSeq( corr, &reader01, 0 );
590
591         for(j=0; j < corr->total; j++)
592         {
593             // Read element from sequence.
594             CV_READ_SEQ_ELEM( corr_point, reader01 );
595
596             // Compute point of intermediate polygon.
597             point_output.x = cvRound(point1[i].x + param*( point2[corr_point].x - point1[i].x ));
598             point_output.y = cvRound(point1[i].y + param*( point2[corr_point].y - point1[i].y ));
599
600             // Write element to sequence.
601             CV_WRITE_SEQ_ELEM( point_output, writer01 );
602         }
603         i--;
604     }
605     // Updates sequence header.
606     cvFlushSeqWriter( &writer01 );
607
608     return output;
609 }
610
611 /**************************************************************************************************
612 *
613 *
614 *
615 *
616 *
617 *
618 *
619 *
620 *
621 *
622 **************************************************************************************************/
623
624
625 static void icvCalcContoursCorrespondence(CvSeq* contour1,
626                                    CvSeq* contour2,
627                                    CvSeq** corr,
628                                    CvMemStorage* storage)
629 {
630     int i,j;                    // counter of cycles
631     int Ni,Nj;                  // size of contours
632     _CvWork** W;                // graph for search minimum of work
633
634     CvPoint* point1;            // array of first contour point
635     CvPoint* point2;            // array of second contour point
636     CvPoint2D32f* edges1;       // array of first contour edge
637     CvPoint2D32f* edges2;       // array of second contour edge
638
639     //CvPoint null_edge = {0,0};    //
640     CvPoint2D32f small_edge;
641     //double inf;                   // infinity
642
643     CvSeq* corr01;
644     CvSeqWriter writer;
645
646     char path;                  //
647
648     // Find size of contours
649     Ni = contour1->total + 1;
650     Nj = contour2->total + 1;
651
652     // Create arrays
653     W = (_CvWork**)malloc(sizeof(_CvWork*)*Ni);
654     for(i=0; i<Ni; i++)
655     {
656         W[i] = (_CvWork*)malloc(sizeof(_CvWork)*Nj);
657     }
658
659     point1 = (CvPoint* )malloc( Ni*sizeof(CvPoint) );
660     point2 = (CvPoint* )malloc( Nj*sizeof(CvPoint) );
661     edges1 = (CvPoint2D32f* )malloc( (Ni-1)*sizeof(CvPoint2D32f) );
662     edges2 = (CvPoint2D32f* )malloc( (Nj-1)*sizeof(CvPoint2D32f) );
663
664     // Initialize arrays of point
665     cvCvtSeqToArray( contour1, point1, CV_WHOLE_SEQ );
666     cvCvtSeqToArray( contour2, point2, CV_WHOLE_SEQ );
667
668     point1[Ni-1] = point1[0];
669     point2[Nj-1] = point2[0];
670
671     for(i=0;i<Ni-1;i++)
672     {
673         edges1[i].x = (float)( point1[i+1].x - point1[i].x );
674         edges1[i].y = (float)( point1[i+1].y - point1[i].y );
675     };
676
677     for(i=0;i<Nj-1;i++)
678     {
679         edges2[i].x = (float)( point2[i+1].x - point2[i].x );
680         edges2[i].y = (float)( point2[i+1].y - point2[i].y );
681     };
682
683     // Find infinity constant
684     //inf=1;
685 /////////////
686
687 //Find min path in graph
688
689 /////////////
690     W[0][0].w_east      = 0;
691     W[0][0].w_south     = 0;
692     W[0][0].w_southeast = 0;
693
694     W[1][1].w_southeast = _cvStretchingWork( &edges1[0], &edges2[0] );
695     W[1][1].w_east = inf;
696     W[1][1].w_south = inf;
697     W[1][1].path_se = PATH_TO_SE;
698
699     W[0][1].w_south =  _cvStretchingWork( &null_edge, &edges2[0] );
700     W[0][1].path_s = 3;
701     W[1][0].w_east =  _cvStretchingWork( &edges2[0], &null_edge );
702     W[1][0].path_e = PATH_TO_E;
703
704     for( i=1; i<Ni; i++ )
705     {
706         W[i][0].w_south     = inf;
707         W[i][0].w_southeast = inf;
708     }
709
710     for(j=1; j<Nj; j++)
711     {
712         W[0][j].w_east      = inf;
713         W[0][j].w_southeast = inf;
714     }
715
716     for(i=2; i<Ni; i++)
717     {
718         j=0;/////////
719         W[i][j].w_east = W[i-1][j].w_east;
720         W[i][j].w_east = W[i][j].w_east /*+
721             _cvBendingWork( &edges1[i-2], &edges1[i-1], &null_edge, &null_edge, NULL )*/;
722         W[i][j].w_east = W[i][j].w_east + _cvStretchingWork( &edges2[i-1], &null_edge );
723         W[i][j].path_e = PATH_TO_E;
724
725         j=1;//////////
726         W[i][j].w_south = inf;
727
728         _cvWorkEast (i, j, W, edges1, edges2);
729
730         W[i][j].w_southeast = W[i-1][j-1].w_east;
731         W[i][j].w_southeast = W[i][j].w_southeast + _cvStretchingWork( &edges1[i-1], &edges2[j-1] );
732
733         small_edge.x = NULL_EDGE*edges1[i-2].x;
734         small_edge.y = NULL_EDGE*edges1[i-2].y;
735
736         W[i][j].w_southeast = W[i][j].w_southeast +
737             _cvBendingWork( &edges1[i-2], &edges1[i-1], /*&null_edge*/&small_edge, &edges2[j-1]/*, &edges2[Nj-2]*/);
738
739         W[i][j].path_se = PATH_TO_E;
740     }
741
742     for(j=2; j<Nj; j++)
743     {
744         i=0;//////////
745         W[i][j].w_south = W[i][j-1].w_south;
746         W[i][j].w_south = W[i][j].w_south + _cvStretchingWork( &null_edge, &edges2[j-1] );
747         W[i][j].w_south = W[i][j].w_south /*+
748             _cvBendingWork( &null_edge, &null_edge, &edges2[j-2], &edges2[j-1], NULL )*/;
749         W[i][j].path_s = 3;
750
751         i=1;///////////
752         W[i][j].w_east= inf;
753
754         _cvWorkSouth(i, j, W, edges1, edges2);
755
756         W[i][j].w_southeast = W[i-1][j-1].w_south;
757         W[i][j].w_southeast = W[i][j].w_southeast + _cvStretchingWork( &edges1[i-1], &edges2[j-1] );
758
759         small_edge.x = NULL_EDGE*edges2[j-2].x;
760         small_edge.y = NULL_EDGE*edges2[j-2].y;
761
762         W[i][j].w_southeast = W[i][j].w_southeast +
763             _cvBendingWork( /*&null_edge*/&small_edge, &edges1[i-1], &edges2[j-2], &edges2[j-1]/*, &edges1[Ni-2]*/);
764         W[i][j].path_se = 3;
765     }
766
767     for(i=2; i<Ni; i++)
768         for(j=2; j<Nj; j++)
769         {
770             _cvWorkEast     (i, j, W, edges1, edges2);
771             _cvWorkSouthEast(i, j, W, edges1, edges2);
772             _cvWorkSouth    (i, j, W, edges1, edges2);
773         }
774
775     i=Ni-1;j=Nj-1;
776
777     *corr = cvCreateSeq(0,
778                         sizeof(CvSeq),
779                         sizeof(int),
780                         storage );
781
782     corr01 = *corr;
783     cvStartAppendToSeq( corr01, &writer );
784     if( W[i][j].w_east > W[i][j].w_southeast )
785         {
786             if( W[i][j].w_southeast > W[i][j].w_south )
787             {
788                 path = 3;
789             }
790             else
791             {
792                 path = PATH_TO_SE;
793             }
794         }
795         else
796         {
797             if( W[i][j].w_east < W[i][j].w_south )
798             {
799                 path = PATH_TO_E;
800             }
801             else
802             {
803                 path = 3;
804             }
805         }
806     do
807     {
808         CV_WRITE_SEQ_ELEM( j, writer );
809
810         switch( path )
811         {
812         case PATH_TO_E:
813             path = W[i][j].path_e;
814             i--;
815             cvFlushSeqWriter( &writer );
816             corr01->h_next = cvCreateSeq(   0,
817                                             sizeof(CvSeq),
818                                             sizeof(int),
819                                             storage );
820             corr01 = corr01->h_next;
821             cvStartAppendToSeq( corr01, &writer );
822             break;
823
824         case PATH_TO_SE:
825             path = W[i][j].path_se;
826             j--; i--;
827             cvFlushSeqWriter( &writer );
828             corr01->h_next = cvCreateSeq(   0,
829                                             sizeof(CvSeq),
830                                             sizeof(int),
831                                             storage );
832             corr01 = corr01->h_next;
833             cvStartAppendToSeq( corr01, &writer );
834             break;
835
836         case 3:
837             path = W[i][j].path_s;
838             j--;
839             break;
840         }
841
842     } while( (i>=0) && (j>=0) );
843     cvFlushSeqWriter( &writer );
844
845     // Free memory
846     for(i=1;i<Ni;i++)
847     {
848         free(W[i]);
849     }
850     free(W);
851     free(point1);
852     free(point2);
853     free(edges1);
854     free(edges2);
855 }
856 #endif