opencv python 曲线拟合
时间: 2023-12-22 09:28:58 浏览: 175
opencv+python 离散点的车道线拟合.zip
5星 · 资源好评率100%
以下是两种使用OpenCV进行曲线拟合的例子:
1. 最小外包圆形拟合:
```python
import cv2 as cv
import numpy as np
image = cv.imread("huzi.jpeg")
cv.imshow("image", image)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
contours, hierarchy = cv.findContours(binary, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
(x, y), rad = cv.minEnclosingCircle(contours[0])
center = (int(x), int(y))
rad = int(rad)
cv.circle(image, center, rad, (255, 255, 255), 1)
cv.imshow("result", image)
cv.waitKey()
cv.destroyAllWindows()
```
2. 最小外包椭圆拟合:
```python
import cv2 as cv
import numpy as np
image = cv.imread("huzi.jpeg")
cv.imshow("image", image)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
contours, hierarchy = cv.findContours(binary, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
ellipse = cv.fitEllipse(contours[0])
cv.ellipse(image, ellipse, (255, 255, 0), 1)
cv.imshow("result", image)
cv.waitKey()
cv.destroyAllWindows()
```
阅读全文