Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/src/convhull.cpp
16354 views
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
#include <iostream>
44
45
namespace cv
46
{
47
48
template<typename _Tp>
49
static int Sklansky_( Point_<_Tp>** array, int start, int end, int* stack, int nsign, int sign2 )
50
{
51
int incr = end > start ? 1 : -1;
52
// prepare first triangle
53
int pprev = start, pcur = pprev + incr, pnext = pcur + incr;
54
int stacksize = 3;
55
56
if( start == end ||
57
(array[start]->x == array[end]->x &&
58
array[start]->y == array[end]->y) )
59
{
60
stack[0] = start;
61
return 1;
62
}
63
64
stack[0] = pprev;
65
stack[1] = pcur;
66
stack[2] = pnext;
67
68
end += incr; // make end = afterend
69
70
while( pnext != end )
71
{
72
// check the angle p1,p2,p3
73
_Tp cury = array[pcur]->y;
74
_Tp nexty = array[pnext]->y;
75
_Tp by = nexty - cury;
76
77
if( CV_SIGN( by ) != nsign )
78
{
79
_Tp ax = array[pcur]->x - array[pprev]->x;
80
_Tp bx = array[pnext]->x - array[pcur]->x;
81
_Tp ay = cury - array[pprev]->y;
82
_Tp convexity = ay*bx - ax*by; // if >0 then convex angle
83
84
if( CV_SIGN( convexity ) == sign2 && (ax != 0 || ay != 0) )
85
{
86
pprev = pcur;
87
pcur = pnext;
88
pnext += incr;
89
stack[stacksize] = pnext;
90
stacksize++;
91
}
92
else
93
{
94
if( pprev == start )
95
{
96
pcur = pnext;
97
stack[1] = pcur;
98
pnext += incr;
99
stack[2] = pnext;
100
}
101
else
102
{
103
stack[stacksize-2] = pnext;
104
pcur = pprev;
105
pprev = stack[stacksize-4];
106
stacksize--;
107
}
108
}
109
}
110
else
111
{
112
pnext += incr;
113
stack[stacksize-1] = pnext;
114
}
115
}
116
117
return --stacksize;
118
}
119
120
121
template<typename _Tp>
122
struct CHullCmpPoints
123
{
124
bool operator()(const Point_<_Tp>* p1, const Point_<_Tp>* p2) const
125
{ return p1->x < p2->x || (p1->x == p2->x && p1->y < p2->y); }
126
};
127
128
129
void convexHull( InputArray _points, OutputArray _hull, bool clockwise, bool returnPoints )
130
{
131
CV_INSTRUMENT_REGION();
132
133
CV_Assert(_points.getObj() != _hull.getObj());
134
Mat points = _points.getMat();
135
int i, total = points.checkVector(2), depth = points.depth(), nout = 0;
136
int miny_ind = 0, maxy_ind = 0;
137
CV_Assert(total >= 0 && (depth == CV_32F || depth == CV_32S));
138
139
if( total == 0 )
140
{
141
_hull.release();
142
return;
143
}
144
145
returnPoints = !_hull.fixedType() ? returnPoints : _hull.type() != CV_32S;
146
147
bool is_float = depth == CV_32F;
148
AutoBuffer<Point*> _pointer(total);
149
AutoBuffer<int> _stack(total + 2), _hullbuf(total);
150
Point** pointer = _pointer.data();
151
Point2f** pointerf = (Point2f**)pointer;
152
Point* data0 = points.ptr<Point>();
153
int* stack = _stack.data();
154
int* hullbuf = _hullbuf.data();
155
156
CV_Assert(points.isContinuous());
157
158
for( i = 0; i < total; i++ )
159
pointer[i] = &data0[i];
160
161
// sort the point set by x-coordinate, find min and max y
162
if( !is_float )
163
{
164
std::sort(pointer, pointer + total, CHullCmpPoints<int>());
165
for( i = 1; i < total; i++ )
166
{
167
int y = pointer[i]->y;
168
if( pointer[miny_ind]->y > y )
169
miny_ind = i;
170
if( pointer[maxy_ind]->y < y )
171
maxy_ind = i;
172
}
173
}
174
else
175
{
176
std::sort(pointerf, pointerf + total, CHullCmpPoints<float>());
177
for( i = 1; i < total; i++ )
178
{
179
float y = pointerf[i]->y;
180
if( pointerf[miny_ind]->y > y )
181
miny_ind = i;
182
if( pointerf[maxy_ind]->y < y )
183
maxy_ind = i;
184
}
185
}
186
187
if( pointer[0]->x == pointer[total-1]->x &&
188
pointer[0]->y == pointer[total-1]->y )
189
{
190
hullbuf[nout++] = 0;
191
}
192
else
193
{
194
// upper half
195
int *tl_stack = stack;
196
int tl_count = !is_float ?
197
Sklansky_( pointer, 0, maxy_ind, tl_stack, -1, 1) :
198
Sklansky_( pointerf, 0, maxy_ind, tl_stack, -1, 1);
199
int *tr_stack = stack + tl_count;
200
int tr_count = !is_float ?
201
Sklansky_( pointer, total-1, maxy_ind, tr_stack, -1, -1) :
202
Sklansky_( pointerf, total-1, maxy_ind, tr_stack, -1, -1);
203
204
// gather upper part of convex hull to output
205
if( !clockwise )
206
{
207
std::swap( tl_stack, tr_stack );
208
std::swap( tl_count, tr_count );
209
}
210
211
for( i = 0; i < tl_count-1; i++ )
212
hullbuf[nout++] = int(pointer[tl_stack[i]] - data0);
213
for( i = tr_count - 1; i > 0; i-- )
214
hullbuf[nout++] = int(pointer[tr_stack[i]] - data0);
215
int stop_idx = tr_count > 2 ? tr_stack[1] : tl_count > 2 ? tl_stack[tl_count - 2] : -1;
216
217
// lower half
218
int *bl_stack = stack;
219
int bl_count = !is_float ?
220
Sklansky_( pointer, 0, miny_ind, bl_stack, 1, -1) :
221
Sklansky_( pointerf, 0, miny_ind, bl_stack, 1, -1);
222
int *br_stack = stack + bl_count;
223
int br_count = !is_float ?
224
Sklansky_( pointer, total-1, miny_ind, br_stack, 1, 1) :
225
Sklansky_( pointerf, total-1, miny_ind, br_stack, 1, 1);
226
227
if( clockwise )
228
{
229
std::swap( bl_stack, br_stack );
230
std::swap( bl_count, br_count );
231
}
232
233
if( stop_idx >= 0 )
234
{
235
int check_idx = bl_count > 2 ? bl_stack[1] :
236
bl_count + br_count > 2 ? br_stack[2-bl_count] : -1;
237
if( check_idx == stop_idx || (check_idx >= 0 &&
238
pointer[check_idx]->x == pointer[stop_idx]->x &&
239
pointer[check_idx]->y == pointer[stop_idx]->y) )
240
{
241
// if all the points lie on the same line, then
242
// the bottom part of the convex hull is the mirrored top part
243
// (except the exteme points).
244
bl_count = MIN( bl_count, 2 );
245
br_count = MIN( br_count, 2 );
246
}
247
}
248
249
for( i = 0; i < bl_count-1; i++ )
250
hullbuf[nout++] = int(pointer[bl_stack[i]] - data0);
251
for( i = br_count-1; i > 0; i-- )
252
hullbuf[nout++] = int(pointer[br_stack[i]] - data0);
253
}
254
255
if( !returnPoints )
256
Mat(nout, 1, CV_32S, hullbuf).copyTo(_hull);
257
else
258
{
259
_hull.create(nout, 1, CV_MAKETYPE(depth, 2));
260
Mat hull = _hull.getMat();
261
size_t step = !hull.isContinuous() ? hull.step[0] : sizeof(Point);
262
for( i = 0; i < nout; i++ )
263
*(Point*)(hull.ptr() + i*step) = data0[hullbuf[i]];
264
}
265
}
266
267
268
void convexityDefects( InputArray _points, InputArray _hull, OutputArray _defects )
269
{
270
CV_INSTRUMENT_REGION();
271
272
Mat points = _points.getMat();
273
int i, j = 0, npoints = points.checkVector(2, CV_32S);
274
CV_Assert( npoints >= 0 );
275
276
if( npoints <= 3 )
277
{
278
_defects.release();
279
return;
280
}
281
282
Mat hull = _hull.getMat();
283
int hpoints = hull.checkVector(1, CV_32S);
284
CV_Assert( hpoints > 0 );
285
286
const Point* ptr = points.ptr<Point>();
287
const int* hptr = hull.ptr<int>();
288
std::vector<Vec4i> defects;
289
if ( hpoints < 3 ) //if hull consists of one or two points, contour is always convex
290
{
291
_defects.release();
292
return;
293
}
294
295
// 1. recognize co-orientation of the contour and its hull
296
bool rev_orientation = ((hptr[1] > hptr[0]) + (hptr[2] > hptr[1]) + (hptr[0] > hptr[2])) != 2;
297
298
// 2. cycle through points and hull, compute defects
299
int hcurr = hptr[rev_orientation ? 0 : hpoints-1];
300
CV_Assert( 0 <= hcurr && hcurr < npoints );
301
302
for( i = 0; i < hpoints; i++ )
303
{
304
int hnext = hptr[rev_orientation ? hpoints - i - 1 : i];
305
CV_Assert( 0 <= hnext && hnext < npoints );
306
307
Point pt0 = ptr[hcurr], pt1 = ptr[hnext];
308
double dx0 = pt1.x - pt0.x;
309
double dy0 = pt1.y - pt0.y;
310
double scale = dx0 == 0 && dy0 == 0 ? 0. : 1./std::sqrt(dx0*dx0 + dy0*dy0);
311
312
int defect_deepest_point = -1;
313
double defect_depth = 0;
314
bool is_defect = false;
315
j=hcurr;
316
for(;;)
317
{
318
// go through points to achieve next hull point
319
j++;
320
j &= j >= npoints ? 0 : -1;
321
if( j == hnext )
322
break;
323
324
// compute distance from current point to hull edge
325
double dx = ptr[j].x - pt0.x;
326
double dy = ptr[j].y - pt0.y;
327
double dist = fabs(-dy0*dx + dx0*dy) * scale;
328
329
if( dist > defect_depth )
330
{
331
defect_depth = dist;
332
defect_deepest_point = j;
333
is_defect = true;
334
}
335
}
336
337
if( is_defect )
338
{
339
int idepth = cvRound(defect_depth*256);
340
defects.push_back(Vec4i(hcurr, hnext, defect_deepest_point, idepth));
341
}
342
343
hcurr = hnext;
344
}
345
346
Mat(defects).copyTo(_defects);
347
}
348
349
350
template<typename _Tp>
351
static bool isContourConvex_( const Point_<_Tp>* p, int n )
352
{
353
Point_<_Tp> prev_pt = p[(n-2+n) % n];
354
Point_<_Tp> cur_pt = p[n-1];
355
356
_Tp dx0 = cur_pt.x - prev_pt.x;
357
_Tp dy0 = cur_pt.y - prev_pt.y;
358
int orientation = 0;
359
360
for( int i = 0; i < n; i++ )
361
{
362
_Tp dxdy0, dydx0;
363
_Tp dx, dy;
364
365
prev_pt = cur_pt;
366
cur_pt = p[i];
367
368
dx = cur_pt.x - prev_pt.x;
369
dy = cur_pt.y - prev_pt.y;
370
dxdy0 = dx * dy0;
371
dydx0 = dy * dx0;
372
373
// find orientation
374
// orient = -dy0 * dx + dx0 * dy;
375
// orientation |= (orient > 0) ? 1 : 2;
376
orientation |= (dydx0 > dxdy0) ? 1 : ((dydx0 < dxdy0) ? 2 : 3);
377
if( orientation == 3 )
378
return false;
379
380
dx0 = dx;
381
dy0 = dy;
382
}
383
384
return true;
385
}
386
387
388
bool isContourConvex( InputArray _contour )
389
{
390
Mat contour = _contour.getMat();
391
int total = contour.checkVector(2), depth = contour.depth();
392
CV_Assert(total >= 0 && (depth == CV_32F || depth == CV_32S));
393
394
if( total == 0 )
395
return false;
396
397
return depth == CV_32S ?
398
isContourConvex_(contour.ptr<Point>(), total ) :
399
isContourConvex_(contour.ptr<Point2f>(), total );
400
}
401
402
}
403
404
CV_IMPL CvSeq*
405
cvConvexHull2( const CvArr* array, void* hull_storage,
406
int orientation, int return_points )
407
{
408
CvMat* mat = 0;
409
CvContour contour_header;
410
CvSeq hull_header;
411
CvSeqBlock block, hullblock;
412
CvSeq* ptseq = 0;
413
CvSeq* hullseq = 0;
414
415
if( CV_IS_SEQ( array ))
416
{
417
ptseq = (CvSeq*)array;
418
if( !CV_IS_SEQ_POINT_SET( ptseq ))
419
CV_Error( CV_StsBadArg, "Unsupported sequence type" );
420
if( hull_storage == 0 )
421
hull_storage = ptseq->storage;
422
}
423
else
424
{
425
ptseq = cvPointSeqFromMat( CV_SEQ_KIND_GENERIC, array, &contour_header, &block );
426
}
427
428
bool isStorage = isStorageOrMat(hull_storage);
429
430
if(isStorage)
431
{
432
if( return_points )
433
{
434
hullseq = cvCreateSeq(CV_SEQ_KIND_CURVE|CV_SEQ_ELTYPE(ptseq)|
435
CV_SEQ_FLAG_CLOSED|CV_SEQ_FLAG_CONVEX,
436
sizeof(CvContour), sizeof(CvPoint),(CvMemStorage*)hull_storage );
437
}
438
else
439
{
440
hullseq = cvCreateSeq(
441
CV_SEQ_KIND_CURVE|CV_SEQ_ELTYPE_PPOINT|
442
CV_SEQ_FLAG_CLOSED|CV_SEQ_FLAG_CONVEX,
443
sizeof(CvContour), sizeof(CvPoint*), (CvMemStorage*)hull_storage );
444
}
445
}
446
else
447
{
448
mat = (CvMat*)hull_storage;
449
450
if( (mat->cols != 1 && mat->rows != 1) || !CV_IS_MAT_CONT(mat->type))
451
CV_Error( CV_StsBadArg,
452
"The hull matrix should be continuous and have a single row or a single column" );
453
454
if( mat->cols + mat->rows - 1 < ptseq->total )
455
CV_Error( CV_StsBadSize, "The hull matrix size might be not enough to fit the hull" );
456
457
if( CV_MAT_TYPE(mat->type) != CV_SEQ_ELTYPE(ptseq) &&
458
CV_MAT_TYPE(mat->type) != CV_32SC1 )
459
CV_Error( CV_StsUnsupportedFormat,
460
"The hull matrix must have the same type as input or 32sC1 (integers)" );
461
462
hullseq = cvMakeSeqHeaderForArray(
463
CV_SEQ_KIND_CURVE|CV_MAT_TYPE(mat->type)|CV_SEQ_FLAG_CLOSED,
464
sizeof(hull_header), CV_ELEM_SIZE(mat->type), mat->data.ptr,
465
mat->cols + mat->rows - 1, &hull_header, &hullblock );
466
cvClearSeq( hullseq );
467
}
468
469
int hulltype = CV_SEQ_ELTYPE(hullseq);
470
int total = ptseq->total;
471
if( total == 0 )
472
{
473
if( !isStorage )
474
CV_Error( CV_StsBadSize,
475
"Point sequence can not be empty if the output is matrix" );
476
return 0;
477
}
478
479
cv::AutoBuffer<double> _ptbuf;
480
cv::Mat h0;
481
cv::convexHull(cv::cvarrToMat(ptseq, false, false, 0, &_ptbuf), h0,
482
orientation == CV_CLOCKWISE, CV_MAT_CN(hulltype) == 2);
483
484
485
if( hulltype == CV_SEQ_ELTYPE_PPOINT )
486
{
487
const int* idx = h0.ptr<int>();
488
int ctotal = (int)h0.total();
489
for( int i = 0; i < ctotal; i++ )
490
{
491
void* ptr = cvGetSeqElem(ptseq, idx[i]);
492
cvSeqPush( hullseq, &ptr );
493
}
494
}
495
else
496
cvSeqPushMulti(hullseq, h0.ptr(), (int)h0.total());
497
498
if (isStorage)
499
{
500
return hullseq;
501
}
502
else
503
{
504
if( mat->rows > mat->cols )
505
mat->rows = hullseq->total;
506
else
507
mat->cols = hullseq->total;
508
return 0;
509
}
510
}
511
512
513
/* contour must be a simple polygon */
514
/* it must have more than 3 points */
515
CV_IMPL CvSeq* cvConvexityDefects( const CvArr* array,
516
const CvArr* hullarray,
517
CvMemStorage* storage )
518
{
519
CvSeq* defects = 0;
520
521
int i, index;
522
CvPoint* hull_cur;
523
524
/* is orientation of hull different from contour one */
525
int rev_orientation;
526
527
CvContour contour_header;
528
CvSeq hull_header;
529
CvSeqBlock block, hullblock;
530
CvSeq *ptseq = (CvSeq*)array, *hull = (CvSeq*)hullarray;
531
532
CvSeqReader hull_reader;
533
CvSeqReader ptseq_reader;
534
CvSeqWriter writer;
535
int is_index;
536
537
if( CV_IS_SEQ( ptseq ))
538
{
539
if( !CV_IS_SEQ_POINT_SET( ptseq ))
540
CV_Error( CV_StsUnsupportedFormat,
541
"Input sequence is not a sequence of points" );
542
if( !storage )
543
storage = ptseq->storage;
544
}
545
else
546
{
547
ptseq = cvPointSeqFromMat( CV_SEQ_KIND_GENERIC, array, &contour_header, &block );
548
}
549
550
if( CV_SEQ_ELTYPE( ptseq ) != CV_32SC2 )
551
CV_Error( CV_StsUnsupportedFormat, "Floating-point coordinates are not supported here" );
552
553
if( CV_IS_SEQ( hull ))
554
{
555
int hulltype = CV_SEQ_ELTYPE( hull );
556
if( hulltype != CV_SEQ_ELTYPE_PPOINT && hulltype != CV_SEQ_ELTYPE_INDEX )
557
CV_Error( CV_StsUnsupportedFormat,
558
"Convex hull must represented as a sequence "
559
"of indices or sequence of pointers" );
560
if( !storage )
561
storage = hull->storage;
562
}
563
else
564
{
565
CvMat* mat = (CvMat*)hull;
566
567
if( !CV_IS_MAT( hull ))
568
CV_Error(CV_StsBadArg, "Convex hull is neither sequence nor matrix");
569
570
if( (mat->cols != 1 && mat->rows != 1) ||
571
!CV_IS_MAT_CONT(mat->type) || CV_MAT_TYPE(mat->type) != CV_32SC1 )
572
CV_Error( CV_StsBadArg,
573
"The matrix should be 1-dimensional and continuous array of int's" );
574
575
if( mat->cols + mat->rows - 1 > ptseq->total )
576
CV_Error( CV_StsBadSize, "Convex hull is larger than the point sequence" );
577
578
hull = cvMakeSeqHeaderForArray(
579
CV_SEQ_KIND_CURVE|CV_MAT_TYPE(mat->type)|CV_SEQ_FLAG_CLOSED,
580
sizeof(hull_header), CV_ELEM_SIZE(mat->type), mat->data.ptr,
581
mat->cols + mat->rows - 1, &hull_header, &hullblock );
582
}
583
584
is_index = CV_SEQ_ELTYPE(hull) == CV_SEQ_ELTYPE_INDEX;
585
586
if( !storage )
587
CV_Error( CV_StsNullPtr, "NULL storage pointer" );
588
589
defects = cvCreateSeq( CV_SEQ_KIND_GENERIC, sizeof(CvSeq), sizeof(CvConvexityDefect), storage );
590
591
if( ptseq->total < 4 || hull->total < 3)
592
{
593
//CV_ERROR( CV_StsBadSize,
594
// "point seq size must be >= 4, convex hull size must be >= 3" );
595
return defects;
596
}
597
598
/* recognize co-orientation of ptseq and its hull */
599
{
600
int sign = 0;
601
int index1, index2, index3;
602
603
if( !is_index )
604
{
605
CvPoint* pos = *CV_SEQ_ELEM( hull, CvPoint*, 0 );
606
index1 = cvSeqElemIdx( ptseq, pos );
607
608
pos = *CV_SEQ_ELEM( hull, CvPoint*, 1 );
609
index2 = cvSeqElemIdx( ptseq, pos );
610
611
pos = *CV_SEQ_ELEM( hull, CvPoint*, 2 );
612
index3 = cvSeqElemIdx( ptseq, pos );
613
}
614
else
615
{
616
index1 = *CV_SEQ_ELEM( hull, int, 0 );
617
index2 = *CV_SEQ_ELEM( hull, int, 1 );
618
index3 = *CV_SEQ_ELEM( hull, int, 2 );
619
}
620
621
sign += (index2 > index1) ? 1 : 0;
622
sign += (index3 > index2) ? 1 : 0;
623
sign += (index1 > index3) ? 1 : 0;
624
625
rev_orientation = (sign == 2) ? 0 : 1;
626
}
627
628
cvStartReadSeq( ptseq, &ptseq_reader, 0 );
629
cvStartReadSeq( hull, &hull_reader, rev_orientation );
630
631
if( !is_index )
632
{
633
hull_cur = *(CvPoint**)hull_reader.prev_elem;
634
index = cvSeqElemIdx( ptseq, (char*)hull_cur, 0 );
635
}
636
else
637
{
638
index = *(int*)hull_reader.prev_elem;
639
hull_cur = CV_GET_SEQ_ELEM( CvPoint, ptseq, index );
640
}
641
cvSetSeqReaderPos( &ptseq_reader, index );
642
cvStartAppendToSeq( defects, &writer );
643
644
/* cycle through ptseq and hull with computing defects */
645
for( i = 0; i < hull->total; i++ )
646
{
647
CvConvexityDefect defect;
648
int is_defect = 0;
649
double dx0, dy0;
650
double depth = 0, scale;
651
CvPoint* hull_next;
652
653
if( !is_index )
654
hull_next = *(CvPoint**)hull_reader.ptr;
655
else
656
{
657
int t = *(int*)hull_reader.ptr;
658
hull_next = CV_GET_SEQ_ELEM( CvPoint, ptseq, t );
659
}
660
CV_Assert(hull_next != NULL && hull_cur != NULL);
661
662
dx0 = (double)hull_next->x - (double)hull_cur->x;
663
dy0 = (double)hull_next->y - (double)hull_cur->y;
664
assert( dx0 != 0 || dy0 != 0 );
665
scale = 1./std::sqrt(dx0*dx0 + dy0*dy0);
666
667
defect.start = hull_cur;
668
defect.end = hull_next;
669
670
for(;;)
671
{
672
/* go through ptseq to achieve next hull point */
673
CV_NEXT_SEQ_ELEM( sizeof(CvPoint), ptseq_reader );
674
675
if( ptseq_reader.ptr == (schar*)hull_next )
676
break;
677
else
678
{
679
CvPoint* cur = (CvPoint*)ptseq_reader.ptr;
680
681
/* compute distance from current point to hull edge */
682
double dx = (double)cur->x - (double)hull_cur->x;
683
double dy = (double)cur->y - (double)hull_cur->y;
684
685
/* compute depth */
686
double dist = fabs(-dy0*dx + dx0*dy) * scale;
687
688
if( dist > depth )
689
{
690
depth = dist;
691
defect.depth_point = cur;
692
defect.depth = (float)depth;
693
is_defect = 1;
694
}
695
}
696
}
697
if( is_defect )
698
{
699
CV_WRITE_SEQ_ELEM( defect, writer );
700
}
701
702
hull_cur = hull_next;
703
if( rev_orientation )
704
{
705
CV_PREV_SEQ_ELEM( hull->elem_size, hull_reader );
706
}
707
else
708
{
709
CV_NEXT_SEQ_ELEM( hull->elem_size, hull_reader );
710
}
711
}
712
713
return cvEndWriteSeq( &writer );
714
}
715
716
717
CV_IMPL int
718
cvCheckContourConvexity( const CvArr* array )
719
{
720
CvContour contour_header;
721
CvSeqBlock block;
722
CvSeq* contour = (CvSeq*)array;
723
724
if( CV_IS_SEQ(contour) )
725
{
726
if( !CV_IS_SEQ_POINT_SET(contour))
727
CV_Error( CV_StsUnsupportedFormat,
728
"Input sequence must be polygon (closed 2d curve)" );
729
}
730
else
731
{
732
contour = cvPointSeqFromMat(CV_SEQ_KIND_CURVE|
733
CV_SEQ_FLAG_CLOSED, array, &contour_header, &block );
734
}
735
736
if( contour->total == 0 )
737
return -1;
738
739
cv::AutoBuffer<double> _buf;
740
return cv::isContourConvex(cv::cvarrToMat(contour, false, false, 0, &_buf)) ? 1 : 0;
741
}
742
743
/* End of file. */
744
745