C++ OpenCV-如何计算图像中暗颜色的百分比?

C++ OpenCV-如何计算图像中暗颜色的百分比?,c++,opencv,image-processing,C++,Opencv,Image Processing,我想通过计算图像中暗色的百分比来过滤图像 此图像中的深色百分比为0.05%,例如: 第二幅图像中的深色百分比为0.5%: 用于将暗像素设置为零,然后用于计算非零值 double getBlackProportion(cv::Mat img, double threshold) { int imgSize = img.rows * img.cols; // you can use whathever for maxval, since it's not used in CV_THR

我想通过计算图像中暗色的百分比来过滤图像

此图像中的深色百分比为0.05%,例如:

第二幅图像中的深色百分比为0.5%:

用于将暗像素设置为零,然后用于计算非零值

double getBlackProportion(cv::Mat img, double threshold)
{
    int imgSize = img.rows * img.cols;
    // you can use whathever for maxval, since it's not used in CV_THRESH_TOZERO
    cv::threshold(img, img, threshold, -1, CV_THRESH_TOZERO);
    int nonzero = cv::countNonZero(img);


    return (imgSize - nonzero) / double(imgSize);
}

比例与图像的其他部分相比?你可以检查所有的像素值,如果它们足够暗,你可以对它们进行计数;NPcountNonZeroimg@user1767754您能给出一些示例代码吗?如果它是一个二进制图像,请使用countNonZero,如@ZdaR所说,否则,请使用threshold转换为二进制图像,然后使用countNonZero