opencv 图片提取印章
时间: 2023-10-25 18:11:03 浏览: 219
1. 首先使用OpenCV读取图片:
```
import cv2
# 读取图片
img = cv2.imread('image.jpg')
```
2. 将图片转换为灰度图像:
```
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
```
3. 使用Canny算法进行边缘检测:
```
# 边缘检测
edges = cv2.Canny(gray, 50, 200)
```
4. 使用霍夫变换检测圆形:
```
# 检测圆形
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)
# 绘制圆形
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
cv2.circle(img, (i[0], i[1]), i[2], (0, 0, 255), 2)
```
5. 显示结果:
```
# 显示结果
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
完整代码:
```
import cv2
import numpy as np
# 读取图片
img = cv2.imread('image.jpg')
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 边缘检测
edges = cv2.Canny(gray, 50, 200)
# 检测圆形
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)
# 绘制圆形
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
cv2.circle(img, (i[0], i[1]), i[2], (0, 0, 255), 2)
# 显示结果
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文