forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontours.cpp
More file actions
1949 lines (1625 loc) · 60.9 KB
/
contours.cpp
File metadata and controls
1949 lines (1625 loc) · 60.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#include "opencv2/core/hal/intrin.hpp"
/* initializes 8-element array for fast access to 3x3 neighborhood of a pixel */
#define CV_INIT_3X3_DELTAS( deltas, step, nch ) \
((deltas)[0] = (nch), (deltas)[1] = -(step) + (nch), \
(deltas)[2] = -(step), (deltas)[3] = -(step) - (nch), \
(deltas)[4] = -(nch), (deltas)[5] = (step) - (nch), \
(deltas)[6] = (step), (deltas)[7] = (step) + (nch))
static const CvPoint icvCodeDeltas[8] =
{ CvPoint(1, 0), CvPoint(1, -1), CvPoint(0, -1), CvPoint(-1, -1), CvPoint(-1, 0), CvPoint(-1, 1), CvPoint(0, 1), CvPoint(1, 1) };
CV_IMPL void
cvStartReadChainPoints( CvChain * chain, CvChainPtReader * reader )
{
int i;
if( !chain || !reader )
CV_Error( CV_StsNullPtr, "" );
if( chain->elem_size != 1 || chain->header_size < (int)sizeof(CvChain))
CV_Error( CV_StsBadSize, "" );
cvStartReadSeq( (CvSeq *) chain, (CvSeqReader *) reader, 0 );
reader->pt = chain->origin;
for( i = 0; i < 8; i++ )
{
reader->deltas[i][0] = (schar) icvCodeDeltas[i].x;
reader->deltas[i][1] = (schar) icvCodeDeltas[i].y;
}
}
/* retrieves next point of the chain curve and updates reader */
CV_IMPL CvPoint
cvReadChainPoint( CvChainPtReader * reader )
{
schar *ptr;
int code;
CvPoint pt;
if( !reader )
CV_Error( CV_StsNullPtr, "" );
pt = reader->pt;
ptr = reader->ptr;
if( ptr )
{
code = *ptr++;
if( ptr >= reader->block_max )
{
cvChangeSeqBlock( (CvSeqReader *) reader, 1 );
ptr = reader->ptr;
}
reader->ptr = ptr;
reader->code = (schar)code;
assert( (code & ~7) == 0 );
reader->pt.x = pt.x + icvCodeDeltas[code].x;
reader->pt.y = pt.y + icvCodeDeltas[code].y;
}
return pt;
}
/****************************************************************************************\
* Raster->Chain Tree (Suzuki algorithms) *
\****************************************************************************************/
typedef struct _CvContourInfo
{
int flags;
struct _CvContourInfo *next; /* next contour with the same mark value */
struct _CvContourInfo *parent; /* information about parent contour */
CvSeq *contour; /* corresponding contour (may be 0, if rejected) */
CvRect rect; /* bounding rectangle */
CvPoint origin; /* origin point (where the contour was traced from) */
int is_hole; /* hole flag */
}
_CvContourInfo;
/*
Structure that is used for sequential retrieving contours from the image.
It supports both hierarchical and plane variants of Suzuki algorithm.
*/
typedef struct _CvContourScanner
{
CvMemStorage *storage1; /* contains fetched contours */
CvMemStorage *storage2; /* contains approximated contours
(!=storage1 if approx_method2 != approx_method1) */
CvMemStorage *cinfo_storage; /* contains _CvContourInfo nodes */
CvSet *cinfo_set; /* set of _CvContourInfo nodes */
CvMemStoragePos initial_pos; /* starting storage pos */
CvMemStoragePos backup_pos; /* beginning of the latest approx. contour */
CvMemStoragePos backup_pos2; /* ending of the latest approx. contour */
schar *img0; /* image origin */
schar *img; /* current image row */
int img_step; /* image step */
CvSize img_size; /* ROI size */
CvPoint offset; /* ROI offset: coordinates, added to each contour point */
CvPoint pt; /* current scanner position */
CvPoint lnbd; /* position of the last met contour */
int nbd; /* current mark val */
_CvContourInfo *l_cinfo; /* information about latest approx. contour */
_CvContourInfo cinfo_temp; /* temporary var which is used in simple modes */
_CvContourInfo frame_info; /* information about frame */
CvSeq frame; /* frame itself */
int approx_method1; /* approx method when tracing */
int approx_method2; /* final approx method */
int mode; /* contour scanning mode:
0 - external only
1 - all the contours w/o any hierarchy
2 - connected components (i.e. two-level structure -
external contours and holes),
3 - full hierarchy;
4 - connected components of a multi-level image
*/
int subst_flag;
int seq_type1; /* type of fetched contours */
int header_size1; /* hdr size of fetched contours */
int elem_size1; /* elem size of fetched contours */
int seq_type2; /* */
int header_size2; /* the same for approx. contours */
int elem_size2; /* */
_CvContourInfo *cinfo_table[128];
}
_CvContourScanner;
#define _CV_FIND_CONTOURS_FLAGS_EXTERNAL_ONLY 1
#define _CV_FIND_CONTOURS_FLAGS_HIERARCHIC 2
/*
Initializes scanner structure.
Prepare image for scanning ( clear borders and convert all pixels to 0-1.
*/
static CvContourScanner
cvStartFindContours_Impl( void* _img, CvMemStorage* storage,
int header_size, int mode,
int method, CvPoint offset, int needFillBorder )
{
if( !storage )
CV_Error( CV_StsNullPtr, "" );
CvMat stub, *mat = cvGetMat( _img, &stub );
if( CV_MAT_TYPE(mat->type) == CV_32SC1 && mode == CV_RETR_CCOMP )
mode = CV_RETR_FLOODFILL;
if( !((CV_IS_MASK_ARR( mat ) && mode < CV_RETR_FLOODFILL) ||
(CV_MAT_TYPE(mat->type) == CV_32SC1 && mode == CV_RETR_FLOODFILL)) )
CV_Error( CV_StsUnsupportedFormat,
"[Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL "
"otherwise supports CV_32SC1 images only" );
CvSize size = cvSize( mat->width, mat->height );
int step = mat->step;
uchar* img = (uchar*)(mat->data.ptr);
if( method < 0 || method > CV_CHAIN_APPROX_TC89_KCOS )
CV_Error( CV_StsOutOfRange, "" );
if( header_size < (int) (method == CV_CHAIN_CODE ? sizeof( CvChain ) : sizeof( CvContour )))
CV_Error( CV_StsBadSize, "" );
CvContourScanner scanner = (CvContourScanner)cvAlloc( sizeof( *scanner ));
memset( scanner, 0, sizeof(*scanner) );
scanner->storage1 = scanner->storage2 = storage;
scanner->img0 = (schar *) img;
scanner->img = (schar *) (img + step);
scanner->img_step = step;
scanner->img_size.width = size.width - 1; /* exclude rightest column */
scanner->img_size.height = size.height - 1; /* exclude bottomost row */
scanner->mode = mode;
scanner->offset = offset;
scanner->pt.x = scanner->pt.y = 1;
scanner->lnbd.x = 0;
scanner->lnbd.y = 1;
scanner->nbd = 2;
scanner->frame_info.contour = &(scanner->frame);
scanner->frame_info.is_hole = 1;
scanner->frame_info.next = 0;
scanner->frame_info.parent = 0;
scanner->frame_info.rect = cvRect( 0, 0, size.width, size.height );
scanner->l_cinfo = 0;
scanner->subst_flag = 0;
scanner->frame.flags = CV_SEQ_FLAG_HOLE;
scanner->approx_method2 = scanner->approx_method1 = method;
if( method == CV_CHAIN_APPROX_TC89_L1 || method == CV_CHAIN_APPROX_TC89_KCOS )
scanner->approx_method1 = CV_CHAIN_CODE;
if( scanner->approx_method1 == CV_CHAIN_CODE )
{
scanner->seq_type1 = CV_SEQ_CHAIN_CONTOUR;
scanner->header_size1 = scanner->approx_method1 == scanner->approx_method2 ?
header_size : sizeof( CvChain );
scanner->elem_size1 = sizeof( char );
}
else
{
scanner->seq_type1 = CV_SEQ_POLYGON;
scanner->header_size1 = scanner->approx_method1 == scanner->approx_method2 ?
header_size : sizeof( CvContour );
scanner->elem_size1 = sizeof( CvPoint );
}
scanner->header_size2 = header_size;
if( scanner->approx_method2 == CV_CHAIN_CODE )
{
scanner->seq_type2 = scanner->seq_type1;
scanner->elem_size2 = scanner->elem_size1;
}
else
{
scanner->seq_type2 = CV_SEQ_POLYGON;
scanner->elem_size2 = sizeof( CvPoint );
}
scanner->seq_type1 = scanner->approx_method1 == CV_CHAIN_CODE ?
CV_SEQ_CHAIN_CONTOUR : CV_SEQ_POLYGON;
scanner->seq_type2 = scanner->approx_method2 == CV_CHAIN_CODE ?
CV_SEQ_CHAIN_CONTOUR : CV_SEQ_POLYGON;
cvSaveMemStoragePos( storage, &(scanner->initial_pos) );
if( method > CV_CHAIN_APPROX_SIMPLE )
{
scanner->storage1 = cvCreateChildMemStorage( scanner->storage2 );
}
if( mode > CV_RETR_LIST )
{
scanner->cinfo_storage = cvCreateChildMemStorage( scanner->storage2 );
scanner->cinfo_set = cvCreateSet( 0, sizeof( CvSet ), sizeof( _CvContourInfo ),
scanner->cinfo_storage );
}
CV_Assert(step >= 0);
CV_Assert(size.height >= 1);
/* make zero borders */
if(needFillBorder)
{
int esz = CV_ELEM_SIZE(mat->type);
memset( img, 0, size.width*esz );
memset( img + static_cast<size_t>(step) * (size.height - 1), 0, size.width*esz );
img += step;
for( int y = 1; y < size.height - 1; y++, img += step )
{
for( int k = 0; k < esz; k++ )
img[k] = img[(size.width - 1)*esz + k] = (schar)0;
}
}
/* converts all pixels to 0 or 1 */
if( CV_MAT_TYPE(mat->type) != CV_32S )
cvThreshold( mat, mat, 0, 1, CV_THRESH_BINARY );
return scanner;
}
CV_IMPL CvContourScanner
cvStartFindContours( void* _img, CvMemStorage* storage,
int header_size, int mode,
int method, CvPoint offset )
{
return cvStartFindContours_Impl(_img, storage, header_size, mode, method, offset, 1);
}
/*
Final stage of contour processing.
Three variants possible:
1. Contour, which was retrieved using border following, is added to
the contour tree. It is the case when the icvSubstituteContour function
was not called after retrieving the contour.
2. New contour, assigned by icvSubstituteContour function, is added to the
tree. The retrieved contour itself is removed from the storage.
Here two cases are possible:
2a. If one deals with plane variant of algorithm
(hierarchical structure is not reconstructed),
the contour is removed completely.
2b. In hierarchical case, the header of the contour is not removed.
It's marked as "link to contour" and h_next pointer of it is set to
new, substituting contour.
3. The similar to 2, but when NULL pointer was assigned by
icvSubstituteContour function. In this case, the function removes
retrieved contour completely if plane case and
leaves header if hierarchical (but doesn't mark header as "link").
------------------------------------------------------------------------
The 1st variant can be used to retrieve and store all the contours from the image
(with optional conversion from chains to contours using some approximation from
restricted set of methods). Some characteristics of contour can be computed in the
same pass.
The usage scheme can look like:
icvContourScanner scanner;
CvMemStorage* contour_storage;
CvSeq* first_contour;
CvStatus result;
...
icvCreateMemStorage( &contour_storage, block_size/0 );
...
cvStartFindContours
( img, contour_storage,
header_size, approx_method,
[external_only,]
&scanner );
for(;;)
{
[CvSeq* contour;]
result = icvFindNextContour( &scanner, &contour/0 );
if( result != CV_OK ) break;
// calculate some characteristics
...
}
if( result < 0 ) goto error_processing;
cvEndFindContours( &scanner, &first_contour );
...
-----------------------------------------------------------------
Second variant is more complex and can be used when someone wants store not
the retrieved contours but transformed ones. (e.g. approximated with some
non-default algorithm ).
The scheme can be the as following:
icvContourScanner scanner;
CvMemStorage* contour_storage;
CvMemStorage* temp_storage;
CvSeq* first_contour;
CvStatus result;
...
icvCreateMemStorage( &contour_storage, block_size/0 );
icvCreateMemStorage( &temp_storage, block_size/0 );
...
icvStartFindContours8uC1R
( <img_params>, temp_storage,
header_size, approx_method,
[retrival_mode],
&scanner );
for(;;)
{
CvSeq* temp_contour;
CvSeq* new_contour;
result = icvFindNextContour( scanner, &temp_contour );
if( result != CV_OK ) break;
<approximation_function>( temp_contour, contour_storage,
&new_contour, <parameters...> );
icvSubstituteContour( scanner, new_contour );
...
}
if( result < 0 ) goto error_processing;
cvEndFindContours( &scanner, &first_contour );
...
----------------------------------------------------------------------------
Third method to retrieve contours may be applied if contours are irrelevant
themselves but some characteristics of them are used only.
The usage is similar to second except slightly different internal loop
for(;;)
{
CvSeq* temp_contour;
result = icvFindNextContour( &scanner, &temp_contour );
if( result != CV_OK ) break;
// calculate some characteristics of temp_contour
icvSubstituteContour( scanner, 0 );
...
}
new_storage variable is not needed here.
Note, that the second and the third methods can interleave. I.e. it is possible to
retain contours that satisfy with some criteria and reject others.
In hierarchic case the resulting tree is the part of original tree with
some nodes absent. But in the resulting tree the contour1 is a child
(may be indirect) of contour2 iff in the original tree the contour1
is a child (may be indirect) of contour2.
*/
static void
icvEndProcessContour( CvContourScanner scanner )
{
_CvContourInfo *l_cinfo = scanner->l_cinfo;
if( l_cinfo )
{
if( scanner->subst_flag )
{
CvMemStoragePos temp;
cvSaveMemStoragePos( scanner->storage2, &temp );
if( temp.top == scanner->backup_pos2.top &&
temp.free_space == scanner->backup_pos2.free_space )
{
cvRestoreMemStoragePos( scanner->storage2, &scanner->backup_pos );
}
scanner->subst_flag = 0;
}
if( l_cinfo->contour )
{
cvInsertNodeIntoTree( l_cinfo->contour, l_cinfo->parent->contour,
&(scanner->frame) );
}
scanner->l_cinfo = 0;
}
}
/* replaces one contour with another */
CV_IMPL void
cvSubstituteContour( CvContourScanner scanner, CvSeq * new_contour )
{
_CvContourInfo *l_cinfo;
if( !scanner )
CV_Error( CV_StsNullPtr, "" );
l_cinfo = scanner->l_cinfo;
if( l_cinfo && l_cinfo->contour && l_cinfo->contour != new_contour )
{
l_cinfo->contour = new_contour;
scanner->subst_flag = 1;
}
}
static const int MAX_SIZE = 16;
/*
marks domain border with +/-<constant> and stores the contour into CvSeq.
method:
<0 - chain
==0 - direct
>0 - simple approximation
*/
static void
icvFetchContour( schar *ptr,
int step,
CvPoint pt,
CvSeq* contour,
int _method )
{
const schar nbd = 2;
int deltas[MAX_SIZE];
CvSeqWriter writer;
schar *i0 = ptr, *i1, *i3, *i4 = 0;
int prev_s = -1, s, s_end;
int method = _method - 1;
CV_DbgAssert( (unsigned) _method <= CV_CHAIN_APPROX_SIMPLE );
/* initialize local state */
CV_INIT_3X3_DELTAS( deltas, step, 1 );
memcpy( deltas + 8, deltas, 8 * sizeof( deltas[0] ));
/* initialize writer */
cvStartAppendToSeq( contour, &writer );
if( method < 0 )
((CvChain *) contour)->origin = pt;
s_end = s = CV_IS_SEQ_HOLE( contour ) ? 0 : 4;
do
{
s = (s - 1) & 7;
i1 = i0 + deltas[s];
}
while( *i1 == 0 && s != s_end );
if( s == s_end ) /* single pixel domain */
{
*i0 = (schar) (nbd | -128);
if( method >= 0 )
{
CV_WRITE_SEQ_ELEM( pt, writer );
}
}
else
{
i3 = i0;
prev_s = s ^ 4;
/* follow border */
for( ;; )
{
CV_Assert(i3 != NULL);
s_end = s;
s = std::min(s, MAX_SIZE - 1);
while( s < MAX_SIZE - 1 )
{
i4 = i3 + deltas[++s];
CV_Assert(i4 != NULL);
if( *i4 != 0 )
break;
}
s &= 7;
/* check "right" bound */
if( (unsigned) (s - 1) < (unsigned) s_end )
{
*i3 = (schar) (nbd | -128);
}
else if( *i3 == 1 )
{
*i3 = nbd;
}
if( method < 0 )
{
schar _s = (schar) s;
CV_WRITE_SEQ_ELEM( _s, writer );
}
else
{
if( s != prev_s || method == 0 )
{
CV_WRITE_SEQ_ELEM( pt, writer );
prev_s = s;
}
pt.x += icvCodeDeltas[s].x;
pt.y += icvCodeDeltas[s].y;
}
if( i4 == i0 && i3 == i1 )
break;
i3 = i4;
s = (s + 4) & 7;
} /* end of border following loop */
}
cvEndWriteSeq( &writer );
if( _method != CV_CHAIN_CODE )
cvBoundingRect( contour, 1 );
CV_DbgAssert( (writer.seq->total == 0 && writer.seq->first == 0) ||
writer.seq->total > writer.seq->first->count ||
(writer.seq->first->prev == writer.seq->first &&
writer.seq->first->next == writer.seq->first) );
}
/*
trace contour until certain point is met.
returns 1 if met, 0 else.
*/
static int
icvTraceContour( schar *ptr, int step, schar *stop_ptr, int is_hole )
{
int deltas[MAX_SIZE];
schar *i0 = ptr, *i1, *i3, *i4 = NULL;
int s, s_end;
/* initialize local state */
CV_INIT_3X3_DELTAS( deltas, step, 1 );
memcpy( deltas + 8, deltas, 8 * sizeof( deltas[0] ));
CV_DbgAssert( (*i0 & -2) != 0 );
s_end = s = is_hole ? 0 : 4;
do
{
s = (s - 1) & 7;
i1 = i0 + deltas[s];
}
while( *i1 == 0 && s != s_end );
i3 = i0;
/* check single pixel domain */
if( s != s_end )
{
/* follow border */
for( ;; )
{
CV_Assert(i3 != NULL);
s = std::min(s, MAX_SIZE - 1);
while( s < MAX_SIZE - 1 )
{
i4 = i3 + deltas[++s];
CV_Assert(i4 != NULL);
if( *i4 != 0 )
break;
}
if( i3 == stop_ptr || (i4 == i0 && i3 == i1) )
break;
i3 = i4;
s = (s + 4) & 7;
} /* end of border following loop */
}
return i3 == stop_ptr;
}
static void
icvFetchContourEx( schar* ptr,
int step,
CvPoint pt,
CvSeq* contour,
int _method,
int nbd,
CvRect* _rect )
{
int deltas[MAX_SIZE];
CvSeqWriter writer;
schar *i0 = ptr, *i1, *i3, *i4 = NULL;
CvRect rect;
int prev_s = -1, s, s_end;
int method = _method - 1;
CV_DbgAssert( (unsigned) _method <= CV_CHAIN_APPROX_SIMPLE );
CV_DbgAssert( 1 < nbd && nbd < 128 );
/* initialize local state */
CV_INIT_3X3_DELTAS( deltas, step, 1 );
memcpy( deltas + 8, deltas, 8 * sizeof( deltas[0] ));
/* initialize writer */
cvStartAppendToSeq( contour, &writer );
if( method < 0 )
((CvChain *)contour)->origin = pt;
rect.x = rect.width = pt.x;
rect.y = rect.height = pt.y;
s_end = s = CV_IS_SEQ_HOLE( contour ) ? 0 : 4;
do
{
s = (s - 1) & 7;
i1 = i0 + deltas[s];
}
while( *i1 == 0 && s != s_end );
if( s == s_end ) /* single pixel domain */
{
*i0 = (schar) (nbd | 0x80);
if( method >= 0 )
{
CV_WRITE_SEQ_ELEM( pt, writer );
}
}
else
{
i3 = i0;
prev_s = s ^ 4;
/* follow border */
for( ;; )
{
CV_Assert(i3 != NULL);
s_end = s;
s = std::min(s, MAX_SIZE - 1);
while( s < MAX_SIZE - 1 )
{
i4 = i3 + deltas[++s];
CV_Assert(i4 != NULL);
if( *i4 != 0 )
break;
}
s &= 7;
/* check "right" bound */
if( (unsigned) (s - 1) < (unsigned) s_end )
{
*i3 = (schar) (nbd | 0x80);
}
else if( *i3 == 1 )
{
*i3 = (schar) nbd;
}
if( method < 0 )
{
schar _s = (schar) s;
CV_WRITE_SEQ_ELEM( _s, writer );
}
else if( s != prev_s || method == 0 )
{
CV_WRITE_SEQ_ELEM( pt, writer );
}
if( s != prev_s )
{
/* update bounds */
if( pt.x < rect.x )
rect.x = pt.x;
else if( pt.x > rect.width )
rect.width = pt.x;
if( pt.y < rect.y )
rect.y = pt.y;
else if( pt.y > rect.height )
rect.height = pt.y;
}
prev_s = s;
pt.x += icvCodeDeltas[s].x;
pt.y += icvCodeDeltas[s].y;
if( i4 == i0 && i3 == i1 ) break;
i3 = i4;
s = (s + 4) & 7;
} /* end of border following loop */
}
rect.width -= rect.x - 1;
rect.height -= rect.y - 1;
cvEndWriteSeq( &writer );
if( _method != CV_CHAIN_CODE )
((CvContour*)contour)->rect = rect;
CV_DbgAssert( (writer.seq->total == 0 && writer.seq->first == 0) ||
writer.seq->total > writer.seq->first->count ||
(writer.seq->first->prev == writer.seq->first &&
writer.seq->first->next == writer.seq->first) );
if( _rect ) *_rect = rect;
}
static int
icvTraceContour_32s( int *ptr, int step, int *stop_ptr, int is_hole )
{
CV_Assert(ptr != NULL);
int deltas[MAX_SIZE];
int *i0 = ptr, *i1, *i3, *i4 = NULL;
int s, s_end;
const int right_flag = INT_MIN;
const int new_flag = (int)((unsigned)INT_MIN >> 1);
const int value_mask = ~(right_flag | new_flag);
const int ccomp_val = *i0 & value_mask;
/* initialize local state */
CV_INIT_3X3_DELTAS( deltas, step, 1 );
memcpy( deltas + 8, deltas, 8 * sizeof( deltas[0] ));
s_end = s = is_hole ? 0 : 4;
do
{
s = (s - 1) & 7;
i1 = i0 + deltas[s];
}
while( (*i1 & value_mask) != ccomp_val && s != s_end );
i3 = i0;
/* check single pixel domain */
if( s != s_end )
{
/* follow border */
for( ;; )
{
CV_Assert(i3 != NULL);
s_end = s;
s = std::min(s, MAX_SIZE - 1);
while( s < MAX_SIZE - 1 )
{
i4 = i3 + deltas[++s];
CV_Assert(i4 != NULL);
if( (*i4 & value_mask) == ccomp_val )
break;
}
if( i3 == stop_ptr || (i4 == i0 && i3 == i1) )
break;
i3 = i4;
s = (s + 4) & 7;
} /* end of border following loop */
}
return i3 == stop_ptr;
}
static void
icvFetchContourEx_32s( int* ptr,
int step,
CvPoint pt,
CvSeq* contour,
int _method,
CvRect* _rect )
{
CV_Assert(ptr != NULL);
int deltas[MAX_SIZE];
CvSeqWriter writer;
int *i0 = ptr, *i1, *i3, *i4;
CvRect rect;
int prev_s = -1, s, s_end;
int method = _method - 1;
const int right_flag = INT_MIN;
const int new_flag = (int)((unsigned)INT_MIN >> 1);
const int value_mask = ~(right_flag | new_flag);
const int ccomp_val = *i0 & value_mask;
const int nbd0 = ccomp_val | new_flag;
const int nbd1 = nbd0 | right_flag;
CV_DbgAssert( (unsigned) _method <= CV_CHAIN_APPROX_SIMPLE );
/* initialize local state */
CV_INIT_3X3_DELTAS( deltas, step, 1 );
memcpy( deltas + 8, deltas, 8 * sizeof( deltas[0] ));
/* initialize writer */
cvStartAppendToSeq( contour, &writer );
if( method < 0 )
((CvChain *)contour)->origin = pt;
rect.x = rect.width = pt.x;
rect.y = rect.height = pt.y;
s_end = s = CV_IS_SEQ_HOLE( contour ) ? 0 : 4;
do
{
s = (s - 1) & 7;
i1 = i0 + deltas[s];
}
while( (*i1 & value_mask) != ccomp_val && s != s_end && ( s < MAX_SIZE - 1 ) );
if( s == s_end ) /* single pixel domain */
{
*i0 = nbd1;
if( method >= 0 )
{
CV_WRITE_SEQ_ELEM( pt, writer );
}
}
else
{
i3 = i0;
prev_s = s ^ 4;
/* follow border */
for( ;; )
{
CV_Assert(i3 != NULL);
s_end = s;
do
{
i4 = i3 + deltas[++s];
CV_Assert(i4 != NULL);
}
while( (*i4 & value_mask) != ccomp_val && ( s < MAX_SIZE - 1 ) );
s &= 7;
/* check "right" bound */
if( (unsigned) (s - 1) < (unsigned) s_end )
{
*i3 = nbd1;
}
else if( *i3 == ccomp_val )
{
*i3 = nbd0;
}
if( method < 0 )
{
schar _s = (schar) s;
CV_WRITE_SEQ_ELEM( _s, writer );
}
else if( s != prev_s || method == 0 )
{
CV_WRITE_SEQ_ELEM( pt, writer );
}
if( s != prev_s )
{
/* update bounds */
if( pt.x < rect.x )
rect.x = pt.x;
else if( pt.x > rect.width )
rect.width = pt.x;
if( pt.y < rect.y )
rect.y = pt.y;
else if( pt.y > rect.height )
rect.height = pt.y;
}
prev_s = s;
pt.x += icvCodeDeltas[s].x;
pt.y += icvCodeDeltas[s].y;
if( i4 == i0 && i3 == i1 ) break;
i3 = i4;
s = (s + 4) & 7;
} /* end of border following loop */
}
rect.width -= rect.x - 1;
rect.height -= rect.y - 1;
cvEndWriteSeq( &writer );
if( _method != CV_CHAIN_CODE )
((CvContour*)contour)->rect = rect;
CV_DbgAssert( (writer.seq->total == 0 && writer.seq->first == 0) ||
writer.seq->total > writer.seq->first->count ||