在下图中分离细胞边界,并使用python进行细胞核计数

2024-04-25 08:03:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用“分水岭”和“大津”进行阈值分割,但它只提取细胞核边界,我想分割细胞边界

我使用Otsu,然后通过打开去除噪声,识别确定背景,应用距离变换,使用它定义确定前景,定义未知,创建标记enter image description here

import cv2
import numpy as np
img = cv2.imread("images/bio_watershed/Osteosarcoma_01.tif")

cells=img[:,:,0]  
#Threshold image to binary using OTSU. ALl thresholded pixels will be set 
#to 255

ret1, thresh = cv2.threshold(cells, 0, 255, 
cv2.THRESH_BINARY+cv2.THRESH_OTSU)



# Morphological operations to remove small noise - opening

 kernel = np.ones((3,3),np.uint8)
 opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, 
 iterations = 2)

 # finding sure background

 sure_bg = cv2.dilate(opening,kernel,iterations=10)
 #applying dustance transform

 dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)

 ret2, sure_fg 
 =cv2.threshold(dist_transform,0.5*dist_transform.max(),255,0)


 # Unknown region 
 sure_fg = np.uint8(sure_fg)
 unknown = cv2.subtract(sure_bg,sure_fg)

 #Now we create a marker and label the regions inside. 

 ret3, markers = cv2.connectedComponents(sure_fg)


 #add 10 to all labels so that sure background is not 0, but 10

 markers = markers+10

 # Now, mark the region of unknown with zero

 markers[unknown==255] = 0

 #applying watershed 

 markers = cv2.watershed(img,markers)

 # color boundaries in yellow. 

 img[markers == -1] = [0,255,255]  

 img2 = color.label2rgb(markers, bg_label=0)

 cv2.imshow('Overlay on original image', img)
 cv2.imshow('Colored Cells', img2)
 cv2.waitKey(0)

通过运行这段代码,我得到了以下核边界分割,但我想得到细胞边界 nuclear segmentation

非常感谢你的帮助


Tags: toimgdistnptransformcv2kernelunknown
2条回答

您的示例非常适合基于颜色的分割(更好的分辨率将改善结果)

对比度已经足够好了(并且可以改进),所以在不使用OpenCV的情况下做了一个非常快速的测试(所以没有代码可以共享)

核边界: enter image description here

单元边界: enter image description here

合并: enter image description here

或作为单独的面具: enter image description here

所以我想说这一切都是关于delta E和适当的分割

我不确定您是否仍在寻找答案,但我已编辑了您的代码以分割单元格边界。您需要选择显示肌动蛋白丝的图像切片,它位于索引1中

我还使用了一个边缘检测器和轮廓图来勾勒细胞边界

这是我的密码:

import cv2
import numpy as np
import skimage.io as skio

img = cv2.imread("cells.png")

img_read = img[:,:,1] #Shows the actin filaments

#Denoising
img_denoise = cv2.fastNlMeansDenoising(img_read, 45, 7, 35)

#Canny edge detector
img_canny = cv2.Canny(img_denoise, 10, 200)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2,2))
img_dilate = cv2.dilate(img_canny, kernel, iterations = 2)

#Contour finding
contours, _ = cv2.findContours(img_dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
img_med = cv2.cvtColor(img_denoise, cv2.COLOR_GRAY2RGB)
img_final = cv2.drawContours(img_med, contours, -1, (0,128,128), 2, 4)

skio.imsave("img_output.tif", img_final)

cv2.imshow('Overlay on original image', img_final)
cv2.waitKey(0)

enter image description here

相关问题 更多 >