pandas skimage.measure.regionprops标签的相应测量值?

b4wnujal  于 2023-02-02  发布在  其他
关注(0)|答案(1)|浏览(128)

我使用scikit库分析了缺陷的面积和平均直径。下面是代码和相应的分割区域。

import cv2
import numpy as np
from matplotlib import pyplot as plt
from skimage import measure, io, img_as_ubyte
from skimage.color import label2rgb, rgb2gray

img = cv2.imread("F:\py_image_pro\pore.jpg", 0)

scale = 0.086 #1 pixel in microns

from skimage.filters import threshold_otsu
threshold = threshold_otsu(img)

thresholded_img = img < threshold
#plt.imshow(thresholded_img, cmap='gray')
#plt.show()
from skimage.segmentation import clear_border
edge_touching_removed = clear_border(thresholded_img)

label_image = measure.label(edge_touching_removed, connectivity=img.ndim)
#plt.imshow(label_image)
#plt.show()

image_label_overlay = label2rgb(label_image, image=img)
plt.imshow(image_label_overlay)
plt.show()

props = measure.regionprops_table(label_image, img, properties=['label', 'area', 'equivalent_diameter', 'mean_intensity', 'solidity'])

import pandas as pd
df = pd.DataFrame(props)

df = df[df['area'] > 20]

df['area_in_microns'] = df['area'] * (scale**2)
df['equivalent_diameter_microns'] = df['equivalent_diameter'] * (scale)
print(df.head())

使用regionprops测量分割区域。Segmented image我想知道是否有任何方法可以在输出图像中显示标签,以便了解分割标签的相应测量值?

rslzwgfq

rslzwgfq1#

你是在问你是否可以在图像的顶部显示一个颜色Map版本的测量结果吗?如果可以,答案是肯定的!你可以使用skimage.util.map_array来实现这一点。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from skimage import (
        color, data, filters, measure,
        morphology, segmentation, util
        )

# grab the image
coins = data.coins()

# segment the image; from:
# https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_expand_labels.html
edges = filters.farid(coins)
markers = np.zeros_like(coins)
markers[coins < 30] = 1
markers[coins > 150] = 2
watershed = segmentation.watershed(edges, markers)
segmented_raw = measure.label(watershed == 2)
# remove tiny background objects due to noise
segmented = morphology.remove_small_objects(segmented_raw, 64)

# measure regionprops
table = pd.DataFrame(measure.regionprops_table(
        segmented, coins, properties=('label', 'area')
        ))

# map the labels to measured properties
colored_by_area = util.map_array(
        segmented,
        np.asarray(table['label']),
        np.asarray(table['area']).astype(float),
        )
# set 0 to nan, so it appears as transparent in pyplot.imshow
colored_by_area[colored_by_area==0] = np.nan

# display the results
fig, axes = plt.subplots(1, 2, sharex=True, sharey=True)
colored_by_label = color.label2rgb(segmented, image=coins, bg_label=0)
axes[0].imshow(colored_by_label)
axes[0].set_axis_off()
axes[0].set_title('segmentation')
axes[1].imshow(coins, cmap='gray')
axim = axes[1].imshow(colored_by_area, cmap='viridis')
axes[1].set_axis_off()
axes[1].set_title('segment area')
plt.colorbar(axim, ax=axes[1], fraction=0.05, label='area (px)')

plt.show()

相关问题