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.
41 #include "precomp.hpp"
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 );
111 // Bugfix by Axel at rubico.com 2010-03-22, affects closed slices only
112 // wraparound not handled by CV_NEXT_SEQ_ELEM
113 if( is_closed && i == count - 2 )
114 cvSetSeqReaderPos( &reader, slice.start_index );
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];
132 icvFindCircle( CvPoint2D32f pt0, CvPoint2D32f pt1,
133 CvPoint2D32f pt2, CvPoint2D32f * center, float *radius )
135 double x1 = (pt0.x + pt1.x) * 0.5;
136 double dy1 = pt0.x - pt1.x;
137 double x2 = (pt1.x + pt2.x) * 0.5;
138 double dy2 = pt1.x - pt2.x;
139 double y1 = (pt0.y + pt1.y) * 0.5;
140 double dx1 = pt1.y - pt0.y;
141 double y2 = (pt1.y + pt2.y) * 0.5;
142 double dx2 = pt2.y - pt1.y;
145 CvStatus result = CV_OK;
147 if( icvIntersectLines( x1, dx1, y1, dy1, x2, dx2, y2, dy2, &t ) >= 0 )
149 center->x = (float) (x2 + dx2 * t);
150 center->y = (float) (y2 + dy2 * t);
151 *radius = (float) icvDistanceL2_32f( *center, pt0 );
155 center->x = center->y = 0.f;
157 result = CV_NOTDEFINED_ERR;
164 CV_INLINE double icvIsPtInCircle( CvPoint2D32f pt, CvPoint2D32f center, float radius )
166 double dx = pt.x - center.x;
167 double dy = pt.y - center.y;
168 return (double)radius*radius - dx*dx - dy*dy;
173 icvFindEnslosingCicle4pts_32f( CvPoint2D32f * pts, CvPoint2D32f * _center, float *_radius )
175 int shuffles[4][4] = { {0, 1, 2, 3}, {0, 1, 3, 2}, {2, 3, 0, 1}, {2, 3, 1, 0} };
177 int idxs[4] = { 0, 1, 2, 3 };
178 int i, j, k = 1, mi = 0;
181 CvPoint2D32f min_center;
182 float radius, min_radius = FLT_MAX;
183 CvPoint2D32f res_pts[4];
185 center = min_center = pts[0];
188 for( i = 0; i < 4; i++ )
189 for( j = i + 1; j < 4; j++ )
191 float dist = icvDistanceL2_32f( pts[i], pts[j] );
193 if( max_dist < dist )
205 for( i = 0; i < 4; i++ )
207 for( j = 0; j < k; j++ )
214 center = cvPoint2D32f( (pts[idxs[0]].x + pts[idxs[1]].x)*0.5f,
215 (pts[idxs[0]].y + pts[idxs[1]].y)*0.5f );
216 radius = (float)(icvDistanceL2_32f( pts[idxs[0]], center )*1.03);
220 if( icvIsPtInCircle( pts[idxs[2]], center, radius ) >= 0 &&
221 icvIsPtInCircle( pts[idxs[3]], center, radius ) >= 0 )
228 for( i = 0; i < 4; i++ )
230 if( icvFindCircle( pts[shuffles[i][0]], pts[shuffles[i][1]],
231 pts[shuffles[i][2]], ¢er, &radius ) >= 0 )
237 if( icvIsPtInCircle( pts[shuffles[i][3]], center, radius ) >= 0 &&
238 min_radius > radius )
252 for( i = 0; i < 4; i++ )
253 idxs[i] = shuffles[mi][i];
261 /* reorder output points */
262 for( i = 0; i < 4; i++ )
263 res_pts[i] = pts[idxs[i]];
265 for( i = 0; i < 4; i++ )
268 assert( icvIsPtInCircle( pts[i], center, radius ) >= 0 );
276 cvMinEnclosingCircle( const void* array, CvPoint2D32f * _center, float *_radius )
278 const int max_iters = 100;
279 const float eps = FLT_EPSILON*2;
280 CvPoint2D32f center = { 0, 0 };
285 _center->x = _center->y = 0.f;
292 CvContour contour_header;
297 if( !_center || !_radius )
298 CV_Error( CV_StsNullPtr, "Null center or radius pointers" );
300 if( CV_IS_SEQ(array) )
302 sequence = (CvSeq*)array;
303 if( !CV_IS_SEQ_POINT_SET( sequence ))
304 CV_Error( CV_StsBadArg, "The passed sequence is not a valid contour" );
308 sequence = cvPointSeqFromMat(
309 CV_SEQ_KIND_GENERIC, array, &contour_header, &block );
312 if( sequence->total <= 0 )
313 CV_Error( CV_StsBadSize, "" );
315 cvStartReadSeq( sequence, &reader, 0 );
317 count = sequence->total;
318 is_float = CV_SEQ_ELTYPE(sequence) == CV_32FC2;
322 CvPoint *pt_left, *pt_right, *pt_top, *pt_bottom;
324 pt_left = pt_right = pt_top = pt_bottom = (CvPoint *)(reader.ptr);
325 CV_READ_SEQ_ELEM( pt, reader );
327 for(int i = 1; i < count; i++ )
329 CvPoint* pt_ptr = (CvPoint*)reader.ptr;
330 CV_READ_SEQ_ELEM( pt, reader );
332 if( pt.x < pt_left->x )
334 if( pt.x > pt_right->x )
336 if( pt.y < pt_top->y )
338 if( pt.y > pt_bottom->y )
342 pts[0] = cvPointTo32f( *pt_left );
343 pts[1] = cvPointTo32f( *pt_right );
344 pts[2] = cvPointTo32f( *pt_top );
345 pts[3] = cvPointTo32f( *pt_bottom );
349 CvPoint2D32f *pt_left, *pt_right, *pt_top, *pt_bottom;
351 pt_left = pt_right = pt_top = pt_bottom = (CvPoint2D32f *) (reader.ptr);
352 CV_READ_SEQ_ELEM( pt, reader );
354 for(int i = 1; i < count; i++ )
356 CvPoint2D32f* pt_ptr = (CvPoint2D32f*)reader.ptr;
357 CV_READ_SEQ_ELEM( pt, reader );
359 if( pt.x < pt_left->x )
361 if( pt.x > pt_right->x )
363 if( pt.y < pt_top->y )
365 if( pt.y > pt_bottom->y )
375 for( k = 0; k < max_iters; k++ )
377 double min_delta = 0, delta;
378 CvPoint2D32f ptfl, farAway = { 0, 0};
379 /*only for first iteration because the alg is repared at the loop's foot*/
381 icvFindEnslosingCicle4pts_32f( pts, ¢er, &radius );
383 cvStartReadSeq( sequence, &reader, 0 );
385 for(int i = 0; i < count; i++ )
389 ptfl.x = (float)((CvPoint*)reader.ptr)->x;
390 ptfl.y = (float)((CvPoint*)reader.ptr)->y;
394 ptfl = *(CvPoint2D32f*)reader.ptr;
396 CV_NEXT_SEQ_ELEM( sequence->elem_size, reader );
398 delta = icvIsPtInCircle( ptfl, center, radius );
399 if( delta < min_delta )
405 result = min_delta >= 0;
409 CvPoint2D32f ptsCopy[4];
410 /* find good replacement partner for the point which is at most far away,
411 starting with the one that lays in the actual circle (i=3) */
412 for(int i = 3; i >=0; i-- )
414 for(int j = 0; j < 4; j++ )
416 ptsCopy[j]=(i != j)? pts[j]: farAway;
419 icvFindEnslosingCicle4pts_32f(ptsCopy, ¢er, &radius );
420 if( icvIsPtInCircle( pts[i], center, radius )>=0){ // replaced one again in the new circle?
429 cvStartReadSeq( sequence, &reader, 0 );
432 for(int i = 0; i < count; i++ )
439 ptfl.x = (float)((CvPoint*)reader.ptr)->x;
440 ptfl.y = (float)((CvPoint*)reader.ptr)->y;
444 ptfl = *(CvPoint2D32f*)reader.ptr;
447 CV_NEXT_SEQ_ELEM( sequence->elem_size, reader );
448 dx = center.x - ptfl.x;
449 dy = center.y - ptfl.y;
451 radius = MAX(radius,t);
454 radius = (float)(sqrt(radius)*(1 + eps));
465 /* area of a whole sequence */
467 icvContourArea( const CvSeq* contour, double *area )
472 int lpt = contour->total;
473 double a00 = 0, xi_1, yi_1;
474 int is_float = CV_SEQ_ELTYPE(contour) == CV_32FC2;
476 cvStartReadSeq( contour, &reader, 0 );
480 xi_1 = ((CvPoint*)(reader.ptr))->x;
481 yi_1 = ((CvPoint*)(reader.ptr))->y;
485 xi_1 = ((CvPoint2D32f*)(reader.ptr))->x;
486 yi_1 = ((CvPoint2D32f*)(reader.ptr))->y;
488 CV_NEXT_SEQ_ELEM( contour->elem_size, reader );
496 xi = ((CvPoint*)(reader.ptr))->x;
497 yi = ((CvPoint*)(reader.ptr))->y;
501 xi = ((CvPoint2D32f*)(reader.ptr))->x;
502 yi = ((CvPoint2D32f*)(reader.ptr))->y;
504 CV_NEXT_SEQ_ELEM( contour->elem_size, reader );
506 dxy = xi_1 * yi - xi * yi_1;
521 /****************************************************************************************\
523 copy data from one buffer to other buffer
525 \****************************************************************************************/
528 icvMemCopy( double **buf1, double **buf2, double **buf3, int *b_max )
532 if( (*buf1 == NULL && *buf2 == NULL) || *buf3 == NULL )
533 return CV_NULLPTR_ERR;
538 *b_max = 2 * (*b_max);
539 *buf2 = (double *)cvAlloc( (*b_max) * sizeof( double ));
542 return CV_OUTOFMEM_ERR;
544 memcpy( *buf2, *buf3, bb * sizeof( double ));
552 *b_max = 2 * (*b_max);
553 *buf1 = (double *) cvAlloc( (*b_max) * sizeof( double ));
556 return CV_OUTOFMEM_ERR;
558 memcpy( *buf1, *buf3, bb * sizeof( double ));
568 /* area of a contour sector */
569 static CvStatus icvContourSecArea( CvSeq * contour, CvSlice slice, double *area )
571 CvPoint pt; /* pointer to points */
572 CvPoint pt_s, pt_e; /* first and last points */
573 CvSeqReader reader; /* points reader of contour */
575 int p_max = 2, p_ind;
577 double a00; /* unnormalized moments m00 */
578 double xi, yi, xi_1, yi_1, x0, y0, dxy, sk, sk1, t;
579 double x_s, y_s, nx, ny, dx, dy, du, dv;
581 double *p_are1, *p_are2, *p_are;
583 assert( contour != NULL );
585 if( contour == NULL )
586 return CV_NULLPTR_ERR;
588 if( !CV_IS_SEQ_POINT_SET( contour ))
589 return CV_BADFLAG_ERR;
591 lpt = cvSliceLength( slice, contour );
595 lpt = contour->total - n1 + n2 + 1;*/
597 if( contour->total && lpt > 2 )
599 a00 = x0 = y0 = xi_1 = yi_1 = 0;
603 p_are1 = (double *) cvAlloc( p_max * sizeof( double ));
606 return CV_OUTOFMEM_ERR;
611 cvStartReadSeq( contour, &reader, 0 );
612 cvSetSeqReaderPos( &reader, slice.start_index );
613 CV_READ_SEQ_ELEM( pt_s, reader );
615 cvSetSeqReaderPos( &reader, slice.end_index );
616 CV_READ_SEQ_ELEM( pt_e, reader );
618 /* normal coefficients */
619 nx = pt_s.y - pt_e.y;
620 ny = pt_e.x - pt_s.x;
621 cvSetSeqReaderPos( &reader, slice.start_index );
625 CV_READ_SEQ_ELEM( pt, reader );
629 xi_1 = (double) pt.x;
630 yi_1 = (double) pt.y;
641 /**************** edges intersection examination **************************/
642 sk = nx * (xi - pt_s.x) + ny * (yi - pt_s.y);
643 if( (fabs( sk ) < eps && lpt > 0) || sk * sk1 < -eps )
645 if( fabs( sk ) < eps )
647 dxy = xi_1 * yi - xi * yi_1;
649 dxy = xi * y0 - x0 * yi;
653 icvMemCopy( &p_are1, &p_are2, &p_are, &p_max );
655 p_are[p_ind] = a00 / 2.;
665 /* define intersection point */
670 if( fabs( du ) > eps )
671 t = ((yi_1 - pt_s.y) * du + dv * (pt_s.x - xi_1)) /
674 t = (xi_1 - pt_s.x) / dx;
675 if( t > eps && t < 1 - eps )
677 x_s = pt_s.x + t * dx;
678 y_s = pt_s.y + t * dy;
679 dxy = xi_1 * y_s - x_s * yi_1;
681 dxy = x_s * y0 - x0 * y_s;
684 icvMemCopy( &p_are1, &p_are2, &p_are, &p_max );
686 p_are[p_ind] = a00 / 2.;
693 dxy = x_s * yi - xi * y_s;
698 dxy = xi_1 * yi - xi * yi_1;
710 dxy = xi_1 * yi - xi * yi_1;
715 icvMemCopy( &p_are1, &p_are2, &p_are, &p_max );
717 p_are[p_ind] = a00 / 2.;
720 /* common area calculation */
722 for( i = 0; i < p_ind; i++ )
723 (*area) += fabs( p_are[i] );
727 else if( p_are2 != NULL )
733 return CV_BADSIZE_ERR;
737 /* external contour area function */
739 cvContourArea( const void *array, CvSlice slice, int oriented )
743 CvContour contour_header;
747 if( CV_IS_SEQ( array ))
749 contour = (CvSeq*)array;
750 if( !CV_IS_SEQ_POLYLINE( contour ))
751 CV_Error( CV_StsBadArg, "Unsupported sequence type" );
755 contour = cvPointSeqFromMat( CV_SEQ_KIND_CURVE, array, &contour_header, &block );
758 if( cvSliceLength( slice, contour ) == contour->total )
760 IPPI_CALL( icvContourArea( contour, &area ));
764 if( CV_SEQ_ELTYPE( contour ) != CV_32SC2 )
765 CV_Error( CV_StsUnsupportedFormat,
766 "Only curves with integer coordinates are supported in case of contour slice" );
767 IPPI_CALL( icvContourSecArea( contour, slice, &area ));
770 return oriented ? area : fabs(area);
775 cvFitEllipse2( const CvArr* array )
778 cv::AutoBuffer<double> Ad, bd;
779 memset( &box, 0, sizeof(box));
781 CvContour contour_header;
786 if( CV_IS_SEQ( array ))
788 ptseq = (CvSeq*)array;
789 if( !CV_IS_SEQ_POINT_SET( ptseq ))
790 CV_Error( CV_StsBadArg, "Unsupported sequence type" );
794 ptseq = cvPointSeqFromMat(CV_SEQ_KIND_GENERIC, array, &contour_header, &block);
799 CV_Error( CV_StsBadSize, "Number of points should be >= 5" );
802 * New fitellipse algorithm, contributed by Dr. Daniel Weiss
804 CvPoint2D32f c = {0,0};
805 double gfp[5], rp[5], t;
807 const double min_eps = 1e-6;
814 // first fit for parameters A - E
815 A = cvMat( n, 5, CV_64F, Ad );
816 b = cvMat( n, 1, CV_64F, bd );
817 x = cvMat( 5, 1, CV_64F, gfp );
819 cvStartReadSeq( ptseq, &reader );
820 is_float = CV_SEQ_ELTYPE(ptseq) == CV_32FC2;
822 for( i = 0; i < n; i++ )
826 p = *(CvPoint2D32f*)(reader.ptr);
829 p.x = (float)((int*)reader.ptr)[0];
830 p.y = (float)((int*)reader.ptr)[1];
832 CV_NEXT_SEQ_ELEM( sizeof(p), reader );
839 for( i = 0; i < n; i++ )
843 p = *(CvPoint2D32f*)(reader.ptr);
846 p.x = (float)((int*)reader.ptr)[0];
847 p.y = (float)((int*)reader.ptr)[1];
849 CV_NEXT_SEQ_ELEM( sizeof(p), reader );
853 bd[i] = 10000.0; // 1.0?
854 Ad[i*5] = -(double)p.x * p.x; // A - C signs inverted as proposed by APP
855 Ad[i*5 + 1] = -(double)p.y * p.y;
856 Ad[i*5 + 2] = -(double)p.x * p.y;
861 cvSolve( &A, &b, &x, CV_SVD );
863 // now use general-form parameters A - E to find the ellipse center:
864 // differentiate general form wrt x/y to get two equations for cx and cy
865 A = cvMat( 2, 2, CV_64F, Ad );
866 b = cvMat( 2, 1, CV_64F, bd );
867 x = cvMat( 2, 1, CV_64F, rp );
869 Ad[1] = Ad[2] = gfp[2];
873 cvSolve( &A, &b, &x, CV_SVD );
875 // re-fit for parameters A - C with those center coordinates
876 A = cvMat( n, 3, CV_64F, Ad );
877 b = cvMat( n, 1, CV_64F, bd );
878 x = cvMat( 3, 1, CV_64F, gfp );
879 for( i = 0; i < n; i++ )
883 p = *(CvPoint2D32f*)(reader.ptr);
886 p.x = (float)((int*)reader.ptr)[0];
887 p.y = (float)((int*)reader.ptr)[1];
889 CV_NEXT_SEQ_ELEM( sizeof(p), reader );
893 Ad[i * 3] = (p.x - rp[0]) * (p.x - rp[0]);
894 Ad[i * 3 + 1] = (p.y - rp[1]) * (p.y - rp[1]);
895 Ad[i * 3 + 2] = (p.x - rp[0]) * (p.y - rp[1]);
897 cvSolve(&A, &b, &x, CV_SVD);
899 // store angle and radii
900 rp[4] = -0.5 * atan2(gfp[2], gfp[1] - gfp[0]); // convert from APP angle usage
901 t = sin(-2.0 * rp[4]);
902 if( fabs(t) > fabs(gfp[2])*min_eps )
906 rp[2] = fabs(gfp[0] + gfp[1] - t);
907 if( rp[2] > min_eps )
908 rp[2] = sqrt(2.0 / rp[2]);
909 rp[3] = fabs(gfp[0] + gfp[1] + t);
910 if( rp[3] > min_eps )
911 rp[3] = sqrt(2.0 / rp[3]);
913 box.center.x = (float)rp[0] + c.x;
914 box.center.y = (float)rp[1] + c.y;
915 box.size.width = (float)(rp[2]*2);
916 box.size.height = (float)(rp[3]*2);
917 if( box.size.width > box.size.height )
920 CV_SWAP( box.size.width, box.size.height, tmp );
921 box.angle = (float)(90 + rp[4]*180/CV_PI);
923 if( box.angle < -180 )
925 if( box.angle > 360 )
932 /* Calculates bounding rectagnle of a point set or retrieves already calculated */
934 cvBoundingRect( CvArr* array, int update )
937 CvRect rect = { 0, 0, 0, 0 };
938 CvContour contour_header;
942 CvMat stub, *mat = 0;
943 int xmin = 0, ymin = 0, xmax = -1, ymax = -1, i, j, k;
944 int calculate = update;
946 if( CV_IS_SEQ( array ))
948 ptseq = (CvSeq*)array;
949 if( !CV_IS_SEQ_POINT_SET( ptseq ))
950 CV_Error( CV_StsBadArg, "Unsupported sequence type" );
952 if( ptseq->header_size < (int)sizeof(CvContour))
960 mat = cvGetMat( array, &stub );
961 if( CV_MAT_TYPE(mat->type) == CV_32SC2 ||
962 CV_MAT_TYPE(mat->type) == CV_32FC2 )
964 ptseq = cvPointSeqFromMat(CV_SEQ_KIND_GENERIC, mat, &contour_header, &block);
967 else if( CV_MAT_TYPE(mat->type) != CV_8UC1 &&
968 CV_MAT_TYPE(mat->type) != CV_8SC1 )
969 CV_Error( CV_StsUnsupportedFormat,
970 "The image/matrix format is not supported by the function" );
976 return ((CvContour*)ptseq)->rect;
980 CvSize size = cvGetMatSize(mat);
984 for( i = 0; i < size.height; i++ )
986 uchar* _ptr = mat->data.ptr + i*mat->step;
987 uchar* ptr = (uchar*)cvAlignPtr(_ptr, 4);
988 int have_nz = 0, k_min, offset = (int)(ptr - _ptr);
990 offset = MIN(offset, size.width);
991 for( ; j < offset; j++ )
1004 if( offset < size.width )
1008 size.width -= offset;
1010 for( ; j <= xmin - 4; j += 4 )
1011 if( *((int*)(ptr+j)) )
1013 for( ; j < xmin; j++ )
1022 k_min = MAX(j-1, xmax);
1024 for( ; k > k_min && (k&3) != 3; k-- )
1027 if( k > k_min && (k&3) == 3 )
1029 for( ; k > k_min+3; k -= 4 )
1030 if( *((int*)(ptr+k-3)) )
1033 for( ; k > k_min; k-- )
1043 for( ; j <= k - 3; j += 4 )
1044 if( *((int*)(ptr+j)) )
1046 for( ; j <= k; j++ )
1055 size.width += offset;
1065 if( xmin >= size.width )
1068 else if( ptseq->total )
1070 int is_float = CV_SEQ_ELTYPE(ptseq) == CV_32FC2;
1071 cvStartReadSeq( ptseq, &reader, 0 );
1073 CV_READ_SEQ_ELEM( pt, reader );
1075 if(cv::checkHardwareSupport(CV_CPU_SSE4_2))
1079 __m128i minval, maxval;
1080 minval = maxval = _mm_loadl_epi64((const __m128i*)(&pt)); //min[0]=pt.x, min[1]=pt.y
1082 for( i = 1; i < ptseq->total; i++)
1084 __m128i ptXY = _mm_loadl_epi64((const __m128i*)(reader.ptr));
1085 CV_NEXT_SEQ_ELEM(sizeof(pt), reader);
1086 minval = _mm_min_epi32(ptXY, minval);
1087 maxval = _mm_max_epi32(ptXY, maxval);
1089 xmin = _mm_cvtsi128_si32(minval);
1090 ymin = _mm_cvtsi128_si32(_mm_srli_si128(minval, 4));
1091 xmax = _mm_cvtsi128_si32(maxval);
1092 ymax = _mm_cvtsi128_si32(_mm_srli_si128(maxval, 4));
1096 __m128 minvalf, maxvalf, z = _mm_setzero_ps(), ptXY = _mm_setzero_ps();
1097 minvalf = maxvalf = _mm_loadl_pi(z, (const __m64*)(&pt));
1099 for( i = 1; i < ptseq->total; i++ )
1101 ptXY = _mm_loadl_pi(ptXY, (const __m64*)reader.ptr);
1102 CV_NEXT_SEQ_ELEM(sizeof(pt), reader);
1104 minvalf = _mm_min_ps(minvalf, ptXY);
1105 maxvalf = _mm_max_ps(maxvalf, ptXY);
1108 float xyminf[2], xymaxf[2];
1109 _mm_storel_pi((__m64*)xyminf, minvalf);
1110 _mm_storel_pi((__m64*)xymaxf, maxvalf);
1111 xmin = cvFloor(xyminf[0]);
1112 ymin = cvFloor(xyminf[1]);
1113 xmax = cvFloor(xymaxf[0]);
1114 ymax = cvFloor(xymaxf[1]);
1125 for( i = 1; i < ptseq->total; i++ )
1127 CV_READ_SEQ_ELEM( pt, reader );
1146 xmin = xmax = CV_TOGGLE_FLT(pt.x);
1147 ymin = ymax = CV_TOGGLE_FLT(pt.y);
1149 for( i = 1; i < ptseq->total; i++ )
1151 CV_READ_SEQ_ELEM( pt, reader );
1152 pt.x = CV_TOGGLE_FLT(pt.x);
1153 pt.y = CV_TOGGLE_FLT(pt.y);
1168 v.i = CV_TOGGLE_FLT(xmin); xmin = cvFloor(v.f);
1169 v.i = CV_TOGGLE_FLT(ymin); ymin = cvFloor(v.f);
1170 // because right and bottom sides of the bounding rectangle are not inclusive
1171 // (note +1 in width and height calculation below), cvFloor is used here instead of cvCeil
1172 v.i = CV_TOGGLE_FLT(xmax); xmax = cvFloor(v.f);
1173 v.i = CV_TOGGLE_FLT(ymax); ymax = cvFloor(v.f);
1178 rect.width = xmax - xmin + 1;
1179 rect.height = ymax - ymin + 1;
1182 ((CvContour*)ptseq)->rect = rect;