opencv计算轮廓中心
时间: 2023-10-12 17:05:27 浏览: 224
要计算轮廓的中心,你可以使用OpenCV中的函数`cv2.moments`。以下是一个示例代码:
```python
import cv2
# 读取图像并转换为灰度
image = cv2.imread('your_image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 进行边缘检测
edges = cv2.Canny(gray, 50, 150)
# 寻找轮廓
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 计算每个轮廓的矩和中心
for contour in contours:
# 计算轮廓的矩
M = cv2.moments(contour)
# 计算轮廓的中心坐标
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
# 在图像上绘制中心点
cv2.circle(image, (cx, cy), 5, (0, 255, 0), -1)
# 显示图像
cv2.imshow('Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
请确保将`your_image.jpg`替换为你想要处理的图像的路径。这段代码将找到图像中的边缘,然后计算每个轮廓的中心,并在原始图像上绘制出来。
阅读全文
相关推荐













