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.
43 /* calculates length of a curve (e.g. contour perimeter) */
45 cvArcLength( const void *array, CvSlice slice, int is_closed )
49 CV_FUNCNAME( "cvArcLength" );
56 CvMat buffer = cvMat( 1, N, CV_32F, buf );
58 CvContour contour_header;
62 if( CV_IS_SEQ( array ))
64 contour = (CvSeq*)array;
65 if( !CV_IS_SEQ_POLYLINE( contour ))
66 CV_ERROR( CV_StsBadArg, "Unsupported sequence type" );
68 is_closed = CV_IS_SEQ_CLOSED( contour );
72 is_closed = is_closed > 0;
73 CV_CALL( contour = cvPointSeqFromMat(
74 CV_SEQ_KIND_CURVE | (is_closed ? CV_SEQ_FLAG_CLOSED : 0),
75 array, &contour_header, &block ));
78 if( contour->total > 1 )
80 int is_float = CV_SEQ_ELTYPE( contour ) == CV_32FC2;
82 cvStartReadSeq( contour, &reader, 0 );
83 cvSetSeqReaderPos( &reader, slice.start_index );
84 count = cvSliceLength( slice, contour );
86 count -= !is_closed && count == contour->total;
88 /* scroll the reader by 1 point */
89 reader.prev_elem = reader.ptr;
90 CV_NEXT_SEQ_ELEM( sizeof(CvPoint), reader );
92 for( i = 0; i < count; i++ )
98 CvPoint* pt = (CvPoint*)reader.ptr;
99 CvPoint* prev_pt = (CvPoint*)reader.prev_elem;
101 dx = (float)pt->x - (float)prev_pt->x;
102 dy = (float)pt->y - (float)prev_pt->y;
106 CvPoint2D32f* pt = (CvPoint2D32f*)reader.ptr;
107 CvPoint2D32f* prev_pt = (CvPoint2D32f*)reader.prev_elem;
109 dx = pt->x - prev_pt->x;
110 dy = pt->y - prev_pt->y;
113 reader.prev_elem = reader.ptr;
114 CV_NEXT_SEQ_ELEM( contour->elem_size, reader );
116 buffer.data.fl[j] = dx * dx + dy * dy;
117 if( ++j == N || i == count - 1 )
120 cvPow( &buffer, &buffer, 0.5 );
122 perimeter += buffer.data.fl[j-1];
134 icvFindCircle( CvPoint2D32f pt0, CvPoint2D32f pt1,
135 CvPoint2D32f pt2, CvPoint2D32f * center, float *radius )
137 double x1 = (pt0.x + pt1.x) * 0.5;
138 double dy1 = pt0.x - pt1.x;
139 double x2 = (pt1.x + pt2.x) * 0.5;
140 double dy2 = pt1.x - pt2.x;
141 double y1 = (pt0.y + pt1.y) * 0.5;
142 double dx1 = pt1.y - pt0.y;
143 double y2 = (pt1.y + pt2.y) * 0.5;
144 double dx2 = pt2.y - pt1.y;
147 CvStatus result = CV_OK;
149 if( icvIntersectLines( x1, dx1, y1, dy1, x2, dx2, y2, dy2, &t ) >= 0 )
151 center->x = (float) (x2 + dx2 * t);
152 center->y = (float) (y2 + dy2 * t);
153 *radius = (float) icvDistanceL2_32f( *center, pt0 );
157 center->x = center->y = 0.f;
159 result = CV_NOTDEFINED_ERR;
166 CV_INLINE double icvIsPtInCircle( CvPoint2D32f pt, CvPoint2D32f center, float radius )
168 double dx = pt.x - center.x;
169 double dy = pt.y - center.y;
170 return (double)radius*radius - dx*dx - dy*dy;
175 icvFindEnslosingCicle4pts_32f( CvPoint2D32f * pts, CvPoint2D32f * _center, float *_radius )
177 int shuffles[4][4] = { {0, 1, 2, 3}, {0, 1, 3, 2}, {2, 3, 0, 1}, {2, 3, 1, 0} };
179 int idxs[4] = { 0, 1, 2, 3 };
180 int i, j, k = 1, mi = 0;
183 CvPoint2D32f min_center;
184 float radius, min_radius = FLT_MAX;
185 CvPoint2D32f res_pts[4];
187 center = min_center = pts[0];
190 for( i = 0; i < 4; i++ )
191 for( j = i + 1; j < 4; j++ )
193 float dist = icvDistanceL2_32f( pts[i], pts[j] );
195 if( max_dist < dist )
207 for( i = 0; i < 4; i++ )
209 for( j = 0; j < k; j++ )
216 center = cvPoint2D32f( (pts[idxs[0]].x + pts[idxs[1]].x)*0.5f,
217 (pts[idxs[0]].y + pts[idxs[1]].y)*0.5f );
218 radius = (float)(icvDistanceL2_32f( pts[idxs[0]], center )*1.03);
222 if( icvIsPtInCircle( pts[idxs[2]], center, radius ) >= 0 &&
223 icvIsPtInCircle( pts[idxs[3]], center, radius ) >= 0 )
230 for( i = 0; i < 4; i++ )
232 if( icvFindCircle( pts[shuffles[i][0]], pts[shuffles[i][1]],
233 pts[shuffles[i][2]], ¢er, &radius ) >= 0 )
239 if( icvIsPtInCircle( pts[shuffles[i][3]], center, radius ) >= 0 &&
240 min_radius > radius )
254 for( i = 0; i < 4; i++ )
255 idxs[i] = shuffles[mi][i];
263 /* reorder output points */
264 for( i = 0; i < 4; i++ )
265 res_pts[i] = pts[idxs[i]];
267 for( i = 0; i < 4; i++ )
270 assert( icvIsPtInCircle( pts[i], center, radius ) >= 0 );
278 cvMinEnclosingCircle( const void* array, CvPoint2D32f * _center, float *_radius )
280 const int max_iters = 100;
281 const float eps = FLT_EPSILON*2;
282 CvPoint2D32f center = { 0, 0 };
287 _center->x = _center->y = 0.f;
291 CV_FUNCNAME( "cvMinEnclosingCircle" );
298 CvContour contour_header;
303 if( !_center || !_radius )
304 CV_ERROR( CV_StsNullPtr, "Null center or radius pointers" );
306 if( CV_IS_SEQ(array) )
308 sequence = (CvSeq*)array;
309 if( !CV_IS_SEQ_POINT_SET( sequence ))
310 CV_ERROR( CV_StsBadArg, "The passed sequence is not a valid contour" );
314 CV_CALL( sequence = cvPointSeqFromMat(
315 CV_SEQ_KIND_GENERIC, array, &contour_header, &block ));
318 if( sequence->total <= 0 )
319 CV_ERROR_FROM_STATUS( CV_BADSIZE_ERR );
321 CV_CALL( cvStartReadSeq( sequence, &reader, 0 ));
323 count = sequence->total;
324 is_float = CV_SEQ_ELTYPE(sequence) == CV_32FC2;
328 CvPoint *pt_left, *pt_right, *pt_top, *pt_bottom;
330 pt_left = pt_right = pt_top = pt_bottom = (CvPoint *)(reader.ptr);
331 CV_READ_SEQ_ELEM( pt, reader );
333 for( i = 1; i < count; i++ )
335 CvPoint* pt_ptr = (CvPoint*)reader.ptr;
336 CV_READ_SEQ_ELEM( pt, reader );
338 if( pt.x < pt_left->x )
340 if( pt.x > pt_right->x )
342 if( pt.y < pt_top->y )
344 if( pt.y > pt_bottom->y )
348 pts[0] = cvPointTo32f( *pt_left );
349 pts[1] = cvPointTo32f( *pt_right );
350 pts[2] = cvPointTo32f( *pt_top );
351 pts[3] = cvPointTo32f( *pt_bottom );
355 CvPoint2D32f *pt_left, *pt_right, *pt_top, *pt_bottom;
357 pt_left = pt_right = pt_top = pt_bottom = (CvPoint2D32f *) (reader.ptr);
358 CV_READ_SEQ_ELEM( pt, reader );
360 for( i = 1; i < count; i++ )
362 CvPoint2D32f* pt_ptr = (CvPoint2D32f*)reader.ptr;
363 CV_READ_SEQ_ELEM( pt, reader );
365 if( pt.x < pt_left->x )
367 if( pt.x > pt_right->x )
369 if( pt.y < pt_top->y )
371 if( pt.y > pt_bottom->y )
381 for( k = 0; k < max_iters; k++ )
383 double min_delta = 0, delta;
386 icvFindEnslosingCicle4pts_32f( pts, ¢er, &radius );
387 cvStartReadSeq( sequence, &reader, 0 );
389 for( i = 0; i < count; i++ )
393 ptfl.x = (float)((CvPoint*)reader.ptr)->x;
394 ptfl.y = (float)((CvPoint*)reader.ptr)->y;
398 ptfl = *(CvPoint2D32f*)reader.ptr;
400 CV_NEXT_SEQ_ELEM( sequence->elem_size, reader );
402 delta = icvIsPtInCircle( ptfl, center, radius );
403 if( delta < min_delta )
409 result = min_delta >= 0;
416 cvStartReadSeq( sequence, &reader, 0 );
419 for( i = 0; i < count; i++ )
426 ptfl.x = (float)((CvPoint*)reader.ptr)->x;
427 ptfl.y = (float)((CvPoint*)reader.ptr)->y;
431 ptfl = *(CvPoint2D32f*)reader.ptr;
434 CV_NEXT_SEQ_ELEM( sequence->elem_size, reader );
435 dx = center.x - ptfl.x;
436 dy = center.y - ptfl.y;
438 radius = MAX(radius,t);
441 radius = (float)(sqrt(radius)*(1 + eps));
454 /* area of a whole sequence */
456 icvContourArea( const CvSeq* contour, double *area )
461 int lpt = contour->total;
462 double a00 = 0, xi_1, yi_1;
463 int is_float = CV_SEQ_ELTYPE(contour) == CV_32FC2;
465 cvStartReadSeq( contour, &reader, 0 );
469 xi_1 = ((CvPoint*)(reader.ptr))->x;
470 yi_1 = ((CvPoint*)(reader.ptr))->y;
474 xi_1 = ((CvPoint2D32f*)(reader.ptr))->x;
475 yi_1 = ((CvPoint2D32f*)(reader.ptr))->y;
477 CV_NEXT_SEQ_ELEM( contour->elem_size, reader );
485 xi = ((CvPoint*)(reader.ptr))->x;
486 yi = ((CvPoint*)(reader.ptr))->y;
490 xi = ((CvPoint2D32f*)(reader.ptr))->x;
491 yi = ((CvPoint2D32f*)(reader.ptr))->y;
493 CV_NEXT_SEQ_ELEM( contour->elem_size, reader );
495 dxy = xi_1 * yi - xi * yi_1;
510 /****************************************************************************************\
512 copy data from one buffer to other buffer
514 \****************************************************************************************/
517 icvMemCopy( double **buf1, double **buf2, double **buf3, int *b_max )
521 if( *buf1 == NULL && *buf2 == NULL || *buf3 == NULL )
522 return CV_NULLPTR_ERR;
527 *b_max = 2 * (*b_max);
528 *buf2 = (double *)cvAlloc( (*b_max) * sizeof( double ));
531 return CV_OUTOFMEM_ERR;
533 memcpy( *buf2, *buf3, bb * sizeof( double ));
541 *b_max = 2 * (*b_max);
542 *buf1 = (double *) cvAlloc( (*b_max) * sizeof( double ));
545 return CV_OUTOFMEM_ERR;
547 memcpy( *buf1, *buf3, bb * sizeof( double ));
557 /* area of a contour sector */
558 static CvStatus icvContourSecArea( CvSeq * contour, CvSlice slice, double *area )
560 CvPoint pt; /* pointer to points */
561 CvPoint pt_s, pt_e; /* first and last points */
562 CvSeqReader reader; /* points reader of contour */
564 int p_max = 2, p_ind;
566 double a00; /* unnormalized moments m00 */
567 double xi, yi, xi_1, yi_1, x0, y0, dxy, sk, sk1, t;
568 double x_s, y_s, nx, ny, dx, dy, du, dv;
570 double *p_are1, *p_are2, *p_are;
572 assert( contour != NULL );
574 if( contour == NULL )
575 return CV_NULLPTR_ERR;
577 if( !CV_IS_SEQ_POLYGON( contour ))
578 return CV_BADFLAG_ERR;
580 lpt = cvSliceLength( slice, contour );
584 lpt = contour->total - n1 + n2 + 1;*/
586 if( contour->total && lpt > 2 )
588 a00 = x0 = y0 = xi_1 = yi_1 = 0;
592 p_are1 = (double *) cvAlloc( p_max * sizeof( double ));
595 return CV_OUTOFMEM_ERR;
600 cvStartReadSeq( contour, &reader, 0 );
601 cvSetSeqReaderPos( &reader, slice.start_index );
602 CV_READ_SEQ_ELEM( pt_s, reader );
604 cvSetSeqReaderPos( &reader, slice.end_index );
605 CV_READ_SEQ_ELEM( pt_e, reader );
607 /* normal coefficients */
608 nx = pt_s.y - pt_e.y;
609 ny = pt_e.x - pt_s.x;
610 cvSetSeqReaderPos( &reader, slice.start_index );
614 CV_READ_SEQ_ELEM( pt, reader );
618 xi_1 = (double) pt.x;
619 yi_1 = (double) pt.y;
630 /**************** edges intersection examination **************************/
631 sk = nx * (xi - pt_s.x) + ny * (yi - pt_s.y);
632 if( fabs( sk ) < eps && lpt > 0 || sk * sk1 < -eps )
634 if( fabs( sk ) < eps )
636 dxy = xi_1 * yi - xi * yi_1;
638 dxy = xi * y0 - x0 * yi;
642 icvMemCopy( &p_are1, &p_are2, &p_are, &p_max );
644 p_are[p_ind] = a00 / 2.;
654 /* define intersection point */
659 if( fabs( du ) > eps )
660 t = ((yi_1 - pt_s.y) * du + dv * (pt_s.x - xi_1)) /
663 t = (xi_1 - pt_s.x) / dx;
664 if( t > eps && t < 1 - eps )
666 x_s = pt_s.x + t * dx;
667 y_s = pt_s.y + t * dy;
668 dxy = xi_1 * y_s - x_s * yi_1;
670 dxy = x_s * y0 - x0 * y_s;
673 icvMemCopy( &p_are1, &p_are2, &p_are, &p_max );
675 p_are[p_ind] = a00 / 2.;
682 dxy = x_s * yi - xi * y_s;
687 dxy = xi_1 * yi - xi * yi_1;
699 dxy = xi_1 * yi - xi * yi_1;
704 icvMemCopy( &p_are1, &p_are2, &p_are, &p_max );
706 p_are[p_ind] = a00 / 2.;
709 /* common area calculation */
711 for( i = 0; i < p_ind; i++ )
712 (*area) += fabs( p_are[i] );
716 else if( p_are2 != NULL )
722 return CV_BADSIZE_ERR;
726 /* external contour area function */
728 cvContourArea( const void *array, CvSlice slice )
732 CV_FUNCNAME( "cvContourArea" );
736 CvContour contour_header;
740 if( CV_IS_SEQ( array ))
742 contour = (CvSeq*)array;
743 if( !CV_IS_SEQ_POLYLINE( contour ))
744 CV_ERROR( CV_StsBadArg, "Unsupported sequence type" );
748 CV_CALL( contour = cvPointSeqFromMat(
749 CV_SEQ_KIND_CURVE, array, &contour_header, &block ));
752 if( cvSliceLength( slice, contour ) == contour->total )
754 IPPI_CALL( icvContourArea( contour, &area ));
758 if( CV_SEQ_ELTYPE( contour ) != CV_32SC2 )
759 CV_ERROR( CV_StsUnsupportedFormat,
760 "Only curves with integer coordinates are supported in case of contour slice" );
761 IPPI_CALL( icvContourSecArea( contour, slice, &area ));
770 /* for now this function works bad with singular cases
771 You can see in the code, that when some troubles with
772 matrices or some variables occur -
773 box filled with zero values is returned.
774 However in general function works fine.
777 icvFitEllipse_F( CvSeq* points, CvBox2D* box )
781 CV_FUNCNAME( "icvFitEllipse_F" );
785 double S[36], C[36], T[36];
788 double eigenvalues[6], eigenvectors[36];
789 double a, b, c, d, e, f;
790 double x0, y0, idet, scale, offx = 0, offy = 0;
792 int n = points->total;
794 int is_float = CV_SEQ_ELTYPE(points) == CV_32FC2;
796 CvMat _S = cvMat(6,6,CV_64F,S), _C = cvMat(6,6,CV_64F,C), _T = cvMat(6,6,CV_64F,T);
797 CvMat _EIGVECS = cvMat(6,6,CV_64F,eigenvectors), _EIGVALS = cvMat(6,1,CV_64F,eigenvalues);
799 /* create matrix D of input points */
800 CV_CALL( D = cvCreateMat( n, 6, CV_64F ));
802 cvStartReadSeq( points, &reader );
804 /* shift all points to zero */
805 for( i = 0; i < n; i++ )
809 offx += ((CvPoint*)reader.ptr)->x;
810 offy += ((CvPoint*)reader.ptr)->y;
814 offx += ((CvPoint2D32f*)reader.ptr)->x;
815 offy += ((CvPoint2D32f*)reader.ptr)->y;
817 CV_NEXT_SEQ_ELEM( points->elem_size, reader );
823 // fill matrix rows as (x*x, x*y, y*y, x, y, 1 )
824 for( i = 0; i < n; i++ )
827 double* Dptr = D->data.db + i*6;
831 x = ((CvPoint*)reader.ptr)->x - offx;
832 y = ((CvPoint*)reader.ptr)->y - offy;
836 x = ((CvPoint2D32f*)reader.ptr)->x - offx;
837 y = ((CvPoint2D32f*)reader.ptr)->y - offy;
839 CV_NEXT_SEQ_ELEM( points->elem_size, reader );
850 cvMulTransposed( D, &_S, 1 );
851 cvSVD( &_S, &_EIGVALS, &_EIGVECS, 0, CV_SVD_MODIFY_A + CV_SVD_U_T );
853 for( i = 0; i < 6; i++ )
855 double a = eigenvalues[i];
856 a = a < DBL_EPSILON ? 0 : 1./sqrt(sqrt(a));
857 for( j = 0; j < 6; j++ )
858 eigenvectors[i*6 + j] *= a;
861 // C = Q^-1 = transp(INVEIGV) * INVEIGV
862 cvMulTransposed( &_EIGVECS, &_C, 1 );
870 cvMatMul( &_C, &_S, &_T );
871 cvMatMul( &_T, &_C, &_S );
873 // and find its eigenvalues and vectors too
874 //cvSVD( &_S, &_EIGVALS, &_EIGVECS, 0, CV_SVD_MODIFY_A + CV_SVD_U_T );
875 cvEigenVV( &_S, &_EIGVECS, &_EIGVALS, 0 );
877 for( i = 0; i < 3; i++ )
878 if( eigenvalues[i] > 0 )
881 if( i >= 3 /*eigenvalues[0] < DBL_EPSILON*/ )
883 box->center.x = box->center.y =
884 box->size.width = box->size.height =
889 // now find truthful eigenvector
890 _EIGVECS = cvMat( 6, 1, CV_64F, eigenvectors + 6*i );
891 _T = cvMat( 6, 1, CV_64F, T );
893 cvMatMul( &_C, &_EIGVECS, &_T );
895 // extract vector components
896 a = T[0]; b = T[1]; c = T[2]; d = T[3]; e = T[4]; f = T[5];
898 ///////////////// extract ellipse axes from above values ////////////////
901 1) find center of ellipse
903 | a b/2 | * | x0 | + | d/2 | = |0 |
904 | b/2 c | | y0 | | e/2 | |0 |
907 idet = a * c - b * b * 0.25;
908 idet = idet > DBL_EPSILON ? 1./idet : 0;
910 // we must normalize (a b c d e f ) to fit (4ac-b^2=1)
911 scale = sqrt( 0.25 * idet );
913 if( scale < DBL_EPSILON )
915 box->center.x = (float)offx;
916 box->center.y = (float)offy;
917 box->size.width = box->size.height = box->angle = 0.f;
928 x0 = (-d * c + e * b * 0.5) * 2.;
929 y0 = (-a * e + d * b * 0.5) * 2.;
932 box->center.x = (float)(x0 + offx);
933 box->center.y = (float)(y0 + offy);
935 // offset ellipse to (x0,y0)
937 f += a * x0 * x0 + b * x0 * y0 + c * y0 * y0 + d * x0 + e * y0;
939 if( fabs(f) < DBL_EPSILON )
941 box->size.width = box->size.height = box->angle = 0.f;
946 // normalize to f = 1
951 // extract axis of ellipse
952 // one more eigenvalue operation
954 S[1] = S[2] = b * 0.5;
957 _S = cvMat( 2, 2, CV_64F, S );
958 _EIGVECS = cvMat( 2, 2, CV_64F, eigenvectors );
959 _EIGVALS = cvMat( 1, 2, CV_64F, eigenvalues );
960 cvSVD( &_S, &_EIGVALS, &_EIGVECS, 0, CV_SVD_MODIFY_A + CV_SVD_U_T );
962 // exteract axis length from eigenvectors
963 box->size.width = (float)(2./sqrt(eigenvalues[0]));
964 box->size.height = (float)(2./sqrt(eigenvalues[1]));
967 box->angle = (float)(180 - atan2(eigenvectors[2], eigenvectors[3])*180/CV_PI);
976 cvFitEllipse2( const CvArr* array )
979 double* Ad = 0, *bd = 0;
981 CV_FUNCNAME( "cvFitEllipse2" );
983 memset( &box, 0, sizeof(box));
987 CvContour contour_header;
992 if( CV_IS_SEQ( array ))
994 ptseq = (CvSeq*)array;
995 if( !CV_IS_SEQ_POINT_SET( ptseq ))
996 CV_ERROR( CV_StsBadArg, "Unsupported sequence type" );
1000 CV_CALL( ptseq = cvPointSeqFromMat(
1001 CV_SEQ_KIND_GENERIC, array, &contour_header, &block ));
1006 CV_ERROR( CV_StsBadSize, "Number of points should be >= 6" );
1008 icvFitEllipse_F( ptseq, &box );
1011 * New fitellipse algorithm, contributed by Dr. Daniel Weiss
1014 double gfp[5], rp[5], t;
1016 const double min_eps = 1e-6;
1020 CV_CALL( Ad = (double*)cvAlloc( n*5*sizeof(Ad[0]) ));
1021 CV_CALL( bd = (double*)cvAlloc( n*sizeof(bd[0]) ));
1023 // first fit for parameters A - E
1024 A = cvMat( n, 5, CV_64F, Ad );
1025 b = cvMat( n, 1, CV_64F, bd );
1026 x = cvMat( 5, 1, CV_64F, gfp );
1028 cvStartReadSeq( ptseq, &reader );
1029 is_float = CV_SEQ_ELTYPE(ptseq) == CV_32FC2;
1031 for( i = 0; i < n; i++ )
1035 p = *(CvPoint2D32f*)(reader.ptr);
1038 p.x = (float)((int*)reader.ptr)[0];
1039 p.y = (float)((int*)reader.ptr)[1];
1041 CV_NEXT_SEQ_ELEM( sizeof(p), reader );
1043 bd[i] = 10000.0; // 1.0?
1044 Ad[i*5] = -(double)p.x * p.x; // A - C signs inverted as proposed by APP
1045 Ad[i*5 + 1] = -(double)p.y * p.y;
1046 Ad[i*5 + 2] = -(double)p.x * p.y;
1051 cvSolve( &A, &b, &x, CV_SVD );
1053 // now use general-form parameters A - E to find the ellipse center:
1054 // differentiate general form wrt x/y to get two equations for cx and cy
1055 A = cvMat( 2, 2, CV_64F, Ad );
1056 b = cvMat( 2, 1, CV_64F, bd );
1057 x = cvMat( 2, 1, CV_64F, rp );
1059 Ad[1] = Ad[2] = gfp[2];
1063 cvSolve( &A, &b, &x, CV_SVD );
1065 // re-fit for parameters A - C with those center coordinates
1066 A = cvMat( n, 3, CV_64F, Ad );
1067 b = cvMat( n, 1, CV_64F, bd );
1068 x = cvMat( 3, 1, CV_64F, gfp );
1069 for( i = 0; i < n; i++ )
1073 p = *(CvPoint2D32f*)(reader.ptr);
1076 p.x = (float)((int*)reader.ptr)[0];
1077 p.y = (float)((int*)reader.ptr)[1];
1079 CV_NEXT_SEQ_ELEM( sizeof(p), reader );
1081 Ad[i * 3] = (p.x - rp[0]) * (p.x - rp[0]);
1082 Ad[i * 3 + 1] = (p.y - rp[1]) * (p.y - rp[1]);
1083 Ad[i * 3 + 2] = (p.x - rp[0]) * (p.y - rp[1]);
1085 cvSolve(&A, &b, &x, CV_SVD);
1087 // store angle and radii
1088 rp[4] = -0.5 * atan2(gfp[2], gfp[1] - gfp[0]); // convert from APP angle usage
1089 t = sin(-2.0 * rp[4]);
1090 if( fabs(t) > fabs(gfp[2])*min_eps )
1093 t = gfp[1] - gfp[0];
1094 rp[2] = fabs(gfp[0] + gfp[1] - t);
1095 if( rp[2] > min_eps )
1096 rp[2] = sqrt(2.0 / rp[2]);
1097 rp[3] = fabs(gfp[0] + gfp[1] + t);
1098 if( rp[3] > min_eps )
1099 rp[3] = sqrt(2.0 / rp[3]);
1101 box.center.x = (float)rp[0];
1102 box.center.y = (float)rp[1];
1103 box.size.width = (float)(rp[2]*2);
1104 box.size.height = (float)(rp[3]*2);
1105 if( box.size.width > box.size.height )
1108 CV_SWAP( box.size.width, box.size.height, tmp );
1109 box.angle = (float)(90 + rp[4]*180/CV_PI);
1111 if( box.angle < -180 )
1113 if( box.angle > 360 )
1126 /* Calculates bounding rectagnle of a point set or retrieves already calculated */
1128 cvBoundingRect( CvArr* array, int update )
1131 CvRect rect = { 0, 0, 0, 0 };
1132 CvContour contour_header;
1136 CV_FUNCNAME( "cvBoundingRect" );
1140 CvMat stub, *mat = 0;
1141 int xmin = 0, ymin = 0, xmax = -1, ymax = -1, i, j, k;
1142 int calculate = update;
1144 if( CV_IS_SEQ( array ))
1146 ptseq = (CvSeq*)array;
1147 if( !CV_IS_SEQ_POINT_SET( ptseq ))
1148 CV_ERROR( CV_StsBadArg, "Unsupported sequence type" );
1150 if( ptseq->header_size < (int)sizeof(CvContour))
1153 CV_ERROR( CV_StsBadArg, "The header is too small to fit the rectangle, "
1154 "so it could not be updated" );*/
1161 CV_CALL( mat = cvGetMat( array, &stub ));
1162 if( CV_MAT_TYPE(mat->type) == CV_32SC2 ||
1163 CV_MAT_TYPE(mat->type) == CV_32FC2 )
1165 CV_CALL( ptseq = cvPointSeqFromMat(
1166 CV_SEQ_KIND_GENERIC, mat, &contour_header, &block ));
1169 else if( CV_MAT_TYPE(mat->type) != CV_8UC1 &&
1170 CV_MAT_TYPE(mat->type) != CV_8SC1 )
1171 CV_ERROR( CV_StsUnsupportedFormat,
1172 "The image/matrix format is not supported by the function" );
1179 rect = ((CvContour*)ptseq)->rect;
1185 CvSize size = cvGetMatSize(mat);
1189 for( i = 0; i < size.height; i++ )
1191 uchar* _ptr = mat->data.ptr + i*mat->step;
1192 uchar* ptr = (uchar*)cvAlignPtr(_ptr, 4);
1193 int have_nz = 0, k_min, offset = (int)(ptr - _ptr);
1195 offset = MIN(offset, size.width);
1196 for( ; j < offset; j++ )
1209 if( offset < size.width )
1213 size.width -= offset;
1215 for( ; j <= xmin - 4; j += 4 )
1216 if( *((int*)(ptr+j)) )
1218 for( ; j < xmin; j++ )
1227 k_min = MAX(j-1, xmax);
1229 for( ; k > k_min && (k&3) != 3; k-- )
1232 if( k > k_min && (k&3) == 3 )
1234 for( ; k > k_min+3; k -= 4 )
1235 if( *((int*)(ptr+k-3)) )
1238 for( ; k > k_min; k-- )
1248 for( ; j <= k - 3; j += 4 )
1249 if( *((int*)(ptr+j)) )
1251 for( ; j <= k; j++ )
1260 size.width += offset;
1270 if( xmin >= size.width )
1273 else if( ptseq->total )
1275 int is_float = CV_SEQ_ELTYPE(ptseq) == CV_32FC2;
1276 cvStartReadSeq( ptseq, &reader, 0 );
1282 CV_READ_SEQ_ELEM( pt, reader );
1286 for( i = 1; i < ptseq->total; i++ )
1288 CV_READ_SEQ_ELEM( pt, reader );
1308 CV_READ_SEQ_ELEM( pt, reader );
1309 xmin = xmax = CV_TOGGLE_FLT(pt.x);
1310 ymin = ymax = CV_TOGGLE_FLT(pt.y);
1312 for( i = 1; i < ptseq->total; i++ )
1314 CV_READ_SEQ_ELEM( pt, reader );
1315 pt.x = CV_TOGGLE_FLT(pt.x);
1316 pt.y = CV_TOGGLE_FLT(pt.y);
1331 v.i = CV_TOGGLE_FLT(xmin); xmin = cvFloor(v.f);
1332 v.i = CV_TOGGLE_FLT(ymin); ymin = cvFloor(v.f);
1333 /* because right and bottom sides of
1334 the bounding rectangle are not inclusive
1335 (note +1 in width and height calculation below),
1336 cvFloor is used here instead of cvCeil */
1337 v.i = CV_TOGGLE_FLT(xmax); xmax = cvFloor(v.f);
1338 v.i = CV_TOGGLE_FLT(ymax); ymax = cvFloor(v.f);
1344 rect.width = xmax - xmin + 1;
1345 rect.height = ymax - ymin + 1;
1348 ((CvContour*)ptseq)->rect = rect;