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
7 changes: 7 additions & 0 deletions plantcv/plantcv/threshold/threshold_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ def triangle(gray_img, object_type="light", xstep=1):
:param xstep: int
:return bin_img: numpy.ndarray
"""
# If the image is empty, return empty image
if np.count_nonzero(gray_img) == 0:
params.device += 1
bin_img = np.copy(gray_img)
_debug(visual=bin_img, filename=os.path.join(params.debug_outdir, f"{params.device}_triangle_empty.png"))
return bin_img

# Calculate automatic threshold value based on triangle algorithm
hist = cv2.calcHist([gray_img], [0], None, [256], [0, 255])

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_empty_mask_triangle():
"""Test for PlantCV."""
mask = np.zeros((100, 100), dtype=np.uint8)
fmask = triangle(mask)
assert np.sum(fmask) == 0