opencv 二维测量模型代码
时间: 2023-11-13 09:44:13 浏览: 31
以下是一个使用 OpenCV 进行二维测量的示例代码:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('sample.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 进行边缘检测
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
# 进行霍夫直线变换
lines = cv2.HoughLines(edges, 1, np.pi/180, 200)
# 绘制直线
for line in lines:
rho, theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
# 显示结果
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
该代码使用了 Canny 边缘检测和霍夫直线变换,可以检测出图像中的直线并进行绘制。你可以根据需要对代码进行修改和优化。