c# - 在 PC 中使用网络摄像头作为环境光传感器

标签 c# windows windows-8 webcam .net-4.5

是否可以让电脑的网络摄像头充当环境光传感器? 我在 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/

相关文章:

c# - .NET 的通用性能测试框架

c# - foreach 循环与 ForEach 方法 - 区别?

c# - 如何从 javascript 读取隐藏字段中的值

windows - 在 Win 7 上使用带有 ANGLE 的 Qt 时出现空白窗口

windows - 不同服务器的 FTP over CMD

windows-8 - WinRT HttpClient 阻止启动画面

java - 在 Windows 上安装 Eclipse IDE 时出错

c# - Windows 手机 8 : Localization doesn't work

windows - 远程桌面中的物理屏幕分辨率大小更改

windows-8 - Windows 8 快照 View