我想计算图像中的细胞和细胞菌落(如链接所示)。它在3点钟左右有一个牢房,在8点钟左右有一个牢房/斑点。
我尝试使用cv2软件包:
import cv2
import numpy as np
import math
image = cv2.imread("C:\\Users\\Tadas\\Desktop\\img-r.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret,th = cv2.threshold(image,155,255,cv2.THRESH_BINARY)
cnts,_ = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
minimum_area = 10
average_cell_area = 14
connected_cell_area = 30
large_collony = 100
cells = 0
cells_individual=0
collonies=0
for c in cnts:
area = cv2.contourArea(c)
if area > minimum_area:
cv2.drawContours(th, [c], -1, (255,2,1), 1)
if area > connected_cell_area:
cells += math.ceil(area / average_cell_area)
if area > large_collony:
collonies += 1
else:
cells += 1
cells_individual +=1
print('Cells: {}'.format(cells))
print('Individual cells: {}'.format(cells_individual))
print('Collonies: {}'.format(collonies))
th=cv2.resize(th,(819,819))
cv2.imshow('image', th)
cv2.waitKey()
如果我从这张照片中剪裁出细胞,它会有些工作。我怎么能忽略它?也许有比阈值更好的过滤图像的方法了吗?感谢帮助!
大约3点钟有一个牢房,大约8点钟有一个牢房/斑点。
最佳答案
我建议在Python / OpenCV中使用自适应阈值,如下所示:
import cv2
import numpy as np
import math
# read image
image = cv2.imread("C:\\Users\\Tadas\\Desktop\\img-r.jpg")
# convert to grayscale
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# apply adaptive thresholding and invert
th = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 55, 30)
th = 255 - th
# remove everything outside radius from center using a circle mask
radius = 1900
hh, ww = image.shape[:2]
cx = ww // 2
cy = hh // 2
mask = np.zeros_like(th)
cv2.circle(mask, (cx,cy), radius, 255, -1)
th[mask==0] = 0
# get contours
cnts = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
minimum_area = 10
average_cell_area = 14
connected_cell_area = 30
large_collony = 100
cells = 0
cells_individual=0
collonies=0
# filter contours
for c in cnts:
area = cv2.contourArea(c)
if area > minimum_area:
cv2.drawContours(th, [c], -1, (255,2,1), 1)
if area > connected_cell_area:
cells += math.ceil(area / average_cell_area)
if area > large_collony:
collonies += 1
else:
cells += 1
cells_individual +=1
# report cells
print('Cells: {}'.format(cells))
print('Individual cells: {}'.format(cells_individual))
print('Collonies: {}'.format(collonies))
th=cv2.resize(th,(819,819))
cv2.imshow('image', th)
cv2.waitKey()
报告结果:
Cells: 25
Individual cells: 1
Collonies: 1
关于python - 具有不必要功能的图像中的细胞计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60792089/