Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions plantcv/plantcv/threshold/threshold_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ def otsu(gray_img, object_type="light"):

params.device += 1

# If the grayscale image is all zeros, return copy of input image.
if np.count_nonzero(gray_img) == 0:
bin_img = np.copy(gray_img)
_debug(visual=bin_img, filename=os.path.join(params.debug_outdir, f"{params.device}_otsu_empty.png"))
return bin_img

# Threshold the image
bin_img = _call_threshold(gray_img, 0, threshold_method, "_otsu_threshold_")

Expand Down
7 changes: 7 additions & 0 deletions tests/plantcv/threshold/test_threshold_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,10 @@ def test_dual_channels_bad_channel():
pts = [(0, 0), (255, 255)]
with pytest.raises(RuntimeError):
_ = dual_channels(img, x_channel='wrong_ch', y_channel='index', points=pts, above=True)

def test_otsu_empty_mask():
"""Test for PlantCV."""
mask = np.zeros((100, 100))
fmask = otsu(mask)
assert np.sum(fmask) == 0