首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有不必要特征的图像中的细胞计数

具有不必要特征的图像中的细胞计数
EN

Stack Overflow用户
提问于 2020-03-22 03:03:13
回答 1查看 36关注 0票数 0

我想计算图像中的细胞和细胞胶原体(如链接所示)。它在大约3点有一个单细胞,在8点有一个细胞胶原/斑点。

我尝试使用cv2包:

代码语言:javascript
复制
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点的细胞胶原/斑点。

EN

回答 1

Stack Overflow用户

发布于 2020-03-22 05:13:07

我建议在Python/OpenCV中使用自适应阈值,如下所示:

代码语言:javascript
复制
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()

报告的结果:

代码语言:javascript
复制
Cells: 25
Individual cells: 1
Collonies: 1
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60792089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档