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 )
52 CvMat buffer = cvMat( 1, N, CV_32F, buf );
54 CvContour contour_header;
58 if( CV_IS_SEQ( array ))
60 contour = (CvSeq*)array;
61 if( !CV_IS_SEQ_POLYLINE( contour ))
62 CV_Error( CV_StsBadArg, "Unsupported sequence type" );
64 is_closed = CV_IS_SEQ_CLOSED( contour );
68 is_closed = is_closed > 0;
69 contour = cvPointSeqFromMat(
70 CV_SEQ_KIND_CURVE | (is_closed ? CV_SEQ_FLAG_CLOSED : 0),
71 array, &contour_header, &block );
74 if( contour->total > 1 )
76 int is_float = CV_SEQ_ELTYPE( contour ) == CV_32FC2;
78 cvStartReadSeq( contour, &reader, 0 );
79 cvSetSeqReaderPos( &reader, slice.start_index );
80 count = cvSliceLength( slice, contour );
82 count -= !is_closed && count == contour->total;
84 /* scroll the reader by 1 point */
85 reader.prev_elem = reader.ptr;
86 CV_NEXT_SEQ_ELEM( sizeof(CvPoint), reader );
88 for( i = 0; i < count; i++ )
94 CvPoint* pt = (CvPoint*)reader.ptr;
95 CvPoint* prev_pt = (CvPoint*)reader.prev_elem;
97 dx = (float)pt->x - (float)prev_pt->x;
98 dy = (float)pt->y - (float)prev_pt->y;
102 CvPoint2D32f* pt = (CvPoint2D32f*)reader.ptr;
103 CvPoint2D32f* prev_pt = (CvPoint2D32f*)reader.prev_elem;
105 dx = pt->x - prev_pt->x;
106 dy = pt->y - prev_pt->y;
109 reader.prev_elem = reader.ptr;
110 CV_NEXT_SEQ_ELEM( contour->elem_size, reader );
112 buffer.data.fl[j] = dx * dx + dy * dy;
113 if( ++j == N || i == count - 1 )
116 cvPow( &buffer, &buffer, 0.5 );
118 perimeter += buffer.data.fl[j-1];
128 icvFindCircle( CvPoint2D32f pt0, CvPoint2D32f pt1,
129 CvPoint2D32f pt2, CvPoint2D32f * center, float *radius )
131 double x1 = (pt0.x + pt1.x) * 0.5;
132 double dy1 = pt0.x - pt1.x;
133 double x2 = (pt1.x + pt2.x) * 0.5;
134 double dy2 = pt1.x - pt2.x;
135 double y1 = (pt0.y + pt1.y) * 0.5;
136 double dx1 = pt1.y - pt0.y;
137 double y2 = (pt1.y + pt2.y) * 0.5;
138 double dx2 = pt2.y - pt1.y;
141 CvStatus result = CV_OK;
143 if( icvIntersectLines( x1, dx1, y1, dy1, x2, dx2, y2, dy2, &t ) >= 0 )
145 center->x = (float) (x2 + dx2 * t);
146 center->y = (float) (y2 + dy2 * t);
147 *radius = (float) icvDistanceL2_32f( *center, pt0 );
151 center->x = center->y = 0.f;
153 result = CV_NOTDEFINED_ERR;
160 CV_INLINE double icvIsPtInCircle( CvPoint2D32f pt, CvPoint2D32f center, float radius )
162 double dx = pt.x - center.x;
163 double dy = pt.y - center.y;
164 return (double)radius*radius - dx*dx - dy*dy;
169 icvFindEnslosingCicle4pts_32f( CvPoint2D32f * pts, CvPoint2D32f * _center, float *_radius )
171 int shuffles[4][4] = { {0, 1, 2, 3}, {0, 1, 3, 2}, {2, 3, 0, 1}, {2, 3, 1, 0} };
173 int idxs[4] = { 0, 1, 2, 3 };
174 int i, j, k = 1, mi = 0;
177 CvPoint2D32f min_center;
178 float radius, min_radius = FLT_MAX;
179 CvPoint2D32f res_pts[4];
181 center = min_center = pts[0];
184 for( i = 0; i < 4; i++ )
185 for( j = i + 1; j < 4; j++ )
187 float dist = icvDistanceL2_32f( pts[i], pts[j] );
189 if( max_dist < dist )
201 for( i = 0; i < 4; i++ )
203 for( j = 0; j < k; j++ )
210 center = cvPoint2D32f( (pts[idxs[0]].x + pts[idxs[1]].x)*0.5f,
211 (pts[idxs[0]].y + pts[idxs[1]].y)*0.5f );
212 radius = (float)(icvDistanceL2_32f( pts[idxs[0]], center )*1.03);
216 if( icvIsPtInCircle( pts[idxs[2]], center, radius ) >= 0 &&
217 icvIsPtInCircle( pts[idxs[3]], center, radius ) >= 0 )
224 for( i = 0; i < 4; i++ )
226 if( icvFindCircle( pts[shuffles[i][0]], pts[shuffles[i][1]],
227 pts[shuffles[i][2]], ¢er, &radius ) >= 0 )
233 if( icvIsPtInCircle( pts[shuffles[i][3]], center, radius ) >= 0 &&
234 min_radius > radius )
248 for( i = 0; i < 4; i++ )
249 idxs[i] = shuffles[mi][i];
257 /* reorder output points */
258 for( i = 0; i < 4; i++ )
259 res_pts[i] = pts[idxs[i]];
261 for( i = 0; i < 4; i++ )
264 assert( icvIsPtInCircle( pts[i], center, radius ) >= 0 );
272 cvMinEnclosingCircle( const void* array, CvPoint2D32f * _center, float *_radius )
274 const int max_iters = 100;
275 const float eps = FLT_EPSILON*2;
276 CvPoint2D32f center = { 0, 0 };
281 _center->x = _center->y = 0.f;
288 CvContour contour_header;
293 if( !_center || !_radius )
294 CV_Error( CV_StsNullPtr, "Null center or radius pointers" );
296 if( CV_IS_SEQ(array) )
298 sequence = (CvSeq*)array;
299 if( !CV_IS_SEQ_POINT_SET( sequence ))
300 CV_Error( CV_StsBadArg, "The passed sequence is not a valid contour" );
304 sequence = cvPointSeqFromMat(
305 CV_SEQ_KIND_GENERIC, array, &contour_header, &block );
308 if( sequence->total <= 0 )
309 CV_Error( CV_StsBadSize, "" );
311 cvStartReadSeq( sequence, &reader, 0 );
313 count = sequence->total;
314 is_float = CV_SEQ_ELTYPE(sequence) == CV_32FC2;
318 CvPoint *pt_left, *pt_right, *pt_top, *pt_bottom;
320 pt_left = pt_right = pt_top = pt_bottom = (CvPoint *)(reader.ptr);
321 CV_READ_SEQ_ELEM( pt, reader );
323 for( i = 1; i < count; i++ )
325 CvPoint* pt_ptr = (CvPoint*)reader.ptr;
326 CV_READ_SEQ_ELEM( pt, reader );
328 if( pt.x < pt_left->x )
330 if( pt.x > pt_right->x )
332 if( pt.y < pt_top->y )
334 if( pt.y > pt_bottom->y )
338 pts[0] = cvPointTo32f( *pt_left );
339 pts[1] = cvPointTo32f( *pt_right );
340 pts[2] = cvPointTo32f( *pt_top );
341 pts[3] = cvPointTo32f( *pt_bottom );
345 CvPoint2D32f *pt_left, *pt_right, *pt_top, *pt_bottom;
347 pt_left = pt_right = pt_top = pt_bottom = (CvPoint2D32f *) (reader.ptr);
348 CV_READ_SEQ_ELEM( pt, reader );
350 for( i = 1; i < count; i++ )
352 CvPoint2D32f* pt_ptr = (CvPoint2D32f*)reader.ptr;
353 CV_READ_SEQ_ELEM( pt, reader );
355 if( pt.x < pt_left->x )
357 if( pt.x > pt_right->x )
359 if( pt.y < pt_top->y )
361 if( pt.y > pt_bottom->y )
371 for( k = 0; k < max_iters; k++ )
373 double min_delta = 0, delta;
376 icvFindEnslosingCicle4pts_32f( pts, ¢er, &radius );
377 cvStartReadSeq( sequence, &reader, 0 );
379 for( i = 0; i < count; i++ )
383 ptfl.x = (float)((CvPoint*)reader.ptr)->x;
384 ptfl.y = (float)((CvPoint*)reader.ptr)->y;
388 ptfl = *(CvPoint2D32f*)reader.ptr;
390 CV_NEXT_SEQ_ELEM( sequence->elem_size, reader );
392 delta = icvIsPtInCircle( ptfl, center, radius );
393 if( delta < min_delta )
399 result = min_delta >= 0;
406 cvStartReadSeq( sequence, &reader, 0 );
409 for( i = 0; i < count; i++ )
416 ptfl.x = (float)((CvPoint*)reader.ptr)->x;
417 ptfl.y = (float)((CvPoint*)reader.ptr)->y;
421 ptfl = *(CvPoint2D32f*)reader.ptr;
424 CV_NEXT_SEQ_ELEM( sequence->elem_size, reader );
425 dx = center.x - ptfl.x;
426 dy = center.y - ptfl.y;
428 radius = MAX(radius,t);
431 radius = (float)(sqrt(radius)*(1 + eps));
442 /* area of a whole sequence */
444 icvContourArea( const CvSeq* contour, double *area )
449 int lpt = contour->total;
450 double a00 = 0, xi_1, yi_1;
451 int is_float = CV_SEQ_ELTYPE(contour) == CV_32FC2;
453 cvStartReadSeq( contour, &reader, 0 );
457 xi_1 = ((CvPoint*)(reader.ptr))->x;
458 yi_1 = ((CvPoint*)(reader.ptr))->y;
462 xi_1 = ((CvPoint2D32f*)(reader.ptr))->x;
463 yi_1 = ((CvPoint2D32f*)(reader.ptr))->y;
465 CV_NEXT_SEQ_ELEM( contour->elem_size, reader );
473 xi = ((CvPoint*)(reader.ptr))->x;
474 yi = ((CvPoint*)(reader.ptr))->y;
478 xi = ((CvPoint2D32f*)(reader.ptr))->x;
479 yi = ((CvPoint2D32f*)(reader.ptr))->y;
481 CV_NEXT_SEQ_ELEM( contour->elem_size, reader );
483 dxy = xi_1 * yi - xi * yi_1;
498 /****************************************************************************************\
500 copy data from one buffer to other buffer
502 \****************************************************************************************/
505 icvMemCopy( double **buf1, double **buf2, double **buf3, int *b_max )
509 if( (*buf1 == NULL && *buf2 == NULL) || *buf3 == NULL )
510 return CV_NULLPTR_ERR;
515 *b_max = 2 * (*b_max);
516 *buf2 = (double *)cvAlloc( (*b_max) * sizeof( double ));
519 return CV_OUTOFMEM_ERR;
521 memcpy( *buf2, *buf3, bb * sizeof( double ));
529 *b_max = 2 * (*b_max);
530 *buf1 = (double *) cvAlloc( (*b_max) * sizeof( double ));
533 return CV_OUTOFMEM_ERR;
535 memcpy( *buf1, *buf3, bb * sizeof( double ));
545 /* area of a contour sector */
546 static CvStatus icvContourSecArea( CvSeq * contour, CvSlice slice, double *area )
548 CvPoint pt; /* pointer to points */
549 CvPoint pt_s, pt_e; /* first and last points */
550 CvSeqReader reader; /* points reader of contour */
552 int p_max = 2, p_ind;
554 double a00; /* unnormalized moments m00 */
555 double xi, yi, xi_1, yi_1, x0, y0, dxy, sk, sk1, t;
556 double x_s, y_s, nx, ny, dx, dy, du, dv;
558 double *p_are1, *p_are2, *p_are;
560 assert( contour != NULL );
562 if( contour == NULL )
563 return CV_NULLPTR_ERR;
565 if( !CV_IS_SEQ_POINT_SET( contour ))
566 return CV_BADFLAG_ERR;
568 lpt = cvSliceLength( slice, contour );
572 lpt = contour->total - n1 + n2 + 1;*/
574 if( contour->total && lpt > 2 )
576 a00 = x0 = y0 = xi_1 = yi_1 = 0;
580 p_are1 = (double *) cvAlloc( p_max * sizeof( double ));
583 return CV_OUTOFMEM_ERR;
588 cvStartReadSeq( contour, &reader, 0 );
589 cvSetSeqReaderPos( &reader, slice.start_index );
590 CV_READ_SEQ_ELEM( pt_s, reader );
592 cvSetSeqReaderPos( &reader, slice.end_index );
593 CV_READ_SEQ_ELEM( pt_e, reader );
595 /* normal coefficients */
596 nx = pt_s.y - pt_e.y;
597 ny = pt_e.x - pt_s.x;
598 cvSetSeqReaderPos( &reader, slice.start_index );
602 CV_READ_SEQ_ELEM( pt, reader );
606 xi_1 = (double) pt.x;
607 yi_1 = (double) pt.y;
618 /**************** edges intersection examination **************************/
619 sk = nx * (xi - pt_s.x) + ny * (yi - pt_s.y);
620 if( (fabs( sk ) < eps && lpt > 0) || sk * sk1 < -eps )
622 if( fabs( sk ) < eps )
624 dxy = xi_1 * yi - xi * yi_1;
626 dxy = xi * y0 - x0 * yi;
630 icvMemCopy( &p_are1, &p_are2, &p_are, &p_max );
632 p_are[p_ind] = a00 / 2.;
642 /* define intersection point */
647 if( fabs( du ) > eps )
648 t = ((yi_1 - pt_s.y) * du + dv * (pt_s.x - xi_1)) /
651 t = (xi_1 - pt_s.x) / dx;
652 if( t > eps && t < 1 - eps )
654 x_s = pt_s.x + t * dx;
655 y_s = pt_s.y + t * dy;
656 dxy = xi_1 * y_s - x_s * yi_1;
658 dxy = x_s * y0 - x0 * y_s;
661 icvMemCopy( &p_are1, &p_are2, &p_are, &p_max );
663 p_are[p_ind] = a00 / 2.;
670 dxy = x_s * yi - xi * y_s;
675 dxy = xi_1 * yi - xi * yi_1;
687 dxy = xi_1 * yi - xi * yi_1;
692 icvMemCopy( &p_are1, &p_are2, &p_are, &p_max );
694 p_are[p_ind] = a00 / 2.;
697 /* common area calculation */
699 for( i = 0; i < p_ind; i++ )
700 (*area) += fabs( p_are[i] );
704 else if( p_are2 != NULL )
710 return CV_BADSIZE_ERR;
714 /* external contour area function */
716 cvContourArea( const void *array, CvSlice slice )
720 CvContour contour_header;
724 if( CV_IS_SEQ( array ))
726 contour = (CvSeq*)array;
727 if( !CV_IS_SEQ_POLYLINE( contour ))
728 CV_Error( CV_StsBadArg, "Unsupported sequence type" );
732 contour = cvPointSeqFromMat( CV_SEQ_KIND_CURVE, array, &contour_header, &block );
735 if( cvSliceLength( slice, contour ) == contour->total )
737 IPPI_CALL( icvContourArea( contour, &area ));
741 if( CV_SEQ_ELTYPE( contour ) != CV_32SC2 )
742 CV_Error( CV_StsUnsupportedFormat,
743 "Only curves with integer coordinates are supported in case of contour slice" );
744 IPPI_CALL( icvContourSecArea( contour, slice, &area ));
751 /* for now this function works bad with singular cases
752 You can see in the code, that when some troubles with
753 matrices or some variables occur -
754 box filled with zero values is returned.
755 However in general function works fine.
758 icvFitEllipse_F( CvSeq* points, CvBox2D* box )
761 double S[36], C[36], T[36];
764 double eigenvalues[6], eigenvectors[36];
765 double a, b, c, d, e, f;
766 double x0, y0, idet, scale, offx = 0, offy = 0;
768 int n = points->total;
770 int is_float = CV_SEQ_ELTYPE(points) == CV_32FC2;
772 CvMat _S = cvMat(6,6,CV_64F,S), _C = cvMat(6,6,CV_64F,C), _T = cvMat(6,6,CV_64F,T);
773 CvMat _EIGVECS = cvMat(6,6,CV_64F,eigenvectors), _EIGVALS = cvMat(6,1,CV_64F,eigenvalues);
775 /* create matrix D of input points */
776 D = cvCreateMat( n, 6, CV_64F );
778 cvStartReadSeq( points, &reader );
780 /* shift all points to zero */
781 for( i = 0; i < n; i++ )
785 offx += ((CvPoint*)reader.ptr)->x;
786 offy += ((CvPoint*)reader.ptr)->y;
790 offx += ((CvPoint2D32f*)reader.ptr)->x;
791 offy += ((CvPoint2D32f*)reader.ptr)->y;
793 CV_NEXT_SEQ_ELEM( points->elem_size, reader );
799 // fill matrix rows as (x*x, x*y, y*y, x, y, 1 )
800 for( i = 0; i < n; i++ )
803 double* Dptr = D->data.db + i*6;
807 x = ((CvPoint*)reader.ptr)->x - offx;
808 y = ((CvPoint*)reader.ptr)->y - offy;
812 x = ((CvPoint2D32f*)reader.ptr)->x - offx;
813 y = ((CvPoint2D32f*)reader.ptr)->y - offy;
815 CV_NEXT_SEQ_ELEM( points->elem_size, reader );
826 cvMulTransposed( D, &_S, 1 );
827 cvSVD( &_S, &_EIGVALS, &_EIGVECS, 0, CV_SVD_MODIFY_A + CV_SVD_U_T );
829 for( i = 0; i < 6; i++ )
831 double a = eigenvalues[i];
832 a = a < DBL_EPSILON ? 0 : 1./sqrt(sqrt(a));
833 for( j = 0; j < 6; j++ )
834 eigenvectors[i*6 + j] *= a;
837 // C = Q^-1 = transp(INVEIGV) * INVEIGV
838 cvMulTransposed( &_EIGVECS, &_C, 1 );
846 cvMatMul( &_C, &_S, &_T );
847 cvMatMul( &_T, &_C, &_S );
849 // and find its eigenvalues and vectors too
850 //cvSVD( &_S, &_EIGVALS, &_EIGVECS, 0, CV_SVD_MODIFY_A + CV_SVD_U_T );
851 cvEigenVV( &_S, &_EIGVECS, &_EIGVALS, 0 );
853 for( i = 0; i < 3; i++ )
854 if( eigenvalues[i] > 0 )
857 if( i >= 3 /*eigenvalues[0] < DBL_EPSILON*/ )
859 box->center.x = box->center.y =
860 box->size.width = box->size.height =
865 // now find truthful eigenvector
866 _EIGVECS = cvMat( 6, 1, CV_64F, eigenvectors + 6*i );
867 _T = cvMat( 6, 1, CV_64F, T );
869 cvMatMul( &_C, &_EIGVECS, &_T );
871 // extract vector components
872 a = T[0]; b = T[1]; c = T[2]; d = T[3]; e = T[4]; f = T[5];
874 ///////////////// extract ellipse axes from above values ////////////////
877 1) find center of ellipse
879 | a b/2 | * | x0 | + | d/2 | = |0 |
880 | b/2 c | | y0 | | e/2 | |0 |
883 idet = a * c - b * b * 0.25;
884 idet = idet > DBL_EPSILON ? 1./idet : 0;
886 // we must normalize (a b c d e f ) to fit (4ac-b^2=1)
887 scale = sqrt( 0.25 * idet );
889 if( scale < DBL_EPSILON )
891 box->center.x = (float)offx;
892 box->center.y = (float)offy;
893 box->size.width = box->size.height = box->angle = 0.f;
904 x0 = (-d * c + e * b * 0.5) * 2.;
905 y0 = (-a * e + d * b * 0.5) * 2.;
908 box->center.x = (float)(x0 + offx);
909 box->center.y = (float)(y0 + offy);
911 // offset ellipse to (x0,y0)
913 f += a * x0 * x0 + b * x0 * y0 + c * y0 * y0 + d * x0 + e * y0;
915 if( fabs(f) < DBL_EPSILON )
917 box->size.width = box->size.height = box->angle = 0.f;
922 // normalize to f = 1
927 // extract axis of ellipse
928 // one more eigenvalue operation
930 S[1] = S[2] = b * 0.5;
933 _S = cvMat( 2, 2, CV_64F, S );
934 _EIGVECS = cvMat( 2, 2, CV_64F, eigenvectors );
935 _EIGVALS = cvMat( 1, 2, CV_64F, eigenvalues );
936 cvSVD( &_S, &_EIGVALS, &_EIGVECS, 0, CV_SVD_MODIFY_A + CV_SVD_U_T );
938 // exteract axis length from eigenvectors
939 box->size.width = (float)(2./sqrt(eigenvalues[0]));
940 box->size.height = (float)(2./sqrt(eigenvalues[1]));
943 box->angle = (float)(180 - atan2(eigenvectors[2], eigenvectors[3])*180/CV_PI);
948 cvFitEllipse2( const CvArr* array )
951 cv::AutoBuffer<double> Ad, bd;
952 memset( &box, 0, sizeof(box));
954 CvContour contour_header;
959 if( CV_IS_SEQ( array ))
961 ptseq = (CvSeq*)array;
962 if( !CV_IS_SEQ_POINT_SET( ptseq ))
963 CV_Error( CV_StsBadArg, "Unsupported sequence type" );
967 ptseq = cvPointSeqFromMat(CV_SEQ_KIND_GENERIC, array, &contour_header, &block);
972 CV_Error( CV_StsBadSize, "Number of points should be >= 6" );
974 icvFitEllipse_F( ptseq, &box );
977 * New fitellipse algorithm, contributed by Dr. Daniel Weiss
979 double gfp[5], rp[5], t;
981 const double min_eps = 1e-6;
988 // first fit for parameters A - E
989 A = cvMat( n, 5, CV_64F, Ad );
990 b = cvMat( n, 1, CV_64F, bd );
991 x = cvMat( 5, 1, CV_64F, gfp );
993 cvStartReadSeq( ptseq, &reader );
994 is_float = CV_SEQ_ELTYPE(ptseq) == CV_32FC2;
996 for( i = 0; i < n; i++ )
1000 p = *(CvPoint2D32f*)(reader.ptr);
1003 p.x = (float)((int*)reader.ptr)[0];
1004 p.y = (float)((int*)reader.ptr)[1];
1006 CV_NEXT_SEQ_ELEM( sizeof(p), reader );
1008 bd[i] = 10000.0; // 1.0?
1009 Ad[i*5] = -(double)p.x * p.x; // A - C signs inverted as proposed by APP
1010 Ad[i*5 + 1] = -(double)p.y * p.y;
1011 Ad[i*5 + 2] = -(double)p.x * p.y;
1016 cvSolve( &A, &b, &x, CV_SVD );
1018 // now use general-form parameters A - E to find the ellipse center:
1019 // differentiate general form wrt x/y to get two equations for cx and cy
1020 A = cvMat( 2, 2, CV_64F, Ad );
1021 b = cvMat( 2, 1, CV_64F, bd );
1022 x = cvMat( 2, 1, CV_64F, rp );
1024 Ad[1] = Ad[2] = gfp[2];
1028 cvSolve( &A, &b, &x, CV_SVD );
1030 // re-fit for parameters A - C with those center coordinates
1031 A = cvMat( n, 3, CV_64F, Ad );
1032 b = cvMat( n, 1, CV_64F, bd );
1033 x = cvMat( 3, 1, CV_64F, gfp );
1034 for( i = 0; i < n; i++ )
1038 p = *(CvPoint2D32f*)(reader.ptr);
1041 p.x = (float)((int*)reader.ptr)[0];
1042 p.y = (float)((int*)reader.ptr)[1];
1044 CV_NEXT_SEQ_ELEM( sizeof(p), reader );
1046 Ad[i * 3] = (p.x - rp[0]) * (p.x - rp[0]);
1047 Ad[i * 3 + 1] = (p.y - rp[1]) * (p.y - rp[1]);
1048 Ad[i * 3 + 2] = (p.x - rp[0]) * (p.y - rp[1]);
1050 cvSolve(&A, &b, &x, CV_SVD);
1052 // store angle and radii
1053 rp[4] = -0.5 * atan2(gfp[2], gfp[1] - gfp[0]); // convert from APP angle usage
1054 t = sin(-2.0 * rp[4]);
1055 if( fabs(t) > fabs(gfp[2])*min_eps )
1058 t = gfp[1] - gfp[0];
1059 rp[2] = fabs(gfp[0] + gfp[1] - t);
1060 if( rp[2] > min_eps )
1061 rp[2] = sqrt(2.0 / rp[2]);
1062 rp[3] = fabs(gfp[0] + gfp[1] + t);
1063 if( rp[3] > min_eps )
1064 rp[3] = sqrt(2.0 / rp[3]);
1066 box.center.x = (float)rp[0];
1067 box.center.y = (float)rp[1];
1068 box.size.width = (float)(rp[2]*2);
1069 box.size.height = (float)(rp[3]*2);
1070 if( box.size.width > box.size.height )
1073 CV_SWAP( box.size.width, box.size.height, tmp );
1074 box.angle = (float)(90 + rp[4]*180/CV_PI);
1076 if( box.angle < -180 )
1078 if( box.angle > 360 )
1086 /* Calculates bounding rectagnle of a point set or retrieves already calculated */
1088 cvBoundingRect( CvArr* array, int update )
1091 CvRect rect = { 0, 0, 0, 0 };
1092 CvContour contour_header;
1096 CvMat stub, *mat = 0;
1097 int xmin = 0, ymin = 0, xmax = -1, ymax = -1, i, j, k;
1098 int calculate = update;
1100 if( CV_IS_SEQ( array ))
1102 ptseq = (CvSeq*)array;
1103 if( !CV_IS_SEQ_POINT_SET( ptseq ))
1104 CV_Error( CV_StsBadArg, "Unsupported sequence type" );
1106 if( ptseq->header_size < (int)sizeof(CvContour))
1109 CV_Error( CV_StsBadArg, "The header is too small to fit the rectangle, "
1110 "so it could not be updated" );*/
1117 mat = cvGetMat( array, &stub );
1118 if( CV_MAT_TYPE(mat->type) == CV_32SC2 ||
1119 CV_MAT_TYPE(mat->type) == CV_32FC2 )
1121 ptseq = cvPointSeqFromMat(CV_SEQ_KIND_GENERIC, mat, &contour_header, &block);
1124 else if( CV_MAT_TYPE(mat->type) != CV_8UC1 &&
1125 CV_MAT_TYPE(mat->type) != CV_8SC1 )
1126 CV_Error( CV_StsUnsupportedFormat,
1127 "The image/matrix format is not supported by the function" );
1133 return ((CvContour*)ptseq)->rect;
1137 CvSize size = cvGetMatSize(mat);
1141 for( i = 0; i < size.height; i++ )
1143 uchar* _ptr = mat->data.ptr + i*mat->step;
1144 uchar* ptr = (uchar*)cvAlignPtr(_ptr, 4);
1145 int have_nz = 0, k_min, offset = (int)(ptr - _ptr);
1147 offset = MIN(offset, size.width);
1148 for( ; j < offset; j++ )
1161 if( offset < size.width )
1165 size.width -= offset;
1167 for( ; j <= xmin - 4; j += 4 )
1168 if( *((int*)(ptr+j)) )
1170 for( ; j < xmin; j++ )
1179 k_min = MAX(j-1, xmax);
1181 for( ; k > k_min && (k&3) != 3; k-- )
1184 if( k > k_min && (k&3) == 3 )
1186 for( ; k > k_min+3; k -= 4 )
1187 if( *((int*)(ptr+k-3)) )
1190 for( ; k > k_min; k-- )
1200 for( ; j <= k - 3; j += 4 )
1201 if( *((int*)(ptr+j)) )
1203 for( ; j <= k; j++ )
1212 size.width += offset;
1222 if( xmin >= size.width )
1225 else if( ptseq->total )
1227 int is_float = CV_SEQ_ELTYPE(ptseq) == CV_32FC2;
1228 cvStartReadSeq( ptseq, &reader, 0 );
1234 CV_READ_SEQ_ELEM( pt, reader );
1238 for( i = 1; i < ptseq->total; i++ )
1240 CV_READ_SEQ_ELEM( pt, reader );
1260 CV_READ_SEQ_ELEM( pt, reader );
1261 xmin = xmax = CV_TOGGLE_FLT(pt.x);
1262 ymin = ymax = CV_TOGGLE_FLT(pt.y);
1264 for( i = 1; i < ptseq->total; i++ )
1266 CV_READ_SEQ_ELEM( pt, reader );
1267 pt.x = CV_TOGGLE_FLT(pt.x);
1268 pt.y = CV_TOGGLE_FLT(pt.y);
1283 v.i = CV_TOGGLE_FLT(xmin); xmin = cvFloor(v.f);
1284 v.i = CV_TOGGLE_FLT(ymin); ymin = cvFloor(v.f);
1285 /* because right and bottom sides of
1286 the bounding rectangle are not inclusive
1287 (note +1 in width and height calculation below),
1288 cvFloor is used here instead of cvCeil */
1289 v.i = CV_TOGGLE_FLT(xmax); xmax = cvFloor(v.f);
1290 v.i = CV_TOGGLE_FLT(ymax); ymax = cvFloor(v.f);
1296 rect.width = xmax - xmin + 1;
1297 rect.height = ymax - ymin + 1;
1300 ((CvContour*)ptseq)->rect = rect;