# Error: (-215:Assertion failed) cn == 1 in function 'countNonZero'

**URL:** <https://forum.opencv.org/t/error-215-assertion-failed-cn-1-in-function-countnonzero/3865>\
**Category:** Uncategorized\
**Created:** [June 16, 2021, 4:00am UTC](https://forum.opencv.org/t/error-215-assertion-failed-cn-1-in-function-countnonzero/3865 "2021-06-16T04:00:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjdrc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sjdrc/32/2411_2.png) [@sjdrc](https://forum.opencv.org/u/sjdrc)\
**Post date:** [June 16, 2021, 4:00am UTC](https://forum.opencv.org/t/error-215-assertion-failed-cn-1-in-function-countnonzero/3865/1 "2021-06-16T04:00:13Z")

</div>

We have a simple test in our CI that was passing with OpenCV 3.4.14:

```auto
cv::Mat testImg = cv::imread("../resources/Images/person1.png");
TensorHWCf testTensor(testImg);
cv::Mat resImg = testTensor.ToCVImage( 0 );
CHECK(cv::countNonZero(testImg != resImg) == 0);

```

However this test now fails with 4.5.2

```auto
../tests/graphite/dnn/TensorTest.cpp:44: ERROR: CHECK( cv::countNonZero(comp) == 0 ) THREW exception: "OpenCV(4.5.2) /usr/local/src/opencv/modules/core/src/count_non_zero.dispatch.cpp:128: error: (-215:Assertion failed) cn == 1 in function 'countNonZero'
"

```

[Documentation suggests](https://docs.opencv.org/4.5.2/d1/d10/classcv_1_1MatExpr.html#details) that “The result of comparison is an 8-bit single channel mask whose elements are set to 255 (if the particular element or pair of elements satisfy the condition) or 0.”

However the result of the comparison appears to be 3 channel.

How can I go about resolving this? It appears that a regression has been introduced.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [June 16, 2021, 6:11am UTC](https://forum.opencv.org/t/error-215-assertion-failed-cn-1-in-function-countnonzero/3865/2 "2021-06-16T06:11:41Z")

</div>

can’t test 3.4, but for an older 4.0.1 version:

```
Mat a(10,10,CV_8UC3);
Mat b(10,10,CV_8UC3);
Mat c = (a!=b);
cout << c.size() << c.type() << " " << c.channels() << endl;
[10 x 10]16 3

compare(a,b,c,CMP_NE);
cout << c.size() << c.type() << " " << c.channels() << endl;
[10 x 10]16 3

```

also see [compare](https://docs.opencv.org/4.5.2/d2/de8/group __core__ array.html#ga303cfb72acf8cbb36d884650c09a3a97) :

> dst output array of type ref CV\_8U that has the same size and the same number of channels as the input arrays.

i’m puzzled 😉
