Skip to content

Fix divide-by-zero RuntimeWarning in PeelColor.get_green_yellow_values#66

Open
loren-honaas wants to merge 4 commits into
SystemsGenetics:devfrom
loren-honaas:fix-peelcolor-divide-by-zero
Open

Fix divide-by-zero RuntimeWarning in PeelColor.get_green_yellow_values#66
loren-honaas wants to merge 4 commits into
SystemsGenetics:devfrom
loren-honaas:fix-peelcolor-divide-by-zero

Conversation

@loren-honaas
Copy link
Copy Markdown

Fix divide-by-zero RuntimeWarning in PeelColor.get_green_yellow_values

Problem

When processing images where no pixels pass the LAB color thresholds, get_green_yellow_values raises a RuntimeWarning: invalid value encountered in divide on lines 135-137:

mean_l = np.sum(lab_img[:, :, 0]) / np.count_nonzero(lab_img[:, :, 0]) * 100 / 255
mean_a = np.sum(lab_img[:, :, 1]) / np.count_nonzero(lab_img[:, :, 1]) - 128
mean_b = np.sum(lab_img[:, :, 2]) / np.count_nonzero(lab_img[:, :, 2]) - 128

This occurs when th123 — the combined LAB threshold mask — excludes all pixels, causing np.count_nonzero to return 0. The resulting nan values propagate silently through the downstream spherical coordinate normalization, producing meaningless scores.

This was observed when running the color analysis on Granny-segmented images of blush apples. Sun-side (blush-facing) images have no green/yellow pixels and correctly produce an empty th123 mask — but instead of being flagged, they were silently assigned bin=1 with no score, which is misleading.

A secondary issue is that each channel used its own independent np.count_nonzero, meaning the three means could in principle be computed over different pixel counts — this is inconsistent.

Fix

Compute a single shared pixel_count = np.count_nonzero(th123) and use it as the denominator for all three channels. Add an explicit guard that returns (nan, nan, nan) when no pixels pass the threshold, making it clear in the output that the image could not be scored rather than propagating silent errors.

# get mean values from each channel
pixel_count = np.count_nonzero(th123)
if pixel_count == 0:
    return (float('nan'), float('nan'), float('nan'))

mean_l = np.sum(lab_img[:, :, 0]) / pixel_count * 100 / 255
mean_a = np.sum(lab_img[:, :, 1]) / pixel_count - 128
mean_b = np.sum(lab_img[:, :, 2]) / pixel_count - 128

Impact

  • Eliminates the RuntimeWarning entirely
  • Ensures consistent pixel count across all three LAB channels
  • Images with no scoreable pixels now return nan explicitly rather than silently producing invalid results
  • No change to scores for images that previously worked correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant