1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
44 #ifdef WIN32 /* make sure it builds under Linux whenever it is included into Makefile.am or not. */
46 //void icvCutContour( CvSeq* current, IplImage* image );
47 CvSeq* icvCutContourRaster( CvSeq* current, CvMemStorage* storage, IplImage* image );
50 //create lists of segments of all contours from image
51 CvSeq* cvExtractSingleEdges( IplImage* image, //bw image - it's content will be destroyed by cvFindContours
52 CvMemStorage* storage )
54 CvMemStorage* tmp_storage = cvCreateChildMemStorage( storage );
56 cvFindContours( image, tmp_storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE );
59 //iterate through contours
60 //iterate through tree
61 CvSeq* current = contours;
68 //actually this loop can iterates through tree,
69 //but still we use CV_RETR_LIST it is not useful
74 //get vertical list of segments for one contour
75 CvSeq* new_seq = icvCutContourRaster( current, storage, image );
77 //add this vertical list to horisontal list
82 tail_seq->h_next = new_seq;
83 new_seq->h_prev = tail_seq;
88 output = tail_seq = new_seq;
92 //iteration through tree
96 current = current->v_next;
102 while( !current->h_next )
104 current = current->v_prev;
109 if( current ) //go brother
110 current = current->h_next;
114 //free temporary memstorage with initial contours
115 cvReleaseMemStorage( &tmp_storage );
120 //makes vertical list of segments for 1 contour
121 CvSeq* icvCutContourRaster( CvSeq* current, CvMemStorage* storage, IplImage* image /*tmp image*/)
123 //iplSet(image, 0 ); // this can cause double edges if two contours have common edge
124 // for example if object is circle with 1 pixel width
125 // to remove such problem - remove this iplSet
127 //approx contour by single edges
132 cvStartReadSeq( current, &reader, 0 );
133 //below line just to avoid warning
134 cvStartWriteSeq( current->flags, sizeof(CvContour), sizeof(CvPoint), storage, &writer );
139 //first pass through contour - compute number of branches at every point
141 for( i = 0; i < current->total; i++ )
145 CV_READ_SEQ_ELEM( cur, reader );
148 ((uchar*)image->imageData)[image->widthStep * cur.y + cur.x]++;
149 assert( ((uchar*)image->imageData)[image->widthStep * cur.y + cur.x] != 255 );
153 //second pass - create separate edges
154 for( i = 0; i < current->total; i++ )
158 CV_READ_SEQ_ELEM( cur, reader );
160 //get pixel at this point
161 uchar flag = image->imageData[image->widthStep * cur.y + cur.x];
162 if( flag != 255 && flag < 3) //
166 cvStartWriteSeq( current->flags, sizeof(CvContour), sizeof(CvPoint), storage, &writer );
171 if( flag < 3 ) ((uchar*)image->imageData)[image->widthStep * cur.y + cur.x] = 255;
172 //add it to another seq
173 CV_WRITE_SEQ_ELEM( cur, writer );
178 //exclude this point from contour
181 CvSeq* newseq = cvEndWriteSeq( &writer );
186 tail->v_next = newseq;
187 newseq->v_prev = tail;
192 output = tail = newseq;
199 if( writing ) //if were not self intersections
201 CvSeq* newseq = cvEndWriteSeq( &writer );
206 tail->v_next = newseq;
207 newseq->v_prev = tail;
212 output = tail = newseq;
222 /*void icvCutContour( CvSeq* current, IplImage* image )
224 //approx contour by single edges
226 CvSeqReader rev_reader;
228 cvStartReadSeq( current, &reader, 0 );
230 int64* cur_pt = (int64*)reader.ptr;
231 int64* prev_pt = (int64*)reader.prev_elem;
233 //search for point a in aba position
234 for( int i = 0; i < current->total; i++ )
236 CV_NEXT_SEQ_ELEM( sizeof(int64), reader );
238 //compare current reader pos element with old previous
239 if( prev_pt[0] == ((int64*)reader.ptr)[0] )
242 CV_PREV_SEQ_ELEM( sizeof(int64), reader );
245 //this point is end of edge
246 //start going both directions and collect edge
247 cvStartReadSeq( current, &rev_reader, 1 );
249 int pos = cvGetSeqReaderPos( &reader );
250 cvSetSeqReaderPos( &rev_reader, pos );
252 //walk in both directions
257 int64* cur_pt = (int64*)reader.ptr;
258 int64* prev_pt = (int64*)reader.prev_elem;