#include<iostream>
#include<opencv2/opencv.hpp>
#include<vector>

using namespace cv;
using namespace std;

int main()
{
	//首先确定构成轮廓的点集
	vector<Point2f> g_vsrcPoints;
	//向容器内存储点的坐标
	g_vsrcPoints.push_back(Point2f(0, 0));
	g_vsrcPoints.push_back(Point2f(10, 0));
	g_vsrcPoints.push_back(Point2f(10, 10));
	g_vsrcPoints.push_back(Point2f(5, 6));

	//计算轮廓的矩
	Moments dstMoments = moments(g_vsrcPoints);
	cout << "【原始轮廓的面积为:】" << dstMoments.m00 << endl;

	cout << "【原始轮廓的重心为:】" << dstMoments.m10 << endl;

	cout << "【原始轮廓的主轴为:】" << dstMoments.nu20 << endl;

	//利用曲线逼近,计算逼近曲线的矩
	//首先创建一个逼近曲线
	vector<Point2f> approx;
	approxPolyDP(g_vsrcPoints, approx, 3, true);
	//接着计算得到的逼近曲线的矩
	dstMoments = moments(approx);
	cout << "【原始轮廓的面积为:】" << dstMoments.m00 << endl;

	return 0;
}

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