|
|
|
@ -1,103 +1,103 @@ |
|
|
|
|
/*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.
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// Intel 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 Intel Corporation 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 Intel Corporation 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" |
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
namespace cv |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
struct MinAreaState |
|
|
|
|
{ |
|
|
|
|
int bottom; |
|
|
|
|
int left; |
|
|
|
|
float height; |
|
|
|
|
float width; |
|
|
|
|
float base_a; |
|
|
|
|
float base_b; |
|
|
|
|
} |
|
|
|
|
icvMinAreaState; |
|
|
|
|
|
|
|
|
|
#define CV_CALIPERS_MAXHEIGHT 0 |
|
|
|
|
#define CV_CALIPERS_MINAREARECT 1 |
|
|
|
|
#define CV_CALIPERS_MAXDIST 2 |
|
|
|
|
|
|
|
|
|
/*F///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Name: icvRotatingCalipers
|
|
|
|
|
// 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 |
|
|
|
|
icvRotatingCalipers( CvPoint2D32f* points, int n, int mode, float* out ) |
|
|
|
|
{ |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
CvPoint2D32f* vect = (CvPoint2D32f*)cvAlloc( n * sizeof(vect[0]) ); |
|
|
|
|
float* inv_vect_length = (float*)cvAlloc( n * sizeof(inv_vect_length[0]) ); |
|
|
|
|
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 }; |
|
|
|
|
|
|
|
|
@ -110,7 +110,7 @@ icvRotatingCalipers( CvPoint2D32f* points, int n, int mode, float* out ) |
|
|
|
|
float base_b = 0; |
|
|
|
|
|
|
|
|
|
float left_x, right_x, top_y, bottom_y; |
|
|
|
|
CvPoint2D32f pt0 = points[0]; |
|
|
|
|
Point2f pt0 = points[0]; |
|
|
|
|
|
|
|
|
|
left_x = right_x = pt0.x; |
|
|
|
|
top_y = bottom_y = pt0.y; |
|
|
|
@ -131,7 +131,7 @@ icvRotatingCalipers( CvPoint2D32f* points, int n, int mode, float* out ) |
|
|
|
|
if( pt0.y < bottom_y ) |
|
|
|
|
bottom_y = pt0.y, bottom = i; |
|
|
|
|
|
|
|
|
|
CvPoint2D32f 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; |
|
|
|
@ -143,9 +143,7 @@ icvRotatingCalipers( CvPoint2D32f* points, int n, int mode, float* out ) |
|
|
|
|
pt0 = pt; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//cvbInvSqrt( inv_vect_length, inv_vect_length, n );
|
|
|
|
|
|
|
|
|
|
/* find convex hull orientation */ |
|
|
|
|
// find convex hull orientation
|
|
|
|
|
{ |
|
|
|
|
double ax = vect[n-1].x; |
|
|
|
|
double ay = vect[n-1].y; |
|
|
|
@ -165,18 +163,18 @@ icvRotatingCalipers( CvPoint2D32f* points, int n, int mode, float* out ) |
|
|
|
|
ax = bx; |
|
|
|
|
ay = by; |
|
|
|
|
} |
|
|
|
|
assert( orientation != 0 ); |
|
|
|
|
CV_Assert( orientation != 0 ); |
|
|
|
|
} |
|
|
|
|
base_a = orientation; |
|
|
|
|
|
|
|
|
|
/*****************************************************************************************/ |
|
|
|
|
/* init calipers position */ |
|
|
|
|
/*****************************************************************************************/ |
|
|
|
|
/* init calipers position */ |
|
|
|
|
seq[0] = bottom; |
|
|
|
|
seq[1] = right; |
|
|
|
|
seq[2] = top; |
|
|
|
|
seq[3] = left; |
|
|
|
|
/*****************************************************************************************/ |
|
|
|
|
/* Main loop - evaluate angles and rotate calipers */ |
|
|
|
|
/*****************************************************************************************/ |
|
|
|
|
/* 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++ ) |
|
|
|
@ -229,17 +227,17 @@ icvRotatingCalipers( CvPoint2D32f* points, int n, int mode, float* out ) |
|
|
|
|
base_a = -lead_y; |
|
|
|
|
base_b = lead_x; |
|
|
|
|
break; |
|
|
|
|
default: assert(0); |
|
|
|
|
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 CV_CALIPERS_MAXHEIGHT: |
|
|
|
|
case CALIPERS_MAXHEIGHT: |
|
|
|
|
{ |
|
|
|
|
/* now main element lies on edge alligned to calipers side */ |
|
|
|
|
|
|
|
|
@ -261,7 +259,7 @@ icvRotatingCalipers( CvPoint2D32f* points, int n, int mode, float* out ) |
|
|
|
|
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case CV_CALIPERS_MINAREARECT: |
|
|
|
|
case CALIPERS_MINAREARECT: |
|
|
|
|
/* find area of rectangle */ |
|
|
|
|
{ |
|
|
|
|
float height; |
|
|
|
@ -304,7 +302,7 @@ icvRotatingCalipers( CvPoint2D32f* points, int n, int mode, float* out ) |
|
|
|
|
|
|
|
|
|
switch (mode) |
|
|
|
|
{ |
|
|
|
|
case CV_CALIPERS_MINAREARECT: |
|
|
|
|
case CALIPERS_MINAREARECT: |
|
|
|
|
{ |
|
|
|
|
float *buf = (float *) buffer; |
|
|
|
|
|
|
|
|
@ -332,87 +330,38 @@ icvRotatingCalipers( CvPoint2D32f* points, int n, int mode, float* out ) |
|
|
|
|
out[5] = B2 * buf[4]; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case CV_CALIPERS_MAXHEIGHT: |
|
|
|
|
case CALIPERS_MAXHEIGHT: |
|
|
|
|
{ |
|
|
|
|
out[0] = max_dist; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cvFree( &vect ); |
|
|
|
|
cvFree( &inv_vect_length ); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CV_IMPL CvBox2D |
|
|
|
|
cvMinAreaRect2( const CvArr* array, CvMemStorage* storage ) |
|
|
|
|
cv::RotatedRect cv::minAreaRect( InputArray _points ) |
|
|
|
|
{ |
|
|
|
|
cv::Ptr<CvMemStorage> temp_storage; |
|
|
|
|
CvBox2D box; |
|
|
|
|
cv::AutoBuffer<CvPoint2D32f> _points; |
|
|
|
|
CvPoint2D32f* points; |
|
|
|
|
Mat hull; |
|
|
|
|
Point2f out[3]; |
|
|
|
|
RotatedRect box; |
|
|
|
|
|
|
|
|
|
memset(&box, 0, sizeof(box)); |
|
|
|
|
convexHull(_points, hull, true, true); |
|
|
|
|
|
|
|
|
|
int i, n; |
|
|
|
|
CvSeqReader reader; |
|
|
|
|
CvContour contour_header; |
|
|
|
|
CvSeqBlock block; |
|
|
|
|
CvSeq* ptseq = (CvSeq*)array; |
|
|
|
|
CvPoint2D32f out[3]; |
|
|
|
|
|
|
|
|
|
if( CV_IS_SEQ(ptseq) ) |
|
|
|
|
{ |
|
|
|
|
if( !CV_IS_SEQ_POINT_SET(ptseq) && |
|
|
|
|
(CV_SEQ_KIND(ptseq) != CV_SEQ_KIND_CURVE || |
|
|
|
|
CV_SEQ_ELTYPE(ptseq) != CV_SEQ_ELTYPE_PPOINT )) |
|
|
|
|
CV_Error( CV_StsUnsupportedFormat, |
|
|
|
|
"Input sequence must consist of 2d points or pointers to 2d points" ); |
|
|
|
|
if( !storage ) |
|
|
|
|
storage = ptseq->storage; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
ptseq = cvPointSeqFromMat( CV_SEQ_KIND_GENERIC, array, &contour_header, &block ); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if( storage ) |
|
|
|
|
{ |
|
|
|
|
temp_storage = cvCreateChildMemStorage( storage ); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
if( hull.depth() != CV_32F ) |
|
|
|
|
{ |
|
|
|
|
temp_storage = cvCreateMemStorage(1 << 10); |
|
|
|
|
Mat temp; |
|
|
|
|
hull.convertTo(temp, CV_32F); |
|
|
|
|
hull = temp; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ptseq = cvConvexHull2( ptseq, temp_storage, CV_CLOCKWISE, 1 ); |
|
|
|
|
n = ptseq->total; |
|
|
|
|
|
|
|
|
|
_points.allocate(n); |
|
|
|
|
points = _points; |
|
|
|
|
cvStartReadSeq( ptseq, &reader ); |
|
|
|
|
|
|
|
|
|
if( CV_SEQ_ELTYPE( ptseq ) == CV_32SC2 ) |
|
|
|
|
{ |
|
|
|
|
for( i = 0; i < n; i++ ) |
|
|
|
|
{ |
|
|
|
|
CvPoint pt; |
|
|
|
|
CV_READ_SEQ_ELEM( pt, reader ); |
|
|
|
|
points[i].x = (float)pt.x; |
|
|
|
|
points[i].y = (float)pt.y; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
for( i = 0; i < n; i++ ) |
|
|
|
|
{ |
|
|
|
|
CV_READ_SEQ_ELEM( points[i], reader ); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
int n = hull.checkVector(2); |
|
|
|
|
const Point2f* hpoints = (const Point2f*)hull.data; |
|
|
|
|
|
|
|
|
|
if( n > 2 ) |
|
|
|
|
{ |
|
|
|
|
icvRotatingCalipers( points, n, CV_CALIPERS_MINAREARECT, (float*)out ); |
|
|
|
|
rotatingCalipers( hpoints, n, CALIPERS_MINAREARECT, (float*)out ); |
|
|
|
|
box.center.x = out[0].x + (out[1].x + out[2].x)*0.5f; |
|
|
|
|
box.center.y = out[0].y + (out[1].y + out[2].y)*0.5f; |
|
|
|
|
box.size.width = (float)sqrt((double)out[1].x*out[1].x + (double)out[1].y*out[1].y); |
|
|
|
@ -421,10 +370,10 @@ cvMinAreaRect2( const CvArr* array, CvMemStorage* storage ) |
|
|
|
|
} |
|
|
|
|
else if( n == 2 ) |
|
|
|
|
{ |
|
|
|
|
box.center.x = (points[0].x + points[1].x)*0.5f; |
|
|
|
|
box.center.y = (points[0].y + points[1].y)*0.5f; |
|
|
|
|
double dx = points[1].x - points[0].x; |
|
|
|
|
double dy = points[1].y - points[0].y; |
|
|
|
|
box.center.x = (hpoints[0].x + hpoints[1].x)*0.5f; |
|
|
|
|
box.center.y = (hpoints[0].y + hpoints[1].y)*0.5f; |
|
|
|
|
double dx = hpoints[1].x - hpoints[0].x;
|
|
|
|
|
double dy = hpoints[1].y - hpoints[0].y; |
|
|
|
|
box.size.width = (float)sqrt(dx*dx + dy*dy); |
|
|
|
|
box.size.height = 0; |
|
|
|
|
box.angle = (float)atan2( dy, dx ); |
|
|
|
@ -432,10 +381,21 @@ cvMinAreaRect2( const CvArr* array, CvMemStorage* storage ) |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
if( n == 1 ) |
|
|
|
|
box.center = points[0]; |
|
|
|
|
box.center = hpoints[0]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
box.angle = (float)(box.angle*180/CV_PI); |
|
|
|
|
return box; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CV_IMPL CvBox2D |
|
|
|
|
cvMinAreaRect2( const CvArr* array, CvMemStorage* storage ) |
|
|
|
|
{ |
|
|
|
|
cv::AutoBuffer<double> abuf; |
|
|
|
|
cv::Mat points = cv::cvarrToMat(array, false, false, 0, &abuf); |
|
|
|
|
|
|
|
|
|
cv::RotatedRect rr = cv::minAreaRect(points); |
|
|
|
|
return (CvBox2D)rr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|