Repository navigation
imgproc: accept CV_Bool masks in matchTemplate (#25895) - #29677
Conversation
cv::Mat_<bool>::depth() returns CV_Bool in 5.0, where it returned CV_8U in 4.x, so matchTemplate with a boolean mask now fails the type check in matchTemplateMask: (-215) _mask.depth() == CV_8U || _mask.depth() == CV_32F Allow CV_Bool and widen it to CV_8U before the existing binarization step. The widening is required because cv::threshold() does not accept CV_Bool; once widened, the existing THRESH_BINARY path treats any non-zero entry as selected, which matches the documented CV_8U mask semantics. CV_8U and CV_32F masks are unaffected. Added a regression test that compares a Mat_<bool> mask against the equivalent CV_8UC1 mask across all six match methods for CV_8UC1, CV_8UC3 and CV_32FC1 images.
There was a problem hiding this comment.
The following CV_8U case applies threshold and converts bool mask with 1 and 0 into 1.0 and 0.0. The bool path does not look efficient.
|
@asmorkalov Good catch, fixed in f50c64c. You are right that the bool path was doing redundant work. It widened the mask to CV_8U and then fell into the CV_8U branch, which ran cv::threshold() over values that were already 0 and 1 before converting them to 0.0 and 1.0. Conversion out of CV_Bool already maps every non-zero entry to exactly 1 (DEF_CVTBOOL2_FUNC in modules/core/src/convert.simd.hpp emits Rebuilt and reran the imgproc suite: all 18 Imgproc_MatchTemplateBoolMask cases and the other 147 tests pass. I also reconfirmed the negative test, all 18 fail against unmodified 5.x. |
Address review feedback: the previous patch widened the bool mask to CV_8U and then fell into the CV_8U branch, which applies cv::threshold() to values that are already 0 and 1 before converting them to 0.0 and 1.0. Conversion out of CV_Bool already maps every non-zero entry to exactly 1, so a single convertTo() to CV_32F produces the same mask in one pass. The CV_8U branch is unchanged.
d72a485 to
f50c64c
Compare
Problem
cv::Mat_<bool>::depth()returnsCV_Boolin 5.0, where it returnedCV_8Uin 4.x.matchTemplateMaskgates the mask attemplmatch.cpp:737, so passing a boolean mask now fails with:A binary mask is a normal input for masked template matching, so this is a regression against 4.x.
Fix
Allow
CV_Boolin the assertion and widen the mask toCV_8Ubefore the existing binarization step.The widening is required rather than passing
CV_Boolstraight through, becausecv::threshold()does not acceptCV_Bool. Once widened, the existingTHRESH_BINARYpath treats any non-zero entry as selected, which is exactly the documentedCV_8Umask semantics.CV_8UandCV_32Fmasks take an unchanged path.The masked path is the only one affected:
cv::matchTemplateroutes every non-empty mask throughmatchTemplateMask, and there is no OpenCL mask variant.Test
Imgproc_MatchTemplateBoolMask.matches_uchar_maskcompares aMat_<bool>mask against the equivalentCV_8UC1mask and requires the results to agree, across all six match methods (TM_SQDIFF,TM_SQDIFF_NORMED,TM_CCORR,TM_CCORR_NORMED,TM_CCOEFF,TM_CCOEFF_NORMED) forCV_8UC1,CV_8UC3andCV_32FC1images. 18 parameter combinations.Verified locally on 5.x: all 18 fail without the source change (with the assertion above) and pass with it. The full
*MatchTemplate*set, 165 tests, passes. No test data needed, the test is synthetic.Part of #25895, and follows the same approach as #29580, #29597 and #29622.
Pull Request Readiness Checklist
Patch to opencv_extra has the same branch name.