mirror of
https://github.com/opencv/opencv.git
synced 2026-12-02 13:16:32 -06:00
core: add the missing CV_Bool kernel to cv::compare
Comparing two CV_Bool matrices threw "no kernel for this op/type combination" for all six comparison operations, while the matrix/scalar overload of the same function worked. compareByType had a case for every other depth, CV_32U, CV_64U and CV_64S included, so CV_Bool was the only one left out. The uchar kernel is not a correct substitute. A CV_Bool byte is not guaranteed to hold 0 or 1, since a matrix wrapping an external buffer is never converted, and comparing the raw bytes would order 2 above 1 when both mean true. countNonZero, norm, sum and the scalar compare overload all already read CV_Bool logically, and notKernel documents the same rule for bitwise NOT. scalarCompareKernel takes an isBool template parameter that normalises each operand to 0/1 before comparing. It is a compile time constant, so the existing instantiations are unchanged.
This commit is contained in:
@@ -650,7 +650,10 @@ static inline void cmpUnpackPatch(int flags, uchar trueVal, uchar mvals[4], ucha
|
||||
// from TKernel::flags - 255 (default, matching cv::compare) or 1 (a numpy-style 0/1 mask). Integers
|
||||
// compare directly (WT == T); f16/bf16 compare through a float WT. This is the fallback for the depths
|
||||
// the SIMD kernel doesn't cover (f16/bf16/64-bit). stepx in {0,1}; dst is u8, byte step == element step.
|
||||
template<typename T, typename WT, class Cmp>
|
||||
// isBool normalises each operand to 0/1 first: a CV_Bool byte is not guaranteed canonical, since a
|
||||
// matrix wrapping an external buffer is never converted, so comparing raw bytes would order 2 above
|
||||
// 1 when both mean true. This matches countNonZero, norm and the scalar cv::compare overload.
|
||||
template<typename T, typename WT, class Cmp, bool isBool = false>
|
||||
static int scalarCompareKernel(const void* src0_, size_t s0y, size_t s0x,
|
||||
const void* src1_, size_t s1y, size_t s1x,
|
||||
const void*, size_t, size_t,
|
||||
@@ -671,6 +674,7 @@ static int scalarCompareKernel(const void* src0_, size_t s0y, size_t s0x,
|
||||
cmpUnpackPatch(flags, trueVal, mvals, vvals);
|
||||
const bool perch = (flags & EW_CMP_PATCH) != 0; // per-channel fix-up (width=cn<=4)
|
||||
CV_Assert(!perch || width <= 4); // a per-channel patch is a short-row tile
|
||||
#define EW_CMP_LD(v) (isBool ? (WT)((v) != 0) : (WT)(v))
|
||||
#define EW_CMP_APPLY(cond, x) (perch ? (uchar)(((cond) ? mvals[x] : 0) | vvals[x]) \
|
||||
: (uchar)((cond) ? trueVal : 0))
|
||||
|
||||
@@ -678,18 +682,19 @@ static int scalarCompareKernel(const void* src0_, size_t s0y, size_t s0x,
|
||||
for (int y = 0; y < height; y++, src0 += s0y, src1 += s1y, dst += dsty)
|
||||
{
|
||||
if (s0x == s1x) {
|
||||
for (int x = 0; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp((WT)src0[x], (WT)src1[x]), x);
|
||||
for (int x = 0; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp(EW_CMP_LD(src0[x]), EW_CMP_LD(src1[x])), x);
|
||||
}
|
||||
else if (s0x == 0) {
|
||||
WT a = (WT)src0[0];
|
||||
for (int x = 0; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp(a, (WT)src1[x]), x);
|
||||
WT a = EW_CMP_LD(src0[0]);
|
||||
for (int x = 0; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp(a, EW_CMP_LD(src1[x])), x);
|
||||
}
|
||||
else {
|
||||
WT b = (WT)src1[0];
|
||||
for (int x = 0; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp((WT)src0[x], b), x);
|
||||
WT b = EW_CMP_LD(src1[0]);
|
||||
for (int x = 0; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp(EW_CMP_LD(src0[x]), b), x);
|
||||
}
|
||||
}
|
||||
#undef EW_CMP_APPLY
|
||||
#undef EW_CMP_LD
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1386,6 +1391,9 @@ static KernelFunc compareByType(int T)
|
||||
case CV_16F: return vecCompareKernel<hfloat, v_float32, v_uint32, Cmp>; // widen f16->f32
|
||||
#endif
|
||||
case CV_16BF: return vecCompareKernel<bfloat, v_float32, v_uint32, Cmp>; // widen bf16->f32 (no native)
|
||||
// CV_Bool is logical, not a one-byte integer: compare it as (x != 0), so a non-canonical
|
||||
// byte compares equal to 1 rather than above it.
|
||||
case CV_Bool: return scalarCompareKernel<uchar, uchar, Cmp, true>;
|
||||
case CV_64U: return scalarCompareKernel<uint64_t, uint64_t, Cmp>;
|
||||
case CV_64S: return scalarCompareKernel<int64_t, int64_t, Cmp>;
|
||||
#if CV_SIMD_64F || CV_SIMD_SCALABLE_64F
|
||||
|
||||
@@ -5338,4 +5338,67 @@ TEST(Core_Multiply, scale_simd_scalar_consistency)
|
||||
}
|
||||
}
|
||||
|
||||
// cv::compare had no CV_Bool kernel for the matrix/matrix form, so every comparison of two
|
||||
// CV_Bool matrices threw "no kernel for this op/type combination" while the matrix/scalar
|
||||
// overload of the same function worked. CV_Bool is logical: a byte is true when it is non-zero,
|
||||
// which is how countNonZero, norm and the scalar overload already read it. A matrix wrapping an
|
||||
// external buffer is never converted, so bytes other than 0 and 1 are reachable through the
|
||||
// public API and must not compare as integers.
|
||||
typedef testing::TestWithParam<int> Core_CompareBool_canonical;
|
||||
typedef testing::TestWithParam<int> Core_CompareBool_nonCanonical;
|
||||
typedef testing::TestWithParam<int> Core_CompareBool_scalarAgreement;
|
||||
|
||||
// Canonical 0/1 operands: the result must match the same comparison done on CV_8U.
|
||||
TEST_P(Core_CompareBool_canonical, matches_CV_8U)
|
||||
{
|
||||
const int op = GetParam();
|
||||
uchar abytes[4] = { 0, 0, 1, 1 };
|
||||
uchar bbytes[4] = { 0, 1, 0, 1 };
|
||||
const Mat ab(1, 4, CV_Bool, abytes), bb(1, 4, CV_Bool, bbytes);
|
||||
const Mat au(1, 4, CV_8U, abytes), bu(1, 4, CV_8U, bbytes);
|
||||
|
||||
Mat got, want;
|
||||
ASSERT_NO_THROW(cv::compare(ab, bb, got, op));
|
||||
cv::compare(au, bu, want, op);
|
||||
EXPECT_EQ(0, cv::norm(got, want, NORM_INF));
|
||||
}
|
||||
|
||||
// Non-canonical bytes: 1, 2 and 255 all mean true, so each must compare exactly as 1 does.
|
||||
TEST_P(Core_CompareBool_nonCanonical, true_bytes_compare_as_one)
|
||||
{
|
||||
const int op = GetParam();
|
||||
uchar raw[4] = { 0, 1, 2, 255 };
|
||||
uchar one[4] = { 0, 1, 1, 1 };
|
||||
const Mat a(1, 4, CV_Bool, raw), canonical(1, 4, CV_Bool, one);
|
||||
uchar ones[4] = { 1, 1, 1, 1 };
|
||||
const Mat rhs(1, 4, CV_Bool, ones);
|
||||
|
||||
Mat got, want;
|
||||
ASSERT_NO_THROW(cv::compare(a, rhs, got, op));
|
||||
cv::compare(canonical, rhs, want, op);
|
||||
EXPECT_EQ(0, cv::norm(got, want, NORM_INF)) << "non-canonical bytes must compare logically";
|
||||
}
|
||||
|
||||
// The matrix/matrix form must agree with the matrix/scalar form, which already worked.
|
||||
TEST_P(Core_CompareBool_scalarAgreement, matrix_matrix_matches_matrix_scalar)
|
||||
{
|
||||
const int op = GetParam();
|
||||
uchar raw[4] = { 0, 1, 2, 255 };
|
||||
const Mat a(1, 4, CV_Bool, raw);
|
||||
uchar ones[4] = { 1, 1, 1, 1 };
|
||||
const Mat rhs(1, 4, CV_Bool, ones);
|
||||
|
||||
Mat fromMat, fromScalar;
|
||||
cv::compare(a, rhs, fromMat, op);
|
||||
cv::compare(a, Scalar(1), fromScalar, op);
|
||||
EXPECT_EQ(0, cv::norm(fromMat, fromScalar, NORM_INF));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Core_CompareBool_canonical,
|
||||
testing::Values(CMP_EQ, CMP_NE, CMP_GT, CMP_GE, CMP_LT, CMP_LE));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Core_CompareBool_nonCanonical,
|
||||
testing::Values(CMP_EQ, CMP_NE, CMP_GT, CMP_GE, CMP_LT, CMP_LE));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Core_CompareBool_scalarAgreement,
|
||||
testing::Values(CMP_EQ, CMP_NE, CMP_GT, CMP_GE, CMP_LT, CMP_LE));
|
||||
|
||||
}} // namespace
|
||||
|
||||
Reference in New Issue
Block a user