组合动态标签/区域(Python,scikit-image)



我有一组 480 张原始图像和 480 个标签(每个原始图像一个(,这些标签已通过分水岭过程进行分割和标记。我使用标签,labels_ws,在寻找原始图像中各个区域的平均强度时,original_images .这些图像形成了一个时间序列,我希望跟踪这个时间序列的每个标记区域的平均强度。

使用以下代码在scikit图像中很容易找到单个图像中区域的平均强度:

regions = measure.regionprops(labels_ws, intensity_image = original_image)
print(["(%s, %s)" % (r, r.mean_intensity) for r in regions])

它打印了一大堆输出,如下所示:

'(skimage.measure._regionprops._RegionProperties 对象位于 0x000000000E5F3F98, 35.46153846153846(',

'(skimage.measure._regionprops._RegionProperties 对象位于 0x000000000E5F3FD0, 47.0(',

'(skimage.measure._regionprops._RegionProperties 对象位于 0x000000000E7B6048, 49.96666666666667(',

'(skimage.measure._regionprops._RegionProperties 对象位于 0x000000000E7B6080, 23.0(',

'(skimage.measure._regionprops._RegionProperties 对象位于 0x000000000E7B60B8, 32.1(',

每个图像可能有大约 100-150 个区域。这些区域是图像中在拍摄图像期间组织样本中存在神经元发光的区域。随着时间序列的进行,区域(神经元(以周期性的方式发光,因此每个区域的强度数据应该看起来像一个周期函数。

我遇到的问题是,在每个连续的图像中,标签/区域略有不同,因为每个区域中的发光都遵循其周期性行为。因此,标签/区域在时间序列的持续时间内"弹出/弹出"。我也不能保证,比方说,第一次发光时Region_1的大小与第二次或第三次发光时的大小相同(但是任何差异都很小,只有几个像素(。

综上所述,有没有办法以某种方式组合我的所有标签,形成一个我可以跟踪的标签?我是否应该以某种方式组合所有原始图像,然后创建一个主标签?我如何处理肯定会重叠但可能相差几个像素的形状/大小不同的区域?谢谢!

我遇到了类似的问题,我想跟踪随时间变化的分段区域。我的解决方案是更改每个分割区域中心点处每个图像中的所有标签。这具有将标签传播到所有其他图像的效果。

当然,这假设这些区域在整个过程中都停留在大致相同的位置

您可以在动画中看到差异:左侧的标签不断变化,右侧的标签保持一致。尽管缺少帧和移动区域,它仍然可以工作

动画链接:https://i.stack.imgur.com/0BIda.jpg (我没有足够的代表直接发布图像(

只需将细分和标记的图像列表发送给standardise_labels_timeline

def standardise_labels_timeline(images_list, start_at_end = True, count_offset = 1000):
    """
    Replace labels on similar images to allow tracking over time
    :param images_list: a list of segmented and lablled images as numpy arrays
    :param start_at_end: relabels the images beginning at the end of the list
    :param count_offset: an int greater than the total number of expected labels in a single image
    :returns: a list of relablled images as numpy arrays
    """
    import numpy as np
    images = list(images_list)
    if start_at_end:
        images.reverse()
    # Relabel all images to ensure there are no duplicates
    for image in images:
        for label in np.unique(image):
            if label > 0:
                count_offset += 1
                image[image == label] = count_offset
    # Ensure labels are propagated through image timeline
    for i, image in enumerate(images):
        labels = get_labelled_centers(image)
        # Apply labels to all subsequent images
        for j in range(i, len(images)):
            images[j] = replace_image_point_labels(images[j], labels)
    if start_at_end:
        images.reverse()
    return images

def get_labelled_centers(image):
    """
    Builds a list of labels and their centers
    :param image: a segmented and labelled image as a numpy array
    :returns: a list of label, co-ordinate tuples
    """
    from skimage.measure import regionprops
    # Find all labelled areas, disable caching so properties are only calculated if required
    rps = regionprops(image, cache = False)
    return [(r.label, r.centroid) for r in rps]

def replace_image_point_labels(image, labels):
    """
    Replace the labelled at a list of points with new labels
    :param image: a segmented and lablled image as a numpy array
    :param labels: a list of label, co-ordinate tuples
    :returns: a relabelled image as a numpy array
    """
    img = image.copy()
    for label, point in labels:
        row, col = point
        # Find the existing label at the point
        index = img[int(row), int(col)]
        # Replace the existing label with new, excluding background
        if index > 0:
            img[img == index] = label
    return img
--

编码:UTF-8 --

"" 创建于 %(日期(s

@author: %(Ahmed Islam ElManawy(s a.elmanawy_90@yahoo.com """>

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import cv2
from skimage.measure import label, regionprops
from sklearn.cluster import KMeans
import numpy as np
## import image
img=cv2.imread('E:\Data\Arabidopsis Thaliana HSI image\20170508\binarry\AQC_RT.jpg',1)
## lablelled image
label_image = label(img[:,:,0])
## combined image center using k-means
Center=[]
Box=[]
for region in regionprops(label_image):
    # take regions with large enough areas
    if region.area >= 10:
        # draw rectangle around segmented coins
        Box.append(region.bbox)
        Center.append(region.centroid)
Center=np.asarray(Center)
Box=np.asarray(Box)
kmeans=KMeans(n_clusters=12, random_state=0).fit(Center)
label=kmeans.labels_
## plot image with different area
fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(img)
for l in label:
    h=np.where(label==l)
    B=Box[h,:]
    B=B[0,:,:]
    minr, minc, maxr, maxc =np.min(B[:,0]), np.min(B[:,1]), np.max(B[:,2]), np.max(B[:,3])
#        plt.imshow(img2[11:88, 2:94,:])
    rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                                  fill=False, edgecolor='red', linewidth=2)
    ax.add_patch(rect)
ax.set_axis_off()
plt.tight_layout()
plt.show()

最新更新