卡尺工具实现步骤
在上一篇《用OPENCV,C++实现直线卡尺工具原理,一步到位》文章里已经很清除的说明了卡尺工具原理。今天在直线卡尺代码上进行修改,写了这个圆卡尺的代码。除了以下几点不同,剩余都相同。
1.计算圆的等分点
2.计算圆上的卡尺矩形,卡尺方向由圆心指向指向卡尺中心,投影方向则与圆相切。
3.拟合圆。
计算圆心等分点:
std::vector<cv::Point2f> caliperType::calcuCircleDivisionPoint(const cv::Point2f & center, double radius, int n)
{
std::vector<cv::Point2f> points;
double avg_radian = 2.0 * PAI / (n*1.0);
cv::Point2f pt;
for (int i = 0; i < n; i++) {
pt.x = center.x + cos(avg_radian * i) * radius;
pt.y = center.y - sin(avg_radian * i) * radius;
points.push_back(pt);
}
return points;
}
计算矩形卡尺:
std::vector<cv::Point2f> caliperType::calcuCalliperRect(cv::Point2f center, int half_width, int half_length, double angle)
{
std::vector<cv::Point2f> points;
double radius = sqrt(half_length*half_length + half_width*half_width);
double araf1 = atan2(half_width, half_length) + PAI / 180.0 * angle;
double araf2 = PAI - atan2(half_width, half_length) + +PAI / 180.0 * angle;
cv::Point2f pt, pt1;
pt.x = center.x + cos(araf1) * radius;
pt.y = center.y - sin(araf1) * radius;
pt1.x = center.x + cos(araf2) * radius;
pt1.y = center.y - sin(araf2) * radius;
points.push_back(center * 2 - pt);
points.push_back(center * 2 - pt1);
points.push_back(pt);
points.push_back(pt1);
return points;
}
拟合圆,直接用OpenCV自带的函数:
//m_vectTargetEdgePoints是找到的目标点集合
cv::RotatedRect item = cv::fitEllipse(m_vectTargetEdgePoints);
center = item.center;
cv::Point2f Points[4];
item.points(Points);
radius = genDistance(Points[0].x, Points[0].y, Points[1].x, Points[1].y) / 2.0;
实操效果:
在提取边缘点时,可以根据二阶导极性和点的位置进行更加细节的筛选,比如亮到暗、暗到亮,第一点、最后一点。
了解原理后,实现起来就是这么简单。有问题可以留言哟