答案 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);
}