测量到中心线的距离#

一个常见的问题是如何确定点到分割对象中心的距离。为此,我们可以对对象进行骨架化,生成距离图,并在给定点处读取距离图的强度,以确定它们到骨架/中心线的距离。

另请参阅:

from skimage.io import imread
import napari_segment_blobs_and_things_with_membranes as nsbatwm
import napari_simpleitk_image_processing as nsitk
import numpy as np
import stackview
import pyclesperanto_prototype as cle

起点:二值图像#

我们从一个看起来像手臂的二值图像开始。

binary_arm = imread("../../data/binary_arm.tif")
stackview.insight(binary_arm)
shape(100, 100)
dtypeuint16
size19.5 kB
min0
max1

此外,我们继续使用X/Y格式的坐标列表:

coordinates_xy = np.asarray([
                  [70, 80],
                  [70, 70],
                  [70, 60]]).T

接下来,我们生成一个标签图像,其中给定的坐标被标记。列表中的第一个坐标(索引=0)将被标记为1,第二个坐标标记为2,依此类推。背景像素为0。 我们使用这个标签图像进行可视化,在下面,我们还将使用这个图像进行测量。

# draw the coordinates into an image; for visualization purposes
blank_image = cle.create((binary_arm.shape))
labeled_spots = coordinate_visualization = cle.pointlist_to_labelled_spots(coordinates_xy, blank_image)

# show the labeled pixels on top of the binary image
cle.imshow(binary_arm, continue_drawing=True, max_display_intensity=1)
cle.imshow(labeled_spots, labels=True, alpha=0.6)
../_images/1ad67129c206bdc4a56ed95e7952eae43e2a96ccac52a8df8f6b0f25095f2d17.png

预处理#

在我们可以对图像进行骨架化之前,我们需要填充白色区域中的黑色孔洞。

filled_holes = nsitk.binary_fill_holes(binary_arm)
filled_holes
n-sitk made image
shape(100, 100)
dtypeuint16
size19.5 kB
min0
max1

骨架化#

二值图像的骨架是白色区域中心的一条细线。

skeleton = nsbatwm.skeletonize(filled_holes)
skeleton
<__array_function__ internals>:200: RuntimeWarning: Converting input from bool to <class 'numpy.uint8'> for compatibility.
nsbatwm made image
shape(100, 100)
dtypebool
size9.8 kB
minFalse
maxTrue

距离图#

接下来,我们绘制一个有符号Maurer距离图。距离图是一种图像,其中像素的强度表示该像素到生成距离图的二值图像中最近的白色像素的距离。

distance_map = nsitk.signed_maurer_distance_map(skeleton)
distance_map
n-sitk made image
shape(100, 100)
dtypefloat32
size39.1 kB
min-68.35203
max0.0

使用stackview.picker,我们可以用鼠标悬停在图像上并读取强度。这只在类似Jupyter的环境中有效。

stackview.picker(distance_map, zoom_factor=3)

测量#

现在我们可以在标记点的给定位置读取距离图中的强度。

values_at_positions = cle.read_intensities_from_positions(coordinates_xy, distance_map)
np.asarray(values_at_positions)
array([[-15.033297 ,  -5.0990195,  -4.       ]], dtype=float32)

练习#

使用binary_arm图像的距离图来确定这三个点到手臂边缘的距离,而不是到中心线的距离。