YaredTaddese / leaf-image-segmentation

Leaf Image Segmentation
MIT License
48 stars 19 forks source link

Crash when no leaf is present #4

Open EmmanuelMess opened 3 years ago

EmmanuelMess commented 3 years ago

Yuca plant root.

421035788

Traceback (most recent call last):
  File "leaf-image-segmentation/segment.py", line 163, in <module>
    segment_leaf(os.path.join(base_folder, file), filling_mode, smooth, args.marker_intensity)
  File "leaf-image-segmentation/segment.py", line 66, in segment_leaf
    select_largest_obj(bin_image, fill_mode=filling_mode,
  File "leaf-image-segmentation/background_marker.py", line 213, in select_largest_obj
    largest_obj_lab = np.argmax(lab_stats[1:, 4]) + 1
  File "<__array_function__ internals>", line 5, in argmax
  File "leaf-image-segmentation/venv/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 1188, in argmax
    return _wrapfunc(a, 'argmax', axis=axis, out=out)
  File "leaf-image-segmentation/venv/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 58, in _wrapfunc
    return bound(*args, **kwds)
ValueError: attempt to get argmax of an empty sequence
EmmanuelMess commented 3 years ago

Fixed by adding:

    if lab_stats[1:, 4].size == 0:
        return np.ones(img_bin.shape, dtype=np.uint8)

to line 207 of background_marker.py.

Put zeros instead if you want everything to be black.

lxfhfut commented 2 years ago

This issue is raised when the entire image has the same single label. My fix is to insert the following code to line 207 of background_marker.py:

if np.max(img_labeled) == np.min(img_labeled):
    largest_mask = np.zeros(img_bin.shape) + lab_val
    largest_mask[img_labeled == 0] = 0
    return largest_mask.astype(np.uint8)