opencv python 提取圆形区域中的圆形斑点
时间: 2023-10-06 11:10:08 浏览: 77
要提取圆形区域中的圆形斑点,你可以使用OpenCV中的二值化和形态学操作。
下面是一个示例代码,演示了如何使用二值化和形态学操作来提取圆形区域中的圆形斑点:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('circle_roi.jpg')
# 灰度化和二值化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# 形态学操作
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)
# 查找轮廓
contours, hierarchy = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
for i, contour in enumerate(contours):
area = cv2.contourArea(contour)
if area < 10 or area > 100:
continue
(x, y), radius = cv2.minEnclosingCircle(contour)
center = (int(x), int(y))
radius = int(radius)
cv2.circle(img, center, radius, (0, 255, 0), 2)
# 显示结果
cv2.imshow('result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这里,我们首先将圆形区域进行灰度化和二值化,然后使用形态学操作来去除噪点和孔洞。接着,我们使用cv2.findContours()函数查找轮廓,并使用cv2.minEnclosingCircle()函数计算每个轮廓的最小外接圆,如果圆形斑点的面积在10到100之间,就绘制出来。
在循环中,我们将绘制出的圆形斑点通过cv2.imshow()函数显示出来,你可以更改代码来保存提取的圆形斑点或进行其他操作。