如何计算细胞核的数量?

我使用 Python 3.5和OpenCV 3来分析生物学中的细胞图片.我的照片看起来像这样:

我希望能够计算出细胞核面积与整个细胞面积之比.

在我的载玻片中,细胞核是深紫色,细胞的其他区域是浅蓝色.还有棕褐色的红细胞,我想完全忽略它.为清楚起见,这是标记图像:

如何使用图像分割来识别和测量我感兴趣的区域?

我试过按照this guide,但它返回一个完全黑色的图像.

解决方法

首先,我们将在下面使用一些初步代码
import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage.morphology import extrema
from skimage.morphology import watershed as skwater

def ShowImage(title,img,ctype):
  if ctype=='bgr':
    b,g,r = cv2.split(img)       # get b,r
    rgb_img = cv2.merge([r,b])     # switch it to rgb
    plt.imshow(rgb_img)
  elif ctype=='hsv':
    rgb = cv2.cvtColor(img,cv2.COLOR_HSV2RGB)
    plt.imshow(rgb)
  elif ctype=='gray':
    plt.imshow(img,cmap='gray')
  elif ctype=='rgb':
    plt.imshow(img)
  else:
    raise Exception("UnkNown colour type")
  plt.title(title)
  plt.show()

作为参考,这是您的原始图像:

#Read in image
img         = cv2.imread('cells.jpg')
ShowImage('Original','bgr')

链接文章建议使用Otsu’s method进行颜色分割.该方法假设可以将图像的像素强度绘制成双峰直方图,并找到该直方图的最佳分离器.我应用以下方法.

#Convert to a single,grayscale channel
gray        = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#Threshold the image to binary using Otsu's method
ret,thresh = cv2.threshold(gray,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
ShowImage('Grayscale',gray,'gray')
ShowImage('Applying Otsu',thresh,'gray')


图像的二进制形式不是那么好!观察灰度图像,您可以看到原因:Otsu转换产生三类像素:深色背景像素,甜甜圈细胞和细胞内部以及细胞核.下面的直方图说明了这一点:

#Make a histogram of the intensities in the grayscale image
plt.hist(gray.ravel(),256)
plt.show()

因此,您已经打破了所使用的算法的假设,因此您得到的结果并不出人意料.通过丢弃颜色信息,我们失去了区分甜甜圈和细胞内部的能力.

解决此问题的一种方法是基于颜色阈值处理来执行分割.为此,您需要选择一个可用的色彩空间.This guide具有对不同空间的出色图像描述.

我们选择HSV.这具有以下优点:单个通道H描述图像的颜色.一旦我们将图像转换为这个空间,我们就可以找到我们感兴趣的颜色的边界.例如,要找到细胞的细胞核,我们可以按如下方式进行:

cell_hsvmin  = (110,40,145)  #Lower end of the HSV range defining the nuclei
cell_hsvmax  = (150,190,255) #Upper end of the HSV range defining the nuclei
#Transform image to HSV color space
hsv          = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) 
#Threshold based on HSV values
color_thresh = cv2.inRange(hsv,cell_hsvmin,cell_hsvmax) 
ShowImage('Color Threshold',color_thresh,'gray')

masked = cv2.bitwise_and(img,mask=color_thresh)
ShowImage('Color Threshold Maksed',masked,'bgr')


这看起来好多了!虽然,注意到细胞内部的某些部分被标记为核,即使它们不应该.人们还可以说它不是很自动:你还需要仔细挑选你的颜色.在HSV空间中操作消除了大量的猜测,但也许我们可以使用有四种不同颜色的事实来消除对范围的需求!为此,我们将HSV像素传递到k-means clustering algorithm.

#Convert pixel space to an array of triplets. These are vectors in 3-space.
Z = hsv.reshape((-1,3)) 
#Convert to floating point
Z = np.float32(Z)
#Define the K-means criteria,these are not too important
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,10,1.0)
#Define the number of clusters to find
K = 4
#Perform the k-means transformation. What we get back are:
#*Centers: The coordinates at the center of each 3-space cluster
#*Labels: Numeric labels for each cluster
#*Ret: A return code indicating whether the algorithm converged,&c.
ret,label,center = cv2.kmeans(Z,K,None,criteria,cv2.KMEANS_RANDOM_CENTERS)

