countNonZero function gives an assertion error in openCV

后端 未结 2 916
挽巷
挽巷 2020-12-15 08:11

I tried to get horizontal projection using countNonZero() function as below.

Mat src = imread(INPUT_FILE, CV_LOAD_IMAGE_COLOR);
Mat binaryImage = src.clone()         


        
2条回答
  •  太阳男子
    2020-12-15 08:35

    Assertion src.channels() == 1 means that image should have 1 channel, i.e. it has to be gray, not colored. You are calling countNonZero on roi, which is a subimage of binaryImage, which is a clone of src, which is originally colored.

    I suppose you wanted to write cvtColor(binaryImage, binaryImage, CV_BGR2GRAY);. In this case it makes sense. However, I do not see you using src anywhere again, so perhaps you do not need this intermediate image. In case you do, do not call "binary", since "binary" in computer vision usually stands for black-or-white image, only two colors. Your image is "gray", since it has all shades of black and white.

    Concerning your original task, Miki is right, you should use cv::reduce for it. He already gave you an example on how to use it.

提交回复
热议问题