我有一个Z叠的2D共焦显微图像(2D切片),我想分割细胞。二维图像的Z-堆栈实际上是一个三维数据。在Z轴的不同切片中,我看到相同的细胞确实出现在多个切片中.我对XY的细胞形状感兴趣,所以我想从不同的Z轴切片中保留最大的细胞面积。我想在将连续的2D切片转换成标签为二进制图像之后,将它们组合在一起,但是我没有什么问题,我需要一些帮助才能继续下去。
我有两张图片img_a和img_b。我首先使用OTSU将它们转换成二值图像,然后应用形态学操作,然后使用cv2.connectedComponentsWithStats()获取标记对象。在给图像贴上标签之后,我用cv2.bitwise_or()将它们组合在一起,但这会使标签变得混乱。您可以在附加的处理图像(由红色圆圈高亮的单元格)中看到这一点。我看到了重叠单元格的多个标签。但是,我想为每个组合的重叠对象指定一个唯一的标签。
最后,我想要的是,当我组合两个标记图像时,我希望为合并的重叠对象分配一个标签(一个唯一值),并通过合并这两个图像来保持最大的单元格面积。有人知道怎么做吗?
以下是代码:
from matplotlib import pyplot as plt
from skimage import io, color, measure
from skimage.util import img_as_ubyte
from skimage.segmentation import clear_border
import cv2
import numpy as np
cells_a=img_a[:,:,1] # get the green channel
#Threshold image to binary using OTSU.
ret_a, thresh_a = cv2.threshold(cells_a, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Morphological operations to remove small noise - opening
kernel = np.ones((3,3),np.uint8)
opening_a = cv2.morphologyEx(thresh_a,cv2.MORPH_OPEN,kernel, iterations = 2)
opening_a = clear_border(opening_a) #Remove edge touchingpixels
numlabels_a, labels_a, stats_a, centroids_a = cv2.connectedComponentsWithStats(opening_a)
img_a1 = color.label2rgb(labels_a, bg_label=0)
## now do the same with image_b
cells_b=img_b[:,:,1] # get the green channel
#Threshold image to binary using OTSU.
ret_b, thresh_b = cv2.threshold(cells_b, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Morphological operations to remove small noise - opening
opening_b = cv2.morphologyEx(thresh_b,cv2.MORPH_OPEN,kernel, iterations = 2)
opening_b = clear_border(opening_b) #Remove edge touchingpixels
numlabels_b, labels_b, stats_b, centroids_b = cv2.connectedComponentsWithStats(opening_b)
img_b1 = color.label2rgb(labels_b, bg_label=0)
## Now combined two images
combined = cv2.bitwise_or(labels_a, labels_b) ## combined both labelled images to get maximum area per cell
combined_img = color.label2rgb(combined, bg_label=0)
plt.imshow(combined_img)图像可以找到这里
发布于 2022-01-25 11:41:33
基于Christoph和beaker的评论,我开始四处寻找3D连接组件标签。我找到了一个python库,它可以处理这样的事情,我安装了它,并尝试了一下。似乎做得很好。它确实在每个切片中分配标签,并为不同切片中的相同单元保持相同的标签。这正是我想要的。
下面是指向用于在3D中标记对象的库的链接。https://pypi.org/project/connected-components-3d/
https://stackoverflow.com/questions/70822638
复制相似问题