fixed warnings; restored fixed_size parameter in AutoBuffer

pull/317/head
Vadim Pisarevsky 12 years ago
parent dc4d0398f3
commit efd00238e2
  1. 13
      modules/core/include/opencv2/core/core.hpp
  2. 35
      modules/core/include/opencv2/core/operations.hpp
  3. 2
      modules/gpu/src/matrix_reductions.cpp
  4. 319
      modules/imgproc/src/convhull.cpp
  5. 4
      modules/imgproc/src/linefit.cpp
  6. 601
      modules/imgproc/src/rotcalipers.cpp
  7. 31
      modules/imgproc/src/shapedescr.cpp

@ -109,7 +109,7 @@ template<typename _Tp> class CV_EXPORTS MatIterator_;
template<typename _Tp> class CV_EXPORTS MatConstIterator_;
template<typename _Tp> class CV_EXPORTS MatCommaInitializer_;
template<typename _Tp> class CV_EXPORTS AutoBuffer;
template<typename _Tp, size_t fixed_size = 1024/sizeof(_Tp)+8> class CV_EXPORTS AutoBuffer;
CV_EXPORTS string format( const char* fmt, ... );
CV_EXPORTS string tempfile( const char* suffix CV_DEFAULT(0));
@ -3093,11 +3093,10 @@ public:
}
\endcode
*/
template<typename _Tp> class CV_EXPORTS AutoBuffer
template<typename _Tp, size_t fixed_size> class CV_EXPORTS AutoBuffer
{
public:
typedef _Tp value_type;
enum { fixed_size = 1024/sizeof(_Tp)+8, buffer_padding = (int)((16 + sizeof(_Tp) - 1)/sizeof(_Tp)) };
//! the default contructor
AutoBuffer();
@ -3105,9 +3104,9 @@ public:
AutoBuffer(size_t _size);
//! the copy constructor
AutoBuffer(const AutoBuffer<_Tp>& buf);
AutoBuffer(const AutoBuffer<_Tp, fixed_size>& buf);
//! the assignment operator
AutoBuffer<_Tp>& operator = (const AutoBuffer<_Tp>& buf);
AutoBuffer<_Tp, fixed_size>& operator = (const AutoBuffer<_Tp, fixed_size>& buf);
//! destructor. calls deallocate()
~AutoBuffer();
@ -3117,7 +3116,7 @@ public:
//! deallocates the buffer if it was dynamically allocated
void deallocate();
//! resizes the buffer and preserves the content
void resize(size_t _size);
void resize(size_t _size);
//! returns the current buffer size
size_t size() const;
//! returns pointer to the real buffer, stack-allocated or head-allocated
@ -3131,7 +3130,7 @@ protected:
//! size of the real buffer
size_t sz;
//! pre-allocated buffer
_Tp buf[fixed_size+buffer_padding];
_Tp buf[fixed_size];
};
/////////////////////////// multi-dimensional dense matrix //////////////////////////

