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

时间:2017-06-28 06:33:54

标签: c++ opencv image-processing

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

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

enter image description here

第二张图片中暗色的百分比为0.5%:

enter image description here

1 个答案:

答案 0 :(得分:1)

使用threshold to zero将暗像素设置为零,然后使用cv::countNonZero()计算非零值。

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);
}
相关问题