c# - Detect bright and dark images -
i have code stackoverflow answers detect bright , dark images
the problem not work , don't know why.
for example if call
isdark(bitmap, 40, 0.9); // sees image bright
any value 0.1 0.99 returns bright image , other value above 0.99 returns images dark.
the tolerance value seems have no effect if set 1 250.
// fast access 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, = bitmap.width * bitmap.height; (int = 0; < bytes.length; += 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; }
ok, after looking @ again, notice comparison @ end of function looks backwards (based on name of variable "darkprocent"). think comparison operator should >=, not <=.
that got me answers expecting test images.
Comments
Post a Comment