forked from zk00006/OpenTLD
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathBlobContour.cpp
More file actions
258 lines (210 loc) · 5.47 KB
/
Copy pathBlobContour.cpp
File metadata and controls
258 lines (210 loc) · 5.47 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
#include "BlobContour.h"
#include "cxcore.h"
CBlobContour::CBlobContour()
{
m_startPoint.x = 0;
m_startPoint.y = 0;
m_area = -1;
m_perimeter = -1;
m_contourPoints = NULL;
m_moments.m00 = -1;
m_contour = NULL;
m_parentStorage = NULL;
}
CBlobContour::CBlobContour(CvPoint startPoint, CvMemStorage *storage )
{
m_startPoint.x = startPoint.x;
m_startPoint.y = startPoint.y;
m_area = -1;
m_perimeter = -1;
m_moments.m00 = -1;
m_parentStorage = storage;
m_contourPoints = NULL;
// contour sequence: must be compatible with opencv functions
m_contour = cvCreateSeq( CV_SEQ_ELTYPE_CODE | CV_SEQ_KIND_CURVE | CV_SEQ_FLAG_CLOSED,
sizeof(CvContour),
sizeof(t_chainCode),m_parentStorage);
}
//! Copy constructor
CBlobContour::CBlobContour( CBlobContour *source )
{
if (source != NULL )
{
*this = *source;
}
}
CBlobContour::~CBlobContour()
{
// let parent blob deallocate all contour and contour point memory
m_contour = NULL;
m_contourPoints = NULL;
}
//! Copy operator
CBlobContour& CBlobContour::operator=( const CBlobContour &source )
{
if( this != &source )
{
m_startPoint = source.m_startPoint;
m_parentStorage = source.m_parentStorage;
if (m_contour)
{
cvClearSeq( m_contour );
}
if (source.m_contour)
{
m_contour = cvCloneSeq( source.m_contour, m_parentStorage);
}
if( source.m_contourPoints )
{
if( m_contourPoints )
cvClearSeq( m_contourPoints );
m_contourPoints = cvCloneSeq( source.m_contourPoints, m_parentStorage);
}
m_area = source.m_area;
m_perimeter = source.m_area;
m_moments = source.m_moments;
}
return *this;
}
/**
- FUNCIÓ: AddChainCode
- FUNCIONALITAT: Add chain code to contour
- PARÀMETRES:
-
- RESULTAT:
-
- RESTRICCIONS:
-
- AUTOR: rborras
- DATA DE CREACIÓ: 2008/05/06
- MODIFICACIÓ: Data. Autor. Descripció.
*/
void CBlobContour::AddChainCode(t_chainCode chaincode)
{
cvSeqPush(m_contour, &chaincode);
}
//! Clears chain code contour and points
void CBlobContour::ResetChainCode()
{
if( m_contour )
{
cvClearSeq( m_contour );
m_contour = NULL;
}
if( m_contourPoints )
{
cvClearSeq( m_contourPoints );
m_contourPoints = NULL;
}
}
/**
- FUNCIÓ: GetPerimeter
- FUNCIONALITAT: Get perimeter from chain code. Diagonals sum sqrt(2) and horizontal and vertical codes 1
- PARÀMETRES:
-
- RESULTAT:
-
- RESTRICCIONS:
-
- AUTOR: rborras
- DATA DE CREACIÓ: 2008/04/30
- MODIFICACIÓ: Data. Autor. Descripció.
- NOTA: Algorithm derived from "Methods to estimate area and perimeters of blob-like objects: A comparison", L.Yang
*/
double CBlobContour::GetPerimeter()
{
// is calculated?
if (m_perimeter != -1)
{
return m_perimeter;
}
if( IsEmpty() )
return 0;
m_perimeter = cvContourPerimeter( GetContourPoints() );
return m_perimeter;
}
/**
- FUNCIÓ: GetArea
- FUNCIONALITAT: Computes area from chain code
- PARÀMETRES:
-
- RESULTAT:
- May give negative areas for clock wise contours
- RESTRICCIONS:
-
- AUTOR: rborras
- DATA DE CREACIÓ: 2008/04/30
- MODIFICACIÓ: Data. Autor. Descripció.
- NOTA: Algorithm derived from "Properties of contour codes", G.R. Wilson
*/
double CBlobContour::GetArea()
{
// is calculated?
if (m_area != -1)
{
return m_area;
}
if( IsEmpty() )
return 0;
m_area = fabs( cvContourArea( GetContourPoints() ));
return m_area;
}
//! Get contour moment (p,q up to MAX_CALCULATED_MOMENTS)
double CBlobContour::GetMoment(int p, int q)
{
// is a valid moment?
if ( p < 0 || q < 0 || p > MAX_MOMENTS_ORDER || q > MAX_MOMENTS_ORDER )
{
return -1;
}
if( IsEmpty() )
return 0;
// it is calculated?
if( m_moments.m00 == -1)
{
cvMoments( GetContourPoints(), &m_moments );
}
return cvGetSpatialMoment( &m_moments, p, q );
}
//! Calculate contour points from crack codes
t_PointList CBlobContour::GetContourPoints()
{
// it is calculated?
if( m_contourPoints != NULL )
return m_contourPoints;
if ( m_contour == NULL || m_contour->total <= 0 )
{
return NULL;
}
CvSeq *tmpPoints;
CvSeqReader reader;
CvSeqWriter writer;
CvPoint actualPoint;
CvRect boundingBox;
// if aproximation is different than simple extern perimeter will not work
tmpPoints = cvApproxChains( m_contour, m_parentStorage, CV_CHAIN_APPROX_NONE);
// apply an offset to contour points to recover real coordinates
cvStartReadSeq( tmpPoints, &reader);
m_contourPoints = cvCreateSeq( tmpPoints->flags, tmpPoints->header_size, tmpPoints->elem_size, m_parentStorage );
cvStartAppendToSeq(m_contourPoints, &writer );
// also calculate bounding box of the contour to allow cvPointPolygonTest
// work correctly on the generated polygon
boundingBox.x = boundingBox.y = 10000;
boundingBox.width = boundingBox.height = 0;
for( int i=0; i< tmpPoints->total; i++)
{
CV_READ_SEQ_ELEM( actualPoint, reader);
actualPoint.x += m_startPoint.x;
actualPoint.y += m_startPoint.y;
boundingBox.x = MIN( boundingBox.x, actualPoint.x );
boundingBox.y = MIN( boundingBox.y, actualPoint.y );
boundingBox.width = MAX( boundingBox.width, actualPoint.x );
boundingBox.height = MAX( boundingBox.height, actualPoint.y );
CV_WRITE_SEQ_ELEM( actualPoint, writer );
}
cvEndWriteSeq( &writer );
cvClearSeq( tmpPoints );
// assign calculated bounding box
((CvContour*)m_contourPoints)->rect = boundingBox;
return m_contourPoints;
}