result, contours ,h = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
时间: 2023-08-13 08:53:56 浏览: 89
这是一个在OpenCV中使用的函数,用于在二制图像中查找廓。函数的参数括关闭的二进制图像(close),轮廓的检索模式(RETR_TREE),以及轮廓的近似方法(CHAIN_APPROX_SIMPLE)。函数返回三个值:结果图像(result),轮廓列表(contours),以及层次结构(h)。层次结构提供了轮廓之间的关系信息。
相关问题
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)
```
注意:这段代码仅供参考,可能还需要根据实际情况进行调整和修改。
import cv2 import numpy as np img = cv2.imread('D:/xiazai/yxtxcl4/BloodCell.jpg', -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_invcv2.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)
这段代码主要是对一张血细胞图像进行图像分割和细胞计数。具体而言,代码首先读入一张图像并进行均值滤波,然后通过大津算法求取一个阈值,将图像二值化。接下来,采用形态学开操作和闭操作对二值化后的图像进行处理,去除噪声和连接细胞。接着,使用漫水填充算法找到每个细胞的边界,并计算细胞数量。最后,将细胞数量输出。
需要注意的是,这段代码可能会在不同的OpenCV版本上产生不同的结果,因为不同版本的函数参数可能存在差异。如果你需要在自己的项目中使用这段代码,请确保你使用的OpenCV版本与作者使用的版本一致,或者根据提示进行相应的修改。
阅读全文