OpenCV PCA方法 计算轮廓角度

参考文章

原理:
https://www.cnblogs.com/mikewolf2002/p/3432243.html
http://blog.codinglabs.org/articles/pca-tutorial.html

根据pca方法可以拟合出 长宽明显轮廓的角度。
下面为实现代码:

#include
//PCA 分析轮廓角度
double getCountourAngleWithPCA(std::vector<cv::Point>& contours/*轮廓*/)
{
	//Construct a buffer used by the pca analysis
	cv::Mat data_pts = cv::Mat((int)contours.size(), 2, CV_64FC1);
	for (int i = 0; i < data_pts.rows; ++i)
	{
		data_pts.at<double>(i, 0) = contours[i].x;
		data_pts.at<double>(i, 1) = contours[i].y;
	}
	//Perform PCA analysis
	cv::PCA pca_analysis(data_pts, cv::Mat(), 0);
	return cv::fastAtan2((float)pca_analysis.eigenvectors.at<double>(0, 1), (float)pca_analysis.eigenvectors.at<double>(0, 0));
}

你可能感兴趣的:(opencv,opencv,算法)