contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
时间: 2024-05-09 10:04:49 浏览: 164
这段代码使用OpenCV中的函数`cv2.findContours()`来查找二进制图像中的轮廓(contours)。它需要三个参数:
1. 输入图像:二进制图像,其中需要查找轮廓。
2. 轮廓检索模式:指定轮廓检索模式。可选值包括`cv2.RETR_EXTERNAL`(只检测外部轮廓)、`cv2.RETR_LIST`(检测所有轮廓,但不建立轮廓间的层级关系)、`cv2.RETR_TREE`(检测所有轮廓,并建立完整的层级关系)等。
3. 轮廓逼近方法:指定轮廓逼近方法。可选值包括`cv2.CHAIN_APPROX_NONE`(存储所有轮廓点)、`cv2.CHAIN_APPROX_SIMPLE`(仅存储端点),以及其他一些更高级的逼近方法。
函数返回两个值:轮廓(contours)和层级关系(hierarchy)。在这个函数中,只用到了轮廓(contours)。
相关问题
输入一张班级合影,用圆形、矩阵、椭圆等凸显出你自己所在的区域。 cv2.bitwise_and cv2.bitwise_or
首先,我们需要导入OpenCV库和numpy库:
```python
import cv2
import numpy as np
```
然后,我们可以读取班级合影图片,并将其转换为灰度图像:
```python
img = cv2.imread('class_photo.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
```
接下来,我们可以使用cv2.threshold函数将灰度图像转换为二值图像:
```python
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
```
现在,我们可以使用cv2.findContours函数查找二值图像中的轮廓,并使用cv2.drawContours函数将轮廓绘制在原始图像上:
```python
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 绘制所有轮廓
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
```
接下来,我们可以创建一个与原始图像大小相同的掩模,然后使用cv2.circle、cv2.rectangle、cv2.ellipse等函数在掩模上绘制自己所在的区域:
```python
# 创建掩模
mask = np.zeros_like(img)
# 绘制圆形
cv2.circle(mask, (300, 300), 150, (255, 255, 255), -1)
# 绘制矩形
cv2.rectangle(mask, (100, 100), (500, 400), (255, 255, 255), -1)
# 绘制椭圆
cv2.ellipse(mask, (600, 300), (100, 200), 45, 0, 360, (255, 255, 255), -1)
```
最后,我们可以使用cv2.bitwise_and或cv2.bitwise_or函数将掩模与原始图像相结合,以凸显出自己所在的区域:
```python
# 将掩模与原始图像相结合
result = cv2.bitwise_and(img, mask)
# 显示结果
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
完整代码如下:
```python
import cv2
import numpy as np
# 读取图片
img = cv2.imread('class_photo.jpg')
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 将灰度图像转换为二值图像
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 绘制所有轮廓
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
# 创建掩模
mask = np.zeros_like(img)
# 绘制圆形
cv2.circle(mask, (300, 300), 150, (255, 255, 255), -1)
# 绘制矩形
cv2.rectangle(mask, (100, 100), (500, 400), (255, 255, 255), -1)
# 绘制椭圆
cv2.ellipse(mask, (600, 300), (100, 200), 45, 0, 360, (255, 255, 255), -1)
# 将掩模与原始图像相结合
result = cv2.bitwise_and(img, mask)
# 显示结果
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
import cv2 import numpy as np img = cv2. imread(' blood. BMP',-1) cv2. imshow("source".img) dst = cv2.blur(img,(3,3)) ret, thresh = cv2.threshold(dst,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) cv2. imshow("thresh".thresh) kernel = np.ones((4,4),np.uint8) opening = cv2.morphologyEx (thresh,cv2. MORPH_OPEN,kernel,iterations=2) kernel1 = np.ones((3, 3).np.uint8) close = cv2.morphologyEx(openning,cv2.MORPH_CLOSE,kernel1) cv2. imshow("opening",close) temp = close.copy () h, w = close. shape[:2] mask= np.zeros((h+2,w+2).np.uint8) cv2.floodFill(temp,mask,(230,145),255) temp_inv = cv2.bitwise_not(temp) result = close|temp_inv cv2. imshow("result",result) contours, hirearchy = cv2. findContours(result,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) count = 0 area = 0 for i in contours: if cv2.contourArea(i)>73: area+=cv2.contourArea(i) count+=1 count1 = 0 for i in contours: if cv2. contourArea(i)>73: count1+=1 if cv2. contourArea(i)>(1.25*area/count): count1+=1 if cv2. contourArea(i)>(2.1*area/count): count1+=1 print("细胞有%d个"%count1) cv2. waitKey(0)
这段代码中有一些语法错误和拼写错误。以下是修正后的代码:
```
import cv2
import numpy as np
img = cv2.imread('blood.BMP', -1)
cv2.imshow("source", img)
dst = cv2.blur(img, (3, 3))
ret, thresh = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
cv2.imshow("thresh", thresh)
kernel = np.ones((4, 4), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
kernel1 = np.ones((3, 3), np.uint8)
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel1)
cv2.imshow("opening", close)
temp = close.copy()
h, w = close.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
cv2.floodFill(temp, mask, (230, 145), 255)
temp_inv = cv2.bitwise_not(temp)
result = close | temp_inv
cv2.imshow("result", result)
contours, hierarchy = cv2.findContours(result, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
count = 0
area = 0
for i in contours:
if cv2.contourArea(i) > 73:
area += cv2.contourArea(i)
count += 1
count1 = 0
for i in contours:
if cv2.contourArea(i) > 73:
count1 += 1
if cv2.contourArea(i) > (1.25 * area / count):
count1 += 1
if cv2.contourArea(i) > (2.1 * area / count):
count1 += 1
print("细胞有%d个" % count1)
cv2.waitKey(0)
```
注意:这段代码仅供参考,可能还需要根据实际情况进行调整和修改。
阅读全文