Skip to content

fix: make test command histogram logic shape-agnostic for OpenCV 5 - #1128

Open
faux123 wants to merge 1 commit into
boltgolt:masterfrom
faux123:fix/opencv5-calchist-shape
Open

fix: make test command histogram logic shape-agnostic for OpenCV 5#1128
faux123 wants to merge 1 commit into
boltgolt:masterfrom
faux123:fix/opencv5-calchist-shape

Conversation

@faux123

@faux123 faux123 commented Jul 10, 2026

Copy link
Copy Markdown

Bug

sudo howdy test crashes on systems with OpenCV 5.x:

  File "/usr/lib/howdy/cli.py", line 116, in <module>
    import cli.test
  File "/usr/lib/howdy/cli/test.py", line 131, in <module>
    hist_total = int(sum(hist)[0])
                     ~~~~~~~~~^^^
IndexError: invalid index to scalar variable.

Fixes #1127

Root cause

OpenCV 5.0 changed the Python binding of cv2.calcHist to return a 1-D array. With the 8-bin call in test.py it now returns shape (8,) where OpenCV 4.x returned (8, 1):

>>> import cv2, numpy
>>> cv2.__version__, numpy.__version__
('5.0.0', '2.5.1')
>>> f = numpy.random.randint(0, 255, (120, 160), dtype=numpy.uint8)
>>> cv2.calcHist([f], [0], None, [8], [0, 256]).shape
(8,)

With a 1-D array, built-in sum(hist) produces a numpy.float32 scalar instead of a length-1 array, so the [0] index raises IndexError. The subsequent value[0] in the bar-drawing loop fails the same way.

Fix

Flatten the histogram with ravel() and total it with hist.sum(), so the logic works with the array layout of every OpenCV version (4.x (8, 1), 5.x (8,)) instead of assuming either one. compare.py and add.py are unaffected: they already use np.sum(hist) and hist[0], which work with both shapes.

Environment where reproduced

Testing

  • Patched logic exercised against real cv2.calcHist output under OpenCV 5.0.0, and synthetically against both the (8, 1) and (8,) layouts.
  • Full howdy test loop (this exact patched file) run against a real IR camera for 30 frames on the system above: histogram bars computed, no errors, clean exit.
  • Interactive sudo howdy test confirmed working with the equivalent fix applied to the installed copy.

OpenCV 5.0 changed cv2.calcHist to return a 1-D array (shape (8,))
where 4.x returned shape (8, 1). This makes sum(hist) a numpy scalar,
so sum(hist)[0] in the test command raises
IndexError: invalid index to scalar variable.

Flatten the histogram with ravel() and use hist.sum() so the logic no
longer depends on the array layout of any particular OpenCV version.
compare.py and add.py already use np.sum(hist) and hist[0], which work
with both shapes.

Fixes boltgolt#1127
@Raymo111

Copy link
Copy Markdown

@boltgolt can we please merge

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.

Howdy test results in IndexError: invalid index to scalar variable

2 participants