#Produce an image using only the center colours of the clusters
center = np.uint8(center)
khsv   = center[label.flatten()]
khsv   = khsv.reshape((img.shape))
ShowImage('K-means',khsv,'hsv')

#Reshape labels for masking
label = label.reshape(img.shape[0:2])
ShowImage('K-means Labels','gray')


请注意,这样做可以很好地分离颜色而无需手动指定! (除了指定簇数之外.)

现在,我们需要确定哪些标签对应于单元格的哪些部分.

为此,我们找到两个像素的颜色:一个明显是核像素,一个明显是细胞像素.然后我们找出哪个聚类中心最接近这些像素.

#(distance,Label) pairs
nucleus_colour = np.array([139,106,192])
cell_colour    = np.array([130,41,207])
nuclei_label  = (np.inf,-1)
cell_label    = (np.inf,-1)
for l,c in enumerate(center):
  print(l,c)
  dist_nuc = np.sum(np.square(c-nucleus_colour)) #Euclidean distance between colours
  if dist_nuc<nuclei_label[0]:
        nuclei_label=(dist_nuc,l)
  dist_cell = np.sum(np.square(c-cell_colour)) #Euclidean distance between colours
  if dist_cell<cell_label[0]:
        cell_label=(dist_cell,l)
nuclei_label = nuclei_label[1]
cell_label   = cell_label[1]
print("Nuclei label={0},cell label={1}".format(nuclei_label,cell_label))

现在,让我们构建我们需要的二进制分类器来识别分水岭算法的整个单元:

#Multiply by 1 to keep image in an integer format suitable for OpenCV
thresh = cv2.bitwise_or(1*(label==nuclei_label),1*(label==cell_label))
thresh = np.uint8(thresh)
ShowImage('Binary','gray')

我们现在可以消除单像素噪声:

#Remove noise by eliminating single-pixel patches
kernel  = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel,iterations = 2)
ShowImage('opening',opening,'gray')

我们现在需要确定流域的峰值并给它们单独的标签.这样做的目的是生成一组像素,使得每个核单元在其中具有像素,并且没有两个核具有其标识符像素接触.

为了实现这一点,我们可以进行距离变换,然后滤除远离核细胞中心两个距离.

但是,我们必须要小心,因为具有高阈值的狭长细胞可能会完全消失.在下图中,我们将右下方接触的两个细胞分开,但完全消除了右上方的长而细的细胞.

#Identify areas which are surely foreground
fraction_foreground = 0.75
dist         = cv2.distanceTransform(opening,cv2.disT_L2,5)
ret,sure_fg = cv2.threshold(dist,fraction_foreground*dist.max(),0)
ShowImage('distance',dist_transform,'gray')
ShowImage('Surely Foreground',sure_fg,'gray')


降低阈值会使长而窄的细胞返回,但会使细胞在右下方连接.

我们可以通过使用识别每个局部区域中的峰值的自适应方法解决这个问题.这消除了为我们的阈值设置单个全局常量的需要.为此,我们使用h_axima函数,该函数返回大于指定截止值的所有局部最大值.这与距离函数形成对比,距离函数返回大于给定值的所有像素.

#Identify areas which are surely foreground
h_fraction = 0.1
dist     = cv2.distanceTransform(opening,5)
maxima   = extrema.h_maxima(dist,h_fraction*dist.max())
print("Peaks found: {0}".format(np.sum(maxima)))
#Dilate the maxima so we can see them
maxima   = cv2.dilate(maxima,iterations=2)
ShowImage('distance',maxima,'gray')


现在我们通过减去最大值来识别未知区域,即将由分水岭算法标记的区域:

# Finding unkNown region
unkNown = cv2.subtract(opening,maxima)
ShowImage('UnkNown',unkNown,'gray')

接下来,我们给出每个最大值唯一标签,然后在最终执行分水岭变换之前标记未知区域:

# Marker labelling
ret,markers = cv2.connectedComponents(maxima)
ShowImage('Connected Components',markers,'rgb')

# Add one to all labels so that sure background is not 0,but 1
markers = markers+1

# Now,mark the region of unkNown with zero
markers[unkNown==np.max(unkNown)] = 0

ShowImage('markers','rgb')

dist    = cv2.distanceTransform(opening,5)
markers = skwater(-dist,watershed_line=True)

ShowImage('watershed','rgb')
imgout = img.copy()
imgout[markers == 0] = [0,255] #Label the watershed_line

ShowImage('img',imgout,'bgr')




这给了我们一组代表细胞的标记区域.接下来,我们遍历这些区域,将它们用作标记数据的掩码,并计算分数:

for l in np.unique(markers):
    if l==0:      #watershed line
        continue
    if l==1:      #Background
        continue
    #For displaying individual cells
    #temp=khsv.copy()
    #temp[markers!=l]=0
    #ShowImage('out',temp,'hsv')
    temp = label.copy()
    temp[markers!=l]=-1
    nucleus_area = np.sum(temp==nuclei_label)
    cell_area    = np.sum(temp==cell_label)
    print("Nucleus fraction for cell {0} is {1}".format(l,nucleus_area/(cell_area+nucleus_area)))

这给出了:

Nucleus fraction for cell 2 is 0.9002795899347623
Nucleus fraction for cell 3 is 0.7953321364452424
Nucleus fraction for cell 4 is 0.7525925925925926
Nucleus fraction for cell 5 is 0.8151515151515152
Nucleus fraction for cell 6 is 0.6808656818962556
Nucleus fraction for cell 7 is 0.8276481149012568
Nucleus fraction for cell 8 is 0.878500237304224
Nucleus fraction for cell 9 is 0.8342518016108521
Nucleus fraction for cell 10 is 0.9742324561403509
Nucleus fraction for cell 11 is 0.8728733459357277
Nucleus fraction for cell 12 is 0.7968570333461096
Nucleus fraction for cell 13 is 0.8226831716293075
Nucleus fraction for cell 14 is 0.7491039426523297
Nucleus fraction for cell 15 is 0.839096357768557
Nucleus fraction for cell 16 is 0.7589670014347202
Nucleus fraction for cell 17 is 0.8559168925022583
Nucleus fraction for cell 18 is 0.7534142640364189
Nucleus fraction for cell 19 is 0.8036734693877551
Nucleus fraction for cell 20 is 0.7566037735849057

(请注意,如果您将其用于学术目的,学术诚信需要正确归属.有关详细信息,请与我联系.)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


