## OpenCV曲线拟合与圆拟合

### 一：曲线拟合与应用

``````def circle_fitness_demo():
image = np.zeros((400, 400, 3), dtype=np.uint8)
x = np.array([30, 50, 100, 120])
y = np.array([100, 150, 240, 200])
for i in range(len(x)):
cv.circle(image, (x[i], y[i]), 3, (255, 0, 0), -1, 8, 0)
cv.imwrite("D:/curve.png", image)

poly = np.poly1d(np.polyfit(x, y, 3))
print(poly)
for t in range(30, 250, 1):
y_ = np.int(poly(t))
cv.circle(image, (t, y_), 1, (0, 0, 255), 1, 8, 0)
cv.imshow("fit curve", image)
cv.imwrite("D:/fitcurve.png", image)``````

### 二：圆拟合与应用

``````def circle_fitness_demo():
cv.imshow("input", src)
src = cv.GaussianBlur(src, (3, 3), 0)
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow("binary", binary)

image, contours, hierachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)):
rrt = cv.fitEllipse(contours[i])
cv.ellipse(src, rrt, (0, 0, 255), 2, cv.LINE_AA)
x, y = rrt[0]
cv.circle(src, (np.int(x), np.int(y)), 4, (255, 0, 0), -1, 8, 0)
cv.imshow("fit circle", src)
cv.imwrite("D:/fitcircle.png", src)``````

OpenCV Python零基础入门视频教程
OpenCV Python图像处理进阶视频教程