imgproc: fix calcBackProject() ignoring the 3rd and further channels - #29941
Open
ShresthSamyak wants to merge 1 commit into
Open
imgproc: fix calcBackProject() ignoring the 3rd and further channels#29941ShresthSamyak wants to merge 1 commit into
ShresthSamyak wants to merge 1 commit into
Conversation
The std::vector<> overload of calcBackProject() rebuilds a multi-channel histogram into an equivalent Mat carrying one extra dimension (H), so that a 3D histogram arriving from the bindings as a 2D multi-channel Mat is interpreted correctly. H was used for the argument checks, but the original `hist` was forwarded to the implementation, which then saw dims == 2 and looked values up using only the first two channels. Forward H instead. For single-channel histograms H aliases hist, so the behaviour there is unchanged. Fixes opencv#29938
There was a problem hiding this comment.
🟢 Approval recommended
The fix is narrowly scoped and covered by regression tests.
Pull request overview
Fixes 3D histogram back-projection by forwarding the corrected multidimensional histogram.
Changes:
- Corrects
calcBackProject()handling of multi-channel histograms. - Adds C++ and Python regression tests for issue #29938.
File summaries
| File | Description |
|---|---|
modules/imgproc/test/test_histograms.cpp |
Adds C++ 3D regression coverage. |
modules/imgproc/src/histogram.cpp |
Forwards the corrected histogram representation. |
modules/imgproc/misc/python/test/test_imgproc.py |
Adds Python reproduction coverage. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Fixes #29938
Problem
cv2.calcBackProject()returns the same value for pixels that differ only inthe 3rd channel, so a 3D (BGR) back-projection is wrong. From the issue:
Cause
Not in the back-projection kernel:
calcBackProj_8u()handlesdims == 3correctly (
tab[*p0] + tab[*p1 + 256] + tab[*p2 + 512]). That branch is simplynever reached.
A
(8, 8, 8)ndarray is converted to an 8x8CV_32FC8Mat, not a3-dimensional one (
cv2_convert.cpp,ismultichannel = ndims == 3). Thestd::vector<>overload ofcalcBackProject()already compensates: whenhcn > 1it rebuilds the histogram asH, carrying the extra dimension. Itthen validates against
H-- which is whydims == 3and thersz == dims*2assert pass -- but forwards the original
histto the implementation, whichre-reads
dimsfrom it, sees2, and takes the 2D path. The third channel isnever read.
This also explains why the call does not simply throw: the checks run against
the corrected histogram while the computation runs against the uncorrected one.
Fix
Forward
Hrather thanhist.Hexists for exactly this purpose, and whenhcn == 1it aliaseshist, so single-channel histograms are bit-identical tobefore.
Verification
Built
imgproc+ Python bindings, GCC 16.1 (MSYS2 UCRT64), Release, Windows.Reproducer from the issue, before -> after:
bp[0,1], 3rd channel differsCV_32Fhistogramopencv_test_imgproc, same binary before and after the one-line change:Comparing the two failure sets, the only difference is the new test moving from
failed to passed; nothing else changed. (The 256 remaining failures are
pre-existing here for lack of a local
opencv_extracheckout, and are identicalin both runs.)
The new C++ test was confirmed to fail without the fix and pass with it.
Tests added
Imgproc_Hist_Calc.calcBackProject_3d_regression_29938inmodules/imgproc/test/test_histograms.cpptest_calcBackProject_3dinmodules/imgproc/misc/python/test/test_imgproc.py(the issue's own case)Notes
modules/imgprocpublic API and ABI are unchanged.CV_OCL_RUNis gated onhist.dims() <= 2andreturns before the changed line.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request