c++ - countNonZero function gives an assertion error in openCV -
i tried horizontal projection using countnonzero() function below.
mat src = imread(input_file, cv_load_image_color); mat binaryimage = src.clone(); cvtcolor(src, src, cv_bgr2gray); mat horizontal = mat::zeros(1,binaryimage.cols, cv_8uc1); (int = 0; i<binaryimage.cols; i++) { mat roi = binaryimage(rect(0, 0, 1, binaryimage.rows)); horizontal.at<int>(0,i) = countnonzero(roi); cout << "col no:" << << " >>" << horizontal.at<int>(0, i); }
but error occured in line of calling countonzero() function. error follows.
opencv error: assertion failed (src.channels() == 1 && func != 0) in cv::countno nzero, file c:\builds\2_4_packslave-win32-vc12-shared\opencv\modules\core\src\st at.cpp, line 549
can please point out mistake?
assertion src.channels() == 1
means image should have 1 channel, i.e. has gray, not colored. calling countnonzero
on roi
, subimage of binaryimage
, clone of src
, colored.
i suppose wanted write cvtcolor(binaryimage, binaryimage, cv_bgr2gray);
. in case makes sense. however, not see using src
anywhere again, perhaps not need intermediate image. in case do, not call "binary", since "binary" in computer vision stands black-or-white image, 2 colors. image "gray", since has shades of black , white.
concerning original task, miki right, should use cv::reduce
it. gave example on how use it.
Comments
Post a Comment