python提取二维图像边框
时间: 2023-09-23 21:12:23 浏览: 63
要提取二维图像的边框,可以使用OpenCV库中的函数。以下是一个示例代码:
```
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用Canny函数检测边缘
edges = cv2.Canny(gray, 100, 200)
# 查找边框
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 绘制边框
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
# 显示结果
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这个代码首先读取一张图像,然后将其转换为灰度图像。接着使用Canny函数检测边缘。然后使用findContours函数查找边框,最后使用drawContours函数绘制边框。执行代码后,将显示带有边框的原始图像。
阅读全文
相关推荐















