最近在做机器视觉的项目,其中有一步需要特征物体的边缘检测及中心位置像素坐标估计,会涉及到一些曲线拟合的内容,在此记录下来,和大家分享。
opencv提供了三种椭圆拟合的方法,其输入输出都相同,只是调用的算法不同,我会先介绍基本用法和一些易踩的坑,关于算法的分析比较会在文章后面深入探讨。
center, axes, angle = cv2.fitEllipse(points)
center, axes, angle = cv2.fitEllipseAMS(points)
center, axes, angle = cv2.fitEllipseDirect(points)
cv2.fitEllipse
此函数用于拟合椭圆
center, axes, angle = cv2.fitEllipse(points)
- Input
points可以是调用cv2.findContours()的返回contours中的一个contour。
如果需要手工构建points作为输入,则用numpy构contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) points = contours[0] # contours[0] 指选取contours返回的第一组contour