明るい画像と暗い画像を検出するために、stackoverflowの回答からこのコードがあります
問題は、それが機能しないことであり、その理由はわかりません。
たとえば、私が電話した場合
IsDark(bitmap, 40, 0.9); // this always sees the image as bright
0.1 から 0.99 までの任意の値は明るい画像を返し、0.99 を超えるその他の値はすべての画像を暗い画像として返します。
許容値は 1 から 250 まで設定しても効果がないようです。
// For fast access to pixels
public static unsafe byte[] BitmapToByteArray(Bitmap bitmap)
{
BitmapData bmd = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);
byte[] bytes = new byte[bmd.Height * bmd.Stride];
byte* pnt = (byte*)bmd.Scan0;
Marshal.Copy((IntPtr)pnt, bytes, 0, bmd.Height * bmd.Stride);
bitmap.UnlockBits(bmd);
return bytes;
}
public bool IsDark(Bitmap bitmap, byte tolerance, double darkProcent)
{
byte[] bytes = BitmapToByteArray(bitmap);
int count = 0, all = bitmap.Width * bitmap.Height;
for (int i = 0; i < bytes.Length; i += 4)
{
byte r = bytes[i + 2], g = bytes[i + 1], b = bytes[i];
byte brightness = (byte)Math.Round((0.299 * r + 0.5876 * g + 0.114 * b));
if (brightness <= tolerance)
count++;
}
return (1d * count / all) <= darkProcent;
}