您的位置:首页 > 运维架构

opencv--轮廓特征2

2016-05-27 16:55 267 查看
函数Moments
moments(InputArray
array, bool
binaryImage=false
)用来计算多边形或珊格形状的0~3阶矩。

moments类定义如下

class
Moments
{
public:
Moments();

Moments(double
m00,
double m10,
double
m01,
double m20,
double
m11,
double
m02,
double m30,
double
m21,
double m12,
double
m03 );

Moments( const
CvMoments&
moments );
operator
CvMoments()
const;
// spatial moments
double
m00, m10, m01, m20, m11, m02, m30, m21, m12, m03;
// central moments
double
mu20, mu11, mu02, mu30, mu21, mu12, mu03;
// central normalized moments
double
nu20, nu11, nu02, nu30, nu21, nu12, nu03;

}

mXX是0~3阶矩,muXX是0~3阶中心矩,nuXX是0~3阶归一化中心矩,公式如下





当计算contour的矩时,使用的是格林公式。

下面是opencv自带的例程

Mat
canny_output;

vector<vector<Point> >
contours;

vector<Vec4i>
hierarchy;
/// Detect edges using canny
Canny( src_gray, canny_output, thresh, thresh*2,
3 );
/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,
0) );
/// Get the moments
vector<Moments>
mu(contours.size() );
for(
int
i
= 0; i
< contours.size(); i++
)

{ mu[i] = moments( contours[i],
false ); }
/// Get the mass centers:
vector<Point2f>
mc( contours.size() );
for(
int
i
= 0; i
< contours.size(); i++
)

{ mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00
); }
/// Draw contours
Mat drawing
= Mat::zeros( canny_output.size(), CV_8UC3 );
for(
int
i
= 0; i<
contours.size(); i++
)

{

Scalar color =
Scalar( rng.uniform(0,
255), rng.uniform(0,255), rng.uniform(0,255)
);

drawContours( drawing, contours, i, color, 2,
8, hierarchy,
0, Point() );

circle( drawing, mc[i], 4, color,
-1,
8,
0 );

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opencv contour