@ -2534,21 +2534,23 @@ inline Point LineIterator::pos() const
/////////////////////////////// AutoBuffer ////////////////////////////////////////
template<typename _Tp> inline AutoBuffer<_Tp>::AutoBuffer()
template<typename _Tp, size_t fixed_size> inline
AutoBuffer<_Tp, fixed_size>::AutoBuffer()
{
ptr = buf;
sz = fixed_size;
}
template<typename _Tp> inline AutoBuffer<_Tp>::AutoBuffer(size_t _size)
template<typename _Tp, size_t fixed_size> inline
AutoBuffer<_Tp, fixed_size>::AutoBuffer(size_t _size)
{
ptr = buf;
sz = fixed_size;
allocate(_size);
}
template<typename _Tp>
inline AutoBuffer<_Tp>::AutoBuffer(const AutoBuffer<_Tp>& abuf )
template<typename _Tp, size_t fixed_size> inline
AutoBuffer<_Tp, fixed_size>::AutoBuffer(const AutoBuffer<_Tp, fixed_size>& abuf )
{
ptr = buf;
sz = fixed_size;
@ -2557,8 +2559,8 @@ inline AutoBuffer<_Tp>::AutoBuffer(const AutoBuffer<_Tp>& abuf )
ptr[i] = abuf.ptr[i];
}
template<typename _Tp>
inline AutoBuffer<_Tp>& AutoBuffer<_Tp>::operator = (const AutoBuffer<_Tp>& abuf )
template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>&
AutoBuffer<_Tp, fixed_size>::operator = (const AutoBuffer<_Tp, fixed_size>& abuf)
{
if( this != &abuf )
{
@ -2570,10 +2572,12 @@ inline AutoBuffer<_Tp>& AutoBuffer<_Tp>::operator = (const AutoBuffer<_Tp>& abuf
return *this;
}
template<typename _Tp> inline AutoBuffer<_Tp>::~AutoBuffer()
template<typename _Tp, size_t fixed_size> inline
AutoBuffer<_Tp, fixed_size>::~AutoBuffer()
{ deallocate(); }
template<typename _Tp> inline void AutoBuffer<_Tp>::allocate(size_t _size)
template<typename _Tp, size_t fixed_size> inline void
AutoBuffer<_Tp, fixed_size>::allocate(size_t _size)
{
if(_size <= sz)
{
@ -2588,7 +2592,8 @@ template<typename _Tp> inline void AutoBuffer<_Tp>::allocate(size_t _size)
}
}
template<typename _Tp> inline void AutoBuffer<_Tp>::deallocate()
template<typename _Tp, size_t fixed_size> inline void
AutoBuffer<_Tp, fixed_size>::deallocate()
{
if( ptr != buf )
{
@ -2598,7 +2603,8 @@ template<typename _Tp> inline void AutoBuffer<_Tp>::deallocate()
}
}
template<typename _Tp> inline void AutoBuffer<_Tp>::resize(size_t _size)
template<typename _Tp, size_t fixed_size> inline void
AutoBuffer<_Tp, fixed_size>::resize(size_t _size)
{
if(_size <= sz)
{
@ -2621,13 +2627,16 @@ template<typename _Tp> inline void AutoBuffer<_Tp>::resize(size_t _size)
delete[] prevptr;
}
template<typename _Tp> inline size_t AutoBuffer<_Tp>::size() const
template<typename _Tp, size_t fixed_size> inline size_t
AutoBuffer<_Tp, fixed_size>::size() const
{ return sz; }
template<typename _Tp> inline AutoBuffer<_Tp>::operator _Tp* ()
template<typename _Tp, size_t fixed_size> inline
AutoBuffer<_Tp, fixed_size>::operator _Tp* ()
{ return ptr; }
template<typename _Tp> inline AutoBuffer<_Tp>::operator const _Tp* () const
template<typename _Tp, size_t fixed_size> inline
AutoBuffer<_Tp, fixed_size>::operator const _Tp* () const
{ return ptr; }

@ -92,7 +92,7 @@ namespace
}
void download(double** hptrs)
{
AutoBuffer<double> hbuf(count);
AutoBuffer<double, 2 * sizeof(double)> hbuf(count);
cudaSafeCall( cudaMemcpy((void*)hbuf, pdev, count * sizeof(double), cudaMemcpyDeviceToHost) );
for (int i = 0; i < count; ++i)
*hptrs[i] = hbuf[i];

@ -42,325 +42,6 @@
#include "precomp.hpp"
#include <iostream>
#if 0
/* contour must be a simple polygon */
/* it must have more than 3 points */
CV_IMPL CvSeq* cvConvexityDefects( const CvArr* array,
const CvArr* hullarray,
CvMemStorage* storage )
{
CvSeq* defects = 0;
int i, index;
CvPoint* hull_cur;
/* is orientation of hull different from contour one */
int rev_orientation;
CvContour contour_header;
union { CvContour c; CvSeq s; } hull_header;
CvSeqBlock block, hullblock;
CvSeq *ptseq = (CvSeq*)array, *hull = (CvSeq*)hullarray;
CvSeqReader hull_reader;
CvSeqReader ptseq_reader;
CvSeqWriter writer;
int is_index;
if( CV_IS_SEQ( ptseq ))
{
if( !CV_IS_SEQ_POINT_SET( ptseq ))
CV_Error( CV_StsUnsupportedFormat,
"Input sequence is not a sequence of points" );
if( !storage )
storage = ptseq->storage;
}
else
{
ptseq = cvPointSeqFromMat( CV_SEQ_KIND_GENERIC, array, &contour_header, &block );
}
if( CV_SEQ_ELTYPE( ptseq ) != CV_32SC2 )
CV_Error( CV_StsUnsupportedFormat, "Floating-point coordinates are not supported here" );
if( CV_IS_SEQ( hull ))
{
int hulltype = CV_SEQ_ELTYPE( hull );
if( hulltype != CV_SEQ_ELTYPE_PPOINT && hulltype != CV_SEQ_ELTYPE_INDEX )
CV_Error( CV_StsUnsupportedFormat,
"Convex hull must represented as a sequence "
"of indices or sequence of pointers" );
if( !storage )
storage = hull->storage;
}
else
{
CvMat* mat = (CvMat*)hull;
if( !CV_IS_MAT( hull ))
CV_Error(CV_StsBadArg, "Convex hull is neither sequence nor matrix");
if( (mat->cols != 1 && mat->rows != 1) ||
!CV_IS_MAT_CONT(mat->type) || CV_MAT_TYPE(mat->type) != CV_32SC1 )
CV_Error( CV_StsBadArg,
"The matrix should be 1-dimensional and continuous array of int's" );
if( mat->cols + mat->rows - 1 > ptseq->total )
CV_Error( CV_StsBadSize, "Convex hull is larger than the point sequence" );
hull = cvMakeSeqHeaderForArray(
CV_SEQ_KIND_CURVE|CV_MAT_TYPE(mat->type)|CV_SEQ_FLAG_CLOSED,
sizeof(CvContour), CV_ELEM_SIZE(mat->type), mat->data.ptr,
mat->cols + mat->rows - 1, &hull_header.s, &hullblock );
}
is_index = CV_SEQ_ELTYPE(hull) == CV_SEQ_ELTYPE_INDEX;
if( !storage )
CV_Error( CV_StsNullPtr, "NULL storage pointer" );
defects = cvCreateSeq( CV_SEQ_KIND_GENERIC, sizeof(CvSeq), sizeof(CvConvexityDefect), storage );
if( ptseq->total < 4 || hull->total < 3)
{
//CV_ERROR( CV_StsBadSize,
// "point seq size must be >= 4, convex hull size must be >= 3" );
return defects;
}
/* recognize co-orientation of ptseq and its hull */
{
int sign = 0;
int index1, index2, index3;
if( !is_index )
{
CvPoint* pos = *CV_SEQ_ELEM( hull, CvPoint*, 0 );
index1 = cvSeqElemIdx( ptseq, pos );
pos = *CV_SEQ_ELEM( hull, CvPoint*, 1 );
index2 = cvSeqElemIdx( ptseq, pos );
pos = *CV_SEQ_ELEM( hull, CvPoint*, 2 );
index3 = cvSeqElemIdx( ptseq, pos );
}
else
{
index1 = *CV_SEQ_ELEM( hull, int, 0 );
index2 = *CV_SEQ_ELEM( hull, int, 1 );
index3 = *CV_SEQ_ELEM( hull, int, 2 );
}
sign += (index2 > index1) ? 1 : 0;
sign += (index3 > index2) ? 1 : 0;
sign += (index1 > index3) ? 1 : 0;
rev_orientation = (sign == 2) ? 0 : 1;
}
cvStartReadSeq( ptseq, &ptseq_reader, 0 );
cvStartReadSeq( hull, &hull_reader, rev_orientation );
if( !is_index )
{
hull_cur = *(CvPoint**)hull_reader.prev_elem;
index = cvSeqElemIdx( ptseq, (char*)hull_cur, 0 );
}
else
{
index = *(int*)hull_reader.prev_elem;
hull_cur = CV_GET_SEQ_ELEM( CvPoint, ptseq, index );
}
cvSetSeqReaderPos( &ptseq_reader, index );
cvStartAppendToSeq( defects, &writer );
/* cycle through ptseq and hull with computing defects */
for( i = 0; i < hull->total; i++ )
{
CvConvexityDefect defect;
int is_defect = 0;
double dx0, dy0;
double depth = 0, scale;
CvPoint* hull_next;
if( !is_index )
hull_next = *(CvPoint**)hull_reader.ptr;
else
{
int t = *(int*)hull_reader.ptr;
hull_next = CV_GET_SEQ_ELEM( CvPoint, ptseq, t );
}
dx0 = (double)hull_next->x - (double)hull_cur->x;
dy0 = (double)hull_next->y - (double)hull_cur->y;
assert( dx0 != 0 || dy0 != 0 );
scale = 1./sqrt(dx0*dx0 + dy0*dy0);
defect.start = hull_cur;
defect.end = hull_next;
for(;;)
{
/* go through ptseq to achieve next hull point */
CV_NEXT_SEQ_ELEM( sizeof(CvPoint), ptseq_reader );
if( ptseq_reader.ptr == (schar*)hull_next )
break;
else
{
CvPoint* cur = (CvPoint*)ptseq_reader.ptr;
/* compute distance from current point to hull edge */
double dx = (double)cur->x - (double)hull_cur->x;
double dy = (double)cur->y - (double)hull_cur->y;
/* compute depth */
double dist = fabs(-dy0*dx + dx0*dy) * scale;
if( dist > depth )
{
depth = dist;
defect.depth_point = cur;
defect.depth = (float)depth;
is_defect = 1;
}
}
}
if( is_defect )
{
CV_WRITE_SEQ_ELEM( defect, writer );
}
hull_cur = hull_next;
if( rev_orientation )
{
CV_PREV_SEQ_ELEM( hull->elem_size, hull_reader );
}
else
{
CV_NEXT_SEQ_ELEM( hull->elem_size, hull_reader );
}
}
return cvEndWriteSeq( &writer );
}
CV_IMPL int
cvCheckContourConvexity( const CvArr* array )
{
int flag = -1;
int i;
int orientation = 0;
CvSeqReader reader;
CvContour contour_header;
CvSeqBlock block;
CvSeq* contour = (CvSeq*)array;
if( CV_IS_SEQ(contour) )
{
if( !CV_IS_SEQ_POINT_SET(contour))
CV_Error( CV_StsUnsupportedFormat,
"Input sequence must be polygon (closed 2d curve)" );
}
else
{
contour = cvPointSeqFromMat(CV_SEQ_KIND_CURVE|CV_SEQ_FLAG_CLOSED, array, &contour_header, &block );
}
if( contour->total == 0 )
return -1;
cvStartReadSeq( contour, &reader, 0 );
flag = 1;
if( CV_SEQ_ELTYPE( contour ) == CV_32SC2 )
{
CvPoint *prev_pt = (CvPoint*)reader.prev_elem;
CvPoint *cur_pt = (CvPoint*)reader.ptr;
int dx0 = cur_pt->x - prev_pt->x;
int dy0 = cur_pt->y - prev_pt->y;
for( i = 0; i < contour->total; i++ )
{
int dxdy0, dydx0;
int dx, dy;
/*int orient; */
CV_NEXT_SEQ_ELEM( sizeof(CvPoint), reader );
prev_pt = cur_pt;
cur_pt = (CvPoint *) reader.ptr;
dx = cur_pt->x - prev_pt->x;
dy = cur_pt->y - prev_pt->y;
dxdy0 = dx * dy0;
dydx0 = dy * dx0;
/* find orientation */
/* orient = -dy0 * dx + dx0 * dy;
orientation |= (orient > 0) ? 1 : 2;
*/
orientation |= (dydx0 > dxdy0) ? 1 : ((dydx0 < dxdy0) ? 2 : 3);
if( orientation == 3 )
{
flag = 0;
break;
}
dx0 = dx;
dy0 = dy;
}
}
else
{
CV_Assert( CV_SEQ_ELTYPE(contour) == CV_32FC2 );
CvPoint2D32f *prev_pt = (CvPoint2D32f*)reader.prev_elem;
CvPoint2D32f *cur_pt = (CvPoint2D32f*)reader.ptr;
float dx0 = cur_pt->x - prev_pt->x;
float dy0 = cur_pt->y - prev_pt->y;
for( i = 0; i < contour->total; i++ )
{
float dxdy0, dydx0;
float dx, dy;
/*int orient; */
CV_NEXT_SEQ_ELEM( sizeof(CvPoint2D32f), reader );
prev_pt = cur_pt;
cur_pt = (CvPoint2D32f*) reader.ptr;
dx = cur_pt->x - prev_pt->x;
dy = cur_pt->y - prev_pt->y;
dxdy0 = dx * dy0;
dydx0 = dy * dx0;
/* find orientation */
/* orient = -dy0 * dx + dx0 * dy;
orientation |= (orient > 0) ? 1 : 2;
*/
orientation |= (dydx0 > dxdy0) ? 1 : ((dydx0 < dxdy0) ? 2 : 3);
if( orientation == 3 )
{
flag = 0;
break;
}
dx0 = dx;
dy0 = dy;
}
}
return flag;
}
#endif
namespace cv
{

@ -323,7 +323,7 @@ static void fitLine2D( const Point2f * points, int count, int dist,
float rdelta = reps != 0 ? reps : 1.0f;
float adelta = aeps != 0 ? aeps : 0.01f;
double min_err = DBL_MAX, err = 0;
RNG rng(-1);
RNG rng((uint64)-1);
memset( line, 0, 4*sizeof(line[0]) );
@ -463,7 +463,7 @@ static void fitLine3D( Point3f * points, int count, int dist,
float rdelta = reps != 0 ? reps : 1.0f;
float adelta = aeps != 0 ? aeps : 0.01f;
double min_err = DBL_MAX, err = 0;
RNG rng(-1);
RNG rng((uint64)-1);
switch (dist)
{

@ -1,342 +1,341 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of OpenCV Foundation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of OpenCV Foundation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
namespace cv
{
struct MinAreaState
{
int bottom;
int left;
float height;
float width;
float base_a;
float base_b;
};
enum { CALIPERS_MAXHEIGHT=0, CALIPERS_MINAREARECT=1, CALIPERS_MAXDIST=2 };
/*F///////////////////////////////////////////////////////////////////////////////////////
// Name: rotatingCalipers
// Purpose:
// Rotating calipers algorithm with some applications
//
// Context:
// Parameters:
// points - convex hull vertices ( any orientation )
// n - number of vertices
// mode - concrete application of algorithm
// can be CV_CALIPERS_MAXDIST or
// CV_CALIPERS_MINAREARECT
// left, bottom, right, top - indexes of extremal points
// out - output info.
// In case CV_CALIPERS_MAXDIST it points to float value -
// maximal height of polygon.
// In case CV_CALIPERS_MINAREARECT
// ((CvPoint2D32f*)out)[0] - corner
// ((CvPoint2D32f*)out)[1] - vector1
// ((CvPoint2D32f*)out)[0] - corner2
//
// ^
// |
// vector2 |
// |
// |____________\
// corner /
// vector1
//
// Returns:
// Notes:
//F*/
/* we will use usual cartesian coordinates */
static void rotatingCalipers( const Point2f* points, int n, int mode, float* out )
struct MinAreaState
{
int bottom;
int left;
float height;
float width;
float base_a;
float base_b;
};
enum { CALIPERS_MAXHEIGHT=0, CALIPERS_MINAREARECT=1, CALIPERS_MAXDIST=2 };
/*F///////////////////////////////////////////////////////////////////////////////////////
// Name: rotatingCalipers
// Purpose:
// Rotating calipers algorithm with some applications
//
// Context:
// Parameters:
// points - convex hull vertices ( any orientation )
// n - number of vertices
// mode - concrete application of algorithm
// can be CV_CALIPERS_MAXDIST or
// CV_CALIPERS_MINAREARECT
// left, bottom, right, top - indexes of extremal points
// out - output info.
// In case CV_CALIPERS_MAXDIST it points to float value -
// maximal height of polygon.
// In case CV_CALIPERS_MINAREARECT
// ((CvPoint2D32f*)out)[0] - corner
// ((CvPoint2D32f*)out)[1] - vector1
// ((CvPoint2D32f*)out)[0] - corner2
//
// ^
// |
// vector2 |
// |
// |____________\
// corner /
// vector1
//
// Returns:
// Notes:
//F*/
/* we will use usual cartesian coordinates */
static void rotatingCalipers( const Point2f* points, int n, int mode, float* out )
{
float minarea = FLT_MAX;
float max_dist = 0;
char buffer[32] = {};
int i, k;
AutoBuffer<float> abuf(n*3);
float* inv_vect_length = abuf;
Point2f* vect = (Point2f*)(inv_vect_length + n);
int left = 0, bottom = 0, right = 0, top = 0;
int seq[4] = { -1, -1, -1, -1 };
/* rotating calipers sides will always have coordinates
(a,b) (-b,a) (-a,-b) (b, -a)
*/
/* this is a first base bector (a,b) initialized by (1,0) */
float orientation = 0;
float base_a;
float base_b = 0;
float left_x, right_x, top_y, bottom_y;
Point2f pt0 = points[0];
left_x = right_x = pt0.x;
top_y = bottom_y = pt0.y;
for( i = 0; i < n; i++ )
{
float minarea = FLT_MAX;
float max_dist = 0;
char buffer[32] = {};
int i, k;
AutoBuffer<float> buf(n*3);
float* inv_vect_length = buf;
Point2f* vect = (Point2f*)(inv_vect_length + n);
int left = 0, bottom = 0, right = 0, top = 0;
int seq[4] = { -1, -1, -1, -1 };
/* rotating calipers sides will always have coordinates
(a,b) (-b,a) (-a,-b) (b, -a)
*/
/* this is a first base bector (a,b) initialized by (1,0) */
float orientation = 0;
float base_a;
float base_b = 0;
float left_x, right_x, top_y, bottom_y;
Point2f pt0 = points[0];
left_x = right_x = pt0.x;
top_y = bottom_y = pt0.y;
double dx, dy;
for( i = 0; i < n; i++ )
{
double dx, dy;
if( pt0.x < left_x )
left_x = pt0.x, left = i;
if( pt0.x < left_x )
left_x = pt0.x, left = i;
if( pt0.x > right_x )
right_x = pt0.x, right = i;
if( pt0.x > right_x )
right_x = pt0.x, right = i;
if( pt0.y > top_y )
top_y = pt0.y, top = i;
if( pt0.y > top_y )
top_y = pt0.y, top = i;
if( pt0.y < bottom_y )
bottom_y = pt0.y, bottom = i;
if( pt0.y < bottom_y )
bottom_y = pt0.y, bottom = i;
Point2f pt = points[(i+1) & (i+1 < n ? -1 : 0)];
Point2f pt = points[(i+1) & (i+1 < n ? -1 : 0)];
dx = pt.x - pt0.x;
dy = pt.y - pt0.y;
dx = pt.x - pt0.x;
dy = pt.y - pt0.y;
vect[i].x = (float)dx;
vect[i].y = (float)dy;
inv_vect_length[i] = (float)(1./sqrt(dx*dx + dy*dy));
vect[i].x = (float)dx;
vect[i].y = (float)dy;
inv_vect_length[i] = (float)(1./sqrt(dx*dx + dy*dy));
pt0 = pt;
}
pt0 = pt;
}
// find convex hull orientation
{
double ax = vect[n-1].x;
double ay = vect[n-1].y;
// find convex hull orientation
for( i = 0; i < n; i++ )
{
double ax = vect[n-1].x;
double ay = vect[n-1].y;
double bx = vect[i].x;
double by = vect[i].y;
double convexity = ax * by - ay * bx;
for( i = 0; i < n; i++ )
if( convexity != 0 )
{
double bx = vect[i].x;
double by = vect[i].y;
double convexity = ax * by - ay * bx;
if( convexity != 0 )
{
orientation = (convexity > 0) ? 1.f : (-1.f);
break;
}
ax = bx;
ay = by;
orientation = (convexity > 0) ? 1.f : (-1.f);
break;
}
CV_Assert( orientation != 0 );
ax = bx;
ay = by;
}
base_a = orientation;
/*****************************************************************************************/
/* init calipers position */
seq[0] = bottom;
seq[1] = right;
seq[2] = top;
seq[3] = left;
/*****************************************************************************************/
/* Main loop - evaluate angles and rotate calipers */
/* all of edges will be checked while rotating calipers by 90 degrees */
for( k = 0; k < n; k++ )
CV_Assert( orientation != 0 );
}
base_a = orientation;
/*****************************************************************************************/
/* init calipers position */
seq[0] = bottom;
seq[1] = right;
seq[2] = top;
seq[3] = left;
/*****************************************************************************************/
/* Main loop - evaluate angles and rotate calipers */
/* all of edges will be checked while rotating calipers by 90 degrees */
for( k = 0; k < n; k++ )
{
/* sinus of minimal angle */
/*float sinus;*/
/* compute cosine of angle between calipers side and polygon edge */
/* dp - dot product */
float dp0 = base_a * vect[seq[0]].x + base_b * vect[seq[0]].y;
float dp1 = -base_b * vect[seq[1]].x + base_a * vect[seq[1]].y;
float dp2 = -base_a * vect[seq[2]].x - base_b * vect[seq[2]].y;
float dp3 = base_b * vect[seq[3]].x - base_a * vect[seq[3]].y;
float cosalpha = dp0 * inv_vect_length[seq[0]];
float maxcos = cosalpha;
/* number of calipers edges, that has minimal angle with edge */
int main_element = 0;
/* choose minimal angle */
cosalpha = dp1 * inv_vect_length[seq[1]];
maxcos = (cosalpha > maxcos) ? (main_element = 1, cosalpha) : maxcos;
cosalpha = dp2 * inv_vect_length[seq[2]];
maxcos = (cosalpha > maxcos) ? (main_element = 2, cosalpha) : maxcos;
cosalpha = dp3 * inv_vect_length[seq[3]];
maxcos = (cosalpha > maxcos) ? (main_element = 3, cosalpha) : maxcos;
/*rotate calipers*/
{
/* sinus of minimal angle */
/*float sinus;*/
/* compute cosine of angle between calipers side and polygon edge */
/* dp - dot product */
float dp0 = base_a * vect[seq[0]].x + base_b * vect[seq[0]].y;
float dp1 = -base_b * vect[seq[1]].x + base_a * vect[seq[1]].y;
float dp2 = -base_a * vect[seq[2]].x - base_b * vect[seq[2]].y;
float dp3 = base_b * vect[seq[3]].x - base_a * vect[seq[3]].y;
float cosalpha = dp0 * inv_vect_length[seq[0]];
float maxcos = cosalpha;
/* number of calipers edges, that has minimal angle with edge */
int main_element = 0;
/* choose minimal angle */
cosalpha = dp1 * inv_vect_length[seq[1]];
maxcos = (cosalpha > maxcos) ? (main_element = 1, cosalpha) : maxcos;
cosalpha = dp2 * inv_vect_length[seq[2]];
maxcos = (cosalpha > maxcos) ? (main_element = 2, cosalpha) : maxcos;
cosalpha = dp3 * inv_vect_length[seq[3]];
maxcos = (cosalpha > maxcos) ? (main_element = 3, cosalpha) : maxcos;
/*rotate calipers*/
//get next base
int pindex = seq[main_element];
float lead_x = vect[pindex].x*inv_vect_length[pindex];
float lead_y = vect[pindex].y*inv_vect_length[pindex];
switch( main_element )
{
//get next base
int pindex = seq[main_element];
float lead_x = vect[pindex].x*inv_vect_length[pindex];
float lead_y = vect[pindex].y*inv_vect_length[pindex];
switch( main_element )
{
case 0:
base_a = lead_x;
base_b = lead_y;
break;
case 1:
base_a = lead_y;
base_b = -lead_x;
break;
case 2:
base_a = -lead_x;
base_b = -lead_y;
break;
case 3:
base_a = -lead_y;
base_b = lead_x;
break;
default:
CV_Error(CV_StsError, "main_element should be 0, 1, 2 or 3");
}
case 0:
base_a = lead_x;
base_b = lead_y;
break;
case 1:
base_a = lead_y;
base_b = -lead_x;
break;
case 2:
base_a = -lead_x;
base_b = -lead_y;
break;
case 3:
base_a = -lead_y;
base_b = lead_x;
break;
default:
CV_Error(CV_StsError, "main_element should be 0, 1, 2 or 3");
}
/* change base point of main edge */
seq[main_element] += 1;
seq[main_element] = (seq[main_element] == n) ? 0 : seq[main_element];
switch (mode)
{
case CALIPERS_MAXHEIGHT:
{
/* now main element lies on edge alligned to calipers side */
/* find opposite element i.e. transform */
/* 0->2, 1->3, 2->0, 3->1 */
int opposite_el = main_element ^ 2;
float dx = points[seq[opposite_el]].x - points[seq[main_element]].x;
float dy = points[seq[opposite_el]].y - points[seq[main_element]].y;
float dist;
if( main_element & 1 )
dist = (float)fabs(dx * base_a + dy * base_b);
else
dist = (float)fabs(dx * (-base_b) + dy * base_a);
if( dist > max_dist )
max_dist = dist;
break;
}
case CALIPERS_MINAREARECT:
/* find area of rectangle */
{
float height;
float area;
/* find vector left-right */
float dx = points[seq[1]].x - points[seq[3]].x;
float dy = points[seq[1]].y - points[seq[3]].y;
/* dotproduct */
float width = dx * base_a + dy * base_b;
/* find vector left-right */
dx = points[seq[2]].x - points[seq[0]].x;
dy = points[seq[2]].y - points[seq[0]].y;
/* dotproduct */
height = -dx * base_b + dy * base_a;
area = width * height;
if( area <= minarea )
{
float *buf = (float *) buffer;
minarea = area;
/* leftist point */
((int *) buf)[0] = seq[3];
buf[1] = base_a;
buf[2] = width;
buf[3] = base_b;
buf[4] = height;
/* bottom point */
((int *) buf)[5] = seq[0];
buf[6] = area;
}
break;
}
} /*switch */
} /* for */
}
/* change base point of main edge */
seq[main_element] += 1;
seq[main_element] = (seq[main_element] == n) ? 0 : seq[main_element];
switch (mode)
{
case CALIPERS_MINAREARECT:
case CALIPERS_MAXHEIGHT:
{
float *buf = (float *) buffer;
/* now main element lies on edge alligned to calipers side */
float A1 = buf[1];
float B1 = buf[3];
/* find opposite element i.e. transform */
/* 0->2, 1->3, 2->0, 3->1 */
int opposite_el = main_element ^ 2;
float A2 = -buf[3];
float B2 = buf[1];
float dx = points[seq[opposite_el]].x - points[seq[main_element]].x;
float dy = points[seq[opposite_el]].y - points[seq[main_element]].y;
float dist;
float C1 = A1 * points[((int *) buf)[0]].x + points[((int *) buf)[0]].y * B1;
float C2 = A2 * points[((int *) buf)[5]].x + points[((int *) buf)[5]].y * B2;
if( main_element & 1 )
dist = (float)fabs(dx * base_a + dy * base_b);
else
dist = (float)fabs(dx * (-base_b) + dy * base_a);
float idet = 1.f / (A1 * B2 - A2 * B1);
if( dist > max_dist )
max_dist = dist;
}
break;
case CALIPERS_MINAREARECT:
/* find area of rectangle */
{
float height;
float area;
float px = (C1 * B2 - C2 * B1) * idet;
float py = (A1 * C2 - A2 * C1) * idet;
/* find vector left-right */
float dx = points[seq[1]].x - points[seq[3]].x;
float dy = points[seq[1]].y - points[seq[3]].y;
out[0] = px;
out[1] = py;
out[2] = A1 * buf[2];
out[3] = B1 * buf[2];
out[4] = A2 * buf[4];
out[5] = B2 * buf[4];
}
break;
case CALIPERS_MAXHEIGHT:
/* dotproduct */
float width = dx * base_a + dy * base_b;
/* find vector left-right */
dx = points[seq[2]].x - points[seq[0]].x;
dy = points[seq[2]].y - points[seq[0]].y;
/* dotproduct */
height = -dx * base_b + dy * base_a;
area = width * height;
if( area <= minarea )
{
out[0] = max_dist;
float *buf = (float *) buffer;
minarea = area;
/* leftist point */
((int *) buf)[0] = seq[3];
buf[1] = base_a;
buf[2] = width;
buf[3] = base_b;
buf[4] = height;
/* bottom point */
((int *) buf)[5] = seq[0];
buf[6] = area;
}
break;
}
break;
} /*switch */
} /* for */
switch (mode)
{
case CALIPERS_MINAREARECT:
{
float *buf = (float *) buffer;
float A1 = buf[1];
float B1 = buf[3];
float A2 = -buf[3];
float B2 = buf[1];
float C1 = A1 * points[((int *) buf)[0]].x + points[((int *) buf)[0]].y * B1;
float C2 = A2 * points[((int *) buf)[5]].x + points[((int *) buf)[5]].y * B2;
float idet = 1.f / (A1 * B2 - A2 * B1);
float px = (C1 * B2 - C2 * B1) * idet;
float py = (A1 * C2 - A2 * C1) * idet;
out[0] = px;
out[1] = py;
out[2] = A1 * buf[2];
out[3] = B1 * buf[2];
out[4] = A2 * buf[4];
out[5] = B2 * buf[4];
}
break;
case CALIPERS_MAXHEIGHT:
{
out[0] = max_dist;
}
break;
}
}
}
@ -390,7 +389,7 @@ cv::RotatedRect cv::minAreaRect( InputArray _points )
CV_IMPL CvBox2D
cvMinAreaRect2( const CvArr* array, CvMemStorage* storage )
cvMinAreaRect2( const CvArr* array, CvMemStorage* /*storage*/ )
{
cv::AutoBuffer<double> abuf;
cv::Mat points = cv::cvarrToMat(array, false, false, 0, &abuf);

@ -110,7 +110,7 @@ static int findEnslosingCicle4pts_32f( Point2f* pts, Point2f& _center, float& _r
for( i = 0; i < 4; i++ )
for( j = i + 1; j < 4; j++ )
{
float dist = norm(pts[i] - pts[j]);
float dist = (float)norm(pts[i] - pts[j]);
if( max_dist < dist )
{
@ -132,13 +132,14 @@ static int findEnslosingCicle4pts_32f( Point2f* pts, Point2f& _center, float& _r
idxs[k++] = i;
}
center = Point2f( (pts[idxs[0]].x + pts[idxs[1]].x)*0.5f, (pts[idxs[0]].y + pts[idxs[1]].y)*0.5f );
center = Point2f( (pts[idxs[0]].x + pts[idxs[1]].x)*0.5f,
(pts[idxs[0]].y + pts[idxs[1]].y)*0.5f );
radius = (float)(norm(pts[idxs[0]] - center)*1.03);
if( radius < 1.f )
radius = 1.f;
if( pointInCircle( pts[idxs[2]], center, radius ) >= 0 &&
pointInCircle( pts[idxs[3]], center, radius ) >= 0 )
pointInCircle( pts[idxs[3]], center, radius ) >= 0 )
{
k = 2; //rand()%2+2;
}
@ -148,14 +149,14 @@ static int findEnslosingCicle4pts_32f( Point2f* pts, Point2f& _center, float& _r
for( i = 0; i < 4; i++ )
{
if( findCircle( pts[shuffles[i][0]], pts[shuffles[i][1]],
pts[shuffles[i][2]], &center, &radius ) >= 0 )
pts[shuffles[i][2]], &center, &radius ) )
{
radius *= 1.03f;
if( radius < 2.f )
radius = 2.f;
if( pointInCircle( pts[shuffles[i][3]], center, radius ) >= 0 &&
min_radius > radius )
min_radius > radius )
{
min_radius = radius;
min_center = center;
@ -217,9 +218,9 @@ void cv::minEnclosingCircle( InputArray _points, Point2f& _center, float& _radiu
Point2f pt = is_float ? ptsf[0] : Point2f((float)ptsi[0].x,(float)ptsi[0].y);
Point2f pts[4] = {pt, pt, pt, pt};
for(int i = 1; i < count; i++ )
for( i = 1; i < count; i++ )
{
Point2f pt = is_float ? ptsf[i] : Point2f((float)ptsi[i].x, (float)ptsi[i].y);
pt = is_float ? ptsf[i] : Point2f((float)ptsi[i].x, (float)ptsi[i].y);
if( pt.x < pts[0].x )
pts[0] = pt;
@ -234,20 +235,20 @@ void cv::minEnclosingCircle( InputArray _points, Point2f& _center, float& _radiu
for( k = 0; k < max_iters; k++ )
{
double min_delta = 0, delta;
Point2f ptf, farAway(0,0);
Point2f farAway(0,0);
/*only for first iteration because the alg is repared at the loop's foot*/
if( k == 0 )
findEnslosingCicle4pts_32f( pts, center, radius );
for( i = 0; i < count; i++ )
{
ptf = is_float ? ptsf[i] : Point2f((float)ptsi[i].x,(float)ptsi[i].y);
pt = is_float ? ptsf[i] : Point2f((float)ptsi[i].x,(float)ptsi[i].y);
delta = pointInCircle( ptf, center, radius );
delta = pointInCircle( pt, center, radius );
if( delta < min_delta )
{
min_delta = delta;
farAway = ptf;
farAway = pt;
}
}
result = min_delta >= 0;
@ -275,10 +276,10 @@ void cv::minEnclosingCircle( InputArray _points, Point2f& _center, float& _radiu
if( !result )
{
radius = 0.f;
for(int i = 0; i < count; i++ )
for( i = 0; i < count; i++ )
{
Point2f ptf = is_float ? ptsf[i] : Point2f((float)ptsi[i].x,(float)ptsi[i].y);
float dx = center.x - ptf.x, dy = center.y - ptf.y;
pt = is_float ? ptsf[i] : Point2f((float)ptsi[i].x,(float)ptsi[i].y);
float dx = center.x - pt.x, dy = center.y - pt.y;
float t = dx*dx + dy*dy;
radius = MAX(radius, t);
}
@ -1045,14 +1046,12 @@ cvFitEllipse2( const CvArr* array )
CV_IMPL CvRect
cvBoundingRect( CvArr* array, int update )
{
CvSeqReader reader;
CvRect rect = { 0, 0, 0, 0 };
CvContour contour_header;
CvSeq* ptseq = 0;
CvSeqBlock block;
CvMat stub, *mat = 0;
int xmin = 0, ymin = 0, xmax = -1, ymax = -1, i, j, k;
int calculate = update;
if( CV_IS_SEQ( array ))

Loading…
Cancel
Save