首页 > 解决方案 > 尝试创建一个功能来标记图像

问题描述

我正在尝试对图像进行阈值处理,然后标记对象。

这是我到目前为止所拥有的:

from skimage.measure import label
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, square
from skimage.color import label2rgb

def segment (image, default= None):
    thresh = threshold_otsu(image)
    bw = closing(image > thresh, square(3))
    cleared = clear_border(bw)
    label_image = label(cleared)
    image_label_overlay = label2rgb(label_image, image=image)
    return (label (image))

标签: pythonimage-processingscikit-image

解决方案


如果您只想要对象的数量,您可以使用:

from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.morphology import closing, square
from scipy.ndimage import label

def segment(image, threshold=None):
    if threshold is None:
        threshold = threshold_otsu(image)
    bw = closing(image > threshold, square(3))
    cleared = clear_border(bw)
    return label(cleared)[1]

你不应该创建image_label_overlay或者image_label如果你不打算使用它们。如果您使用 Python 感知编辑器,例如带有 Python 扩展的 Visual Studio Code(它会在您第一次运行它时询问您要安装哪些语言)或 PyCharm,代码编辑器会在某些变量出现时警告您不曾用过。


推荐阅读