countNonZero function gives an assertion error in openCV

后端 未结 2 909
挽巷
挽巷 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.

    0 讨论(0)
  • 2020-12-15 08:54

    BTW, you can compute horizontal projection using reduce giving as argument CV_REDUCE_SUM.

    A minimal example:

    Mat1b mat(4, 4, uchar(0));
    mat(0,0) = uchar(1);
    mat(0,1) = uchar(1);
    mat(1,1) = uchar(1);
    
    // mat is: 
    //
    // 1100
    // 0100
    // 0000
    // 0000
    
    // Horizontal projection, result would be a column matrix
    Mat1i reducedHor;
    cv::reduce(mat, reducedHor, 1, CV_REDUCE_SUM);
    
    // reducedHor is:
    //
    // 2
    // 1
    // 0
    // 0
    
    // Vertical projection, result would be a row matrix
    Mat1i reducedVer;
    cv::reduce(mat, reducedVer, 0, CV_REDUCE_SUM);
    
    // reducedVer is:
    //
    // 1200
    
    
    // Summary
    //
    // 1100 > 2
    // 0100 > 1
    // 0000 > 0
    // 0000 > 0
    // 
    // vvvv
    // 1200
    

    You can use this with your images like this:

    // RGB image
    Mat3b img = imread("path_to_image");
    
    // Gray image, contains values in [0,255]
    Mat1b gray;
    cvtColor(img, gray, CV_BGR2GRAY);
    
    // Binary image, contains only 0,1 values
    // The sum of pixel values will equal the count of non-zero pixels
    Mat1b binary;
    threshold(gray, binary, 1, 1, THRESH_BINARY);
    
    // Horizontal projection
    Mat1i reducedHor;
    cv::reduce(binary, reducedHor, 1, CV_REDUCE_SUM);
    
    // Vertical projection
    Mat1i reducedVer;
    cv::reduce(binary, reducedVer, 0, CV_REDUCE_SUM);
    
    0 讨论(0)
提交回复
热议问题