0

I am attempting to threshold an image then label the objects.

This is what i have so far:

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))

Jack Ryan
  • 21
  • 1
  • 5

1 Answers1

1

If you only want the number of objects, you can use:

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]

You should not create image_label_overlay or image_label if you are not going to use them. If you use a Python-aware editor like Visual Studio Code with the Python extension (it will ask you which languages you want to install when you run it for the first time), or PyCharm, the code editor will warn you when some variables are not used.

Juan
  • 5,433
  • 21
  • 23