我最近重新拾起了计算机视觉,借助Python的opencv还有face_recognition库写了个简单的图像识别demo,额外定制了一些内容,原本想打包成exe然后发给朋友,不过在这当中遇到了许多小问题,都解决了,记录一下踩过的坑。 1、Pyinstaller打包过程当中出现warning,跟d
说到Pooling,相信学习过CNN的朋友们都不会感到陌生。Pooling在中文当中的意思是“池化”,在神经网络当中非常常见,通常用的比较多的一种是Max Pooling,具体操作如下图: 结合图像理解,相信你也会大概明白其中的本意。不过Pooling并不是只可以选取2x2的窗口大小,即便是3x3,
记得大一学Python的时候,有一个题目是判断一个数是否是复数。当时觉得比较复杂不好写,就琢磨了一个偷懒的好办法,用异常处理的手段便可以大大程度帮助你简短代码(偷懒)。以下是判断整数和复数的两段小代码: 相信看到这里,你也有所顿悟,能拓展出更多有意思的方法~
文章目录 3 直方图Histogramplot1. 基本直方图的绘制 Basic histogram2. 数据分布与密度信息显示 Control rug and density on seaborn histogram3. 带箱形图的直方图 Histogram with a boxplot on t
文章目录 5 小提琴图Violinplot1. 基础小提琴图绘制 Basic violinplot2. 小提琴图样式自定义 Custom seaborn violinplot3. 小提琴图颜色自定义 Control color of seaborn violinplot4. 分组小提琴图 Group
文章目录 4 核密度图Densityplot1. 基础核密度图绘制 Basic density plot2. 核密度图的区间控制 Control bandwidth of density plot3. 多个变量的核密度图绘制 Density plot of several variables4. 边
首先 import tensorflow as tf tf.argmax(tenso,n)函数会返回tensor中参数指定的维度中的最大值的索引或者向量。当tensor为矩阵返回向量,tensor为向量返回索引号。其中n表示具体参数的维度。 以实际例子为说明: import tensorflow a
seaborn学习笔记章节 seaborn是一个基于matplotlib的Python数据可视化库。seaborn是matplotlib的高级封装,可以绘制有吸引力且信息丰富的统计图形。相对于matplotlib,seaborn语法更简洁,两者关系类似于numpy和pandas之间的关系,seabo
Python ConfigParser教程显示了如何使用ConfigParser在Python中使用配置文件。 文章目录 1 介绍1.1 Python ConfigParser读取文件1.2 Python ConfigParser中的节1.3 Python ConfigParser从字符串中读取数据
1. 处理Excel 电子表格笔记(第12章)(代码下载) 本文主要介绍openpyxl 的2.5.12版处理excel电子表格,原书是2.1.4 版,OpenPyXL 团队会经常发布新版本。不过不用担心,新版本应该在相当长的时间内向后兼容。如果你有新版本,想看看它提供了什么新功能,可以查看Open
1. 发送电子邮件和短信笔记(第16章)(代码下载) 1.1 发送电子邮件 简单邮件传输协议(SMTP)是用于发送电子邮件的协议。SMTP 规定电子邮件应该如何格式化、加密、在邮件服务器之间传递,以及在你点击发送后,计算机要处理的所有其他细节。。但是,你并不需要知道这些技术细节,因为Python 的
文章目录 12 绘图实例(4) Drawing example(4)1. Scatterplot with varying point sizes and hues(relplot)2. Scatterplot with categorical variables(swarmplot)3. Scat
文章目录 10 绘图实例(2) Drawing example(2)1. Grouped violinplots with split violins(violinplot)2. Annotated heatmaps(heatmap)3. Hexbin plot with marginal dist
文章目录 9 绘图实例(1) Drawing example(1)1. Anscombe’s quartet(lmplot)2. Color palette choices(barplot)3. Different cubehelix palettes(kdeplot)4. Distribution
Python装饰器教程展示了如何在Python中使用装饰器基本功能。 文章目录 1 使用教程1.1 Python装饰器简单示例1.2 带@符号的Python装饰器1.3 用参数修饰函数1.4 Python装饰器修改数据1.5 Python多层装饰器1.6 Python装饰器计时示例 2 参考 1 使
1. 用GUI 自动化控制键盘和鼠标第18章 (代码下载) pyautogui模块可以向Windows、OS X 和Linux 发送虚拟按键和鼠标点击。根据使用的操作系统,在安装pyautogui之前,可能需要安装一些其他模块。 Windows: 不需要安装其他模块。OS X: sudo pip3
文章目录 生成文件目录结构多图合并找出文件夹中相似图像 生成文件目录结构 生成文件夹或文件的目录结构,并保存结果。可选是否滤除目录,特定文件以及可以设定最大查找文件结构深度。效果如下: root:[z:/] |--a.py |--image | |--cat1.jpg | |--cat2.jpg |
文章目录 VENN DIAGRAM(维恩图)1. 具有2个分组的基本的维恩图 Venn diagram with 2 groups2. 具有3个组的基本维恩图 Venn diagram with 3 groups3. 自定义维恩图 Custom Venn diagram4. 精致的维恩图 Elabo
mxnet60分钟入门Gluon教程代码下载,适合做过深度学习的人使用。入门教程地址: https://beta.mxnet.io/guide/getting-started/crash-course/index.html mxnet安装方法:pip install mxnet 1 在mxnet中使
文章目录 1 安装2 快速入门2.1 基本用法2.2 输出图像格式2.3 图像style设置2.4 属性2.5 子图和聚类 3 实例4 如何进一步使用python graphviz Graphviz是一款能够自动排版的流程图绘图软件。python graphviz则是graphviz的python实