是否可以让电脑的网络摄像头充当环境光传感器? 我在 Windows 8 专业版中使用 .Net 4.5 框架。
最佳答案
菲利普对这个问题的回答:How to calculate the average rgb color values of a bitmap有代码可以计算位图图像的平均 RGB 值(他的代码中的 bm):
BitmapData srcData = bm.LockBits(
new Rectangle(0, 0, bm.Width, bm.Height),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);
int stride = srcData.Stride;
IntPtr Scan0 = dstData.Scan0;
long[] totals = new long[] {0,0,0};
int width = bm.Width;
int height = bm.Height;
unsafe
{
byte* p = (byte*) (void*) Scan0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
for (int color = 0; color < 3; color++)
{
int idx = (y*stride) + x*4 + color;
totals[color] += p[idx];
}
}
}
}
int avgR = totals[0] / (width*height);
int avgG = totals[1] / (width*height);
int avgB = totals[2] / (width*height);
获得平均 RGB 值后,您可以使用 Color.GetBrightness 确定亮度或暗度。 GetBrightness 将返回一个介于 0 和 1 之间的数字,0 为黑色,1 为白色。你可以使用这样的东西:
Color imageColor = Color.FromARGB(avgR, avgG, avgB);
double brightness = imageColor.GetBrightness();
您也可以将 RGB 值转换为 HSL 并查看“L”,这可能更准确,我不知道。
关于c# - 在 PC 中使用网络摄像头作为环境光传感器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12230137/