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)
时间: 2024-03-24 20:37:29 浏览: 84
这段代码中有一些语法错误和拼写错误。以下是修正后的代码:
```
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)
```
注意:这段代码仅供参考,可能还需要根据实际情况进行调整和修改。
阅读全文