【问题标题】:Use Webcam as ambient light sensor in PC在 PC 中使用网络摄像头作为环境光传感器
【发布时间】:2012-09-01 18:08:10
【问题描述】:

是否可以让电脑的网络摄像头充当环境光传感器? 我在 Windows 8 pro 中使用 .Net 4.5 框架。

【问题讨论】:

  • 您的意思是仅检测网络摄像头可以查看的房间内有多少光?只需使用 WIA 或其他库从中拍摄一张照片,然后查看像素以确定它的暗或亮。
  • 感谢您的回复..我将搜索并了解 WIA 的使用,我也在寻找 Aforge .net 是否可以提供帮助。但是您能帮我理解如何通过像素来确定它吗?

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


【解决方案1】:

Philippe 对此问题的回答: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”,这可能更准确,我不知道。

【讨论】:

  • 感谢您的回复,这将解决亮度部分。从您之前的评论中,我正在搜索 WIA 以及访问 cam 有什么用处,并找到了这篇文章 social.msdn.microsoft.com/Forums/en-US/tailoringappsfordevices/… - 这表示 Windows 8 不支持 WIA :(
  • @indranilpal 如果您阅读了您的链接,Rob Caplan 会说“问题和答案是特定于 Metro 风格的应用程序。桌面应用程序支持 WIA。”。它适用于 Windows 8,但不适用于 Metro 应用程序,普通的桌面应用程序可以使用 WIA。此外,还有很多库可用于访问网络摄像头。
  • 是的,我忽略了那部分……它确实支持桌面模式。如果我遇到任何问题,我会尝试实施它并让您知道。
猜你喜欢
  • 1970-01-01
  • 2013-08-25
  • 1970-01-01
  • 2010-11-12
  • 2012-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多