2017-06-28 11 views
-4

画像の暗い色の割合を計算して画像をフィルタリングします。OpenCV - 画像の暗い色の割合を計算する方法は?

この画像中の暗い色のパーセントは、例えば、0.05%である。

enter image description here

+0

残りの画像と比較してプロポーショナルですか?あなたは十分な "暗い"場合は、すべてのピクセル値を通過し、それらを数えることができます。 – user1767754

+0

'import numpy as np; np.countNonZero(img) ' – ZdaR

+0

@ user1767754いくつかのコード例がありますか? – tomfriwel

答えて

1

enter image description here

と第2の画像中の暗い色の割合は0.5%です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); 
} 
関連する問題