fix: make test command histogram logic shape-agnostic for OpenCV 5 - #1128
Open
faux123 wants to merge 1 commit into
Open
fix: make test command histogram logic shape-agnostic for OpenCV 5#1128faux123 wants to merge 1 commit into
faux123 wants to merge 1 commit into
Conversation
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
This was referenced Jul 27, 2026
|
@boltgolt can we please merge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
sudo howdy testcrashes on systems with OpenCV 5.x:Fixes #1127
Root cause
OpenCV 5.0 changed the Python binding of
cv2.calcHistto return a 1-D array. With the 8-bin call intest.pyit now returns shape(8,)where OpenCV 4.x returned(8, 1):With a 1-D array, built-in
sum(hist)produces anumpy.float32scalar instead of a length-1 array, so the[0]index raisesIndexError. The subsequentvalue[0]in the bar-drawing loop fails the same way.Fix
Flatten the histogram with
ravel()and total it withhist.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.pyandadd.pyare unaffected: they already usenp.sum(hist)andhist[0], which work with both shapes.Environment where reproduced
howdy-git)Testing
cv2.calcHistoutput under OpenCV 5.0.0, and synthetically against both the(8, 1)and(8,)layouts.howdy testloop (this exact patched file) run against a real IR camera for 30 frames on the system above: histogram bars computed, no errors, clean exit.sudo howdy testconfirmed working with the equivalent fix applied to the installed copy.