略读:在大纲视图上使用距离条件对标签进行分组

2024-03-29 10:04:52 发布

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

我用过skimage.measure.label我想知道是否有一个函数或一个最好的方法来将标签与轮廓上的距离条件进行分组。在

目前我使用skimage.measure.regionprops分析每个标签然后skimage.segmentation.find_边界为了得到每个标签的轮廓,然后我得到坐标,我检查每个点之间的距离,如果距离符合条件,我更新标签,然后我重用regionprops以获得正确的标签分组后(我会很快发布我的代码)。在

当前使用此代码:

import math
import matplotlib.pyplot as plt
import numpy as np

from skimage.draw import ellipse
from skimage.measure import label, regionprops
from skimage.transform import rotate


image = np.zeros((600, 600))

rr, cc = ellipse(300, 350, 100, 220)
rr2, cc2 = ellipse(100, 100, 20, 50)
image[rr, cc] = 1
image[rr2, cc2] = 1

image = rotate(image, angle=15, order=0)

label_img = label(image)
regions = regionprops(label_img)

fig, ax = plt.subplots()
ax.imshow(image, cmap=plt.cm.gray)

for props in regions:
    y0, x0 = props.centroid
    orientation = props.orientation
    x1 = x0 + math.cos(orientation) * 0.5 * props.major_axis_length
    y1 = y0 - math.sin(orientation) * 0.5 * props.major_axis_length
    x2 = x0 - math.sin(orientation) * 0.5 * props.minor_axis_length
    y2 = y0 - math.cos(orientation) * 0.5 * props.minor_axis_length

    ax.plot((x0, x1), (y0, y1), '-r', linewidth=2.5)
    ax.plot((x0, x2), (y0, y2), '-r', linewidth=2.5)
    ax.plot(x0, y0, '.g', markersize=15)

    minr, minc, maxr, maxc = props.bbox
    bx = (minc, maxc, maxc, minc, minc)
    by = (minr, minr, maxr, maxr, minr)
    ax.plot(bx, by, '-b', linewidth=2.5)

ax.axis((0, 600, 600, 0))
plt.show()

Tags: imageimportpltmath标签axpropslength
1条回答
网友
1楼 · 发布于 2024-03-29 10:04:52

此代码用于分组并显示结果(蓝色在前面,青色在后面):

label grouping

import math
import matplotlib.pyplot as plt
import numpy as np

from skimage.draw import ellipse
from skimage.measure import label, regionprops
from skimage.transform import rotate
from skimage.segmentation import find_boundaries

image = np.zeros((600, 600))

rr, cc = ellipse(300, 350, 100, 220)
rr2, cc2 = ellipse(100, 100, 20, 50)
rr3, cc3 = ellipse(200, 50, 20, 50)
image[rr, cc] = 1
image[rr2, cc2] = 1
image[rr3, cc3] = 1

image = rotate(image, angle=15, order=0)

pixel_distance = 100

# Labeling image
label_img = label(image)

# Applying regionprops
regions = regionprops(label_img)

# First draw before regroup with blue rectangles
fig, ax = plt.subplots()
ax.imshow(image, interpolation="none")

for props in regions:
    y0, x0 = props.centroid
    orientation = props.orientation
    x1 = x0 + math.cos(orientation) * 0.5 * props.major_axis_length
    y1 = y0 - math.sin(orientation) * 0.5 * props.major_axis_length
    x2 = x0 - math.sin(orientation) * 0.5 * props.minor_axis_length
    y2 = y0 - math.cos(orientation) * 0.5 * props.minor_axis_length

    ax.plot((x0, x1), (y0, y1), '-r', linewidth=2.5)
    ax.plot((x0, x2), (y0, y2), '-r', linewidth=2.5)
    ax.plot(x0, y0, '.g', markersize=15)

    minr, minc, maxr, maxc = props.bbox
    bx = (minc, maxc, maxc, minc, minc)
    by = (minr, minr, maxr, maxr, minr)
    ax.plot(bx, by, '-b', linewidth=2.5)

# Check distances for each label
for props in regions:

    # Get boundaries coordinates for that label
    label_boundaries = find_boundaries(label_img == props.label)
    bound_cord = np.column_stack(np.where(label_boundaries))

    # DEBUG: Draw boundaries
    # for cord in bound_cord:
    #     ax.plot(cord[1], cord[0], '.b', markersize=5)

    # We will compare each boundaries coordinates with the coordinates of all other label boundaries
    for j_props in regions:
        if j_props.label > props.label:
            regrouped = False
            print "LABEL :", props.label, "VS :", j_props.label
            # Get boundaries coordinates for that label
            j_label_boundaries = find_boundaries(label_img == j_props.label)
            j_bound_cord = np.column_stack(np.where(j_label_boundaries))

            # Coordinates comparisons
            i = 0
            while not regrouped and i < len(bound_cord):
                j = 0
                while not regrouped and j < len(j_bound_cord):
                    # Apply distance condition
                    if math.hypot(j_bound_cord[j][1] - bound_cord[i][1], j_bound_cord[j][0] - bound_cord[i][0]) <= pixel_distance:
                        # Assign the less label value
                        label_img[label_img == j_props.label] = min(props.label, j_props.label)
                        j_props.label = min(props.label, j_props.label)
                        regrouped = True
                    j += 1
                i += 1

# Second time we use regionprobs to get new labels informations
regions_2 = regionprops(label_img)

# Redraw after grouping with cyan rectangles
for props in regions_2:
    y0, x0 = props.centroid
    orientation = props.orientation
    x1 = x0 + math.cos(orientation) * 0.5 * props.major_axis_length
    y1 = y0 - math.sin(orientation) * 0.5 * props.major_axis_length
    x2 = x0 - math.sin(orientation) * 0.5 * props.minor_axis_length
    y2 = y0 - math.cos(orientation) * 0.5 * props.minor_axis_length

    ax.plot((x0, x1), (y0, y1), '-y', linewidth=2.5)
    ax.plot((x0, x2), (y0, y2), '-y', linewidth=2.5)
    ax.plot(x0, y0, '.c', markersize=15)

    minr, minc, maxr, maxc = props.bbox
    bx = (minc, maxc, maxc, minc, minc)
    by = (minr, minr, maxr, maxr, minr)
    ax.plot(bx, by, '-c', linewidth=2.5)

ax.axis((0, 600, 600, 0))
plt.show()

相关问题 更多 >