silverlight 4互动应用之虚拟实现

自从sl4对webcam的支持后,我并想通过webcam做一些简单的sl互动应用。经过一晚的研究,目前实现了一个简单的互动基础。

这个demo实现了:

1.通过鼠标选取摄像头内容的某一个特别的色。

2.动态查找这个色所在的位置(当这个色移动了也可以找到位置)

(可以选取你可以移动的物体的特别的颜色,达到动态跟踪这个物体)

3.我用一个四方形去跟随这个颜色的位置变化。

颜色的转换我通过以下代码实现转换:

 1:  public static Color Pixel2Color(int pixel)
 2:  {
 3:      return Color.FromArgb((byte)((pixel >> 24) & 0xff),
 4:                            (byte)((pixel >> 16) & 0xff),
 5:                            (byte)((pixel >> 8) & 0xff),
 6:                            (byte)((pixel) & 0xff));
 7:  }
 8:  
 9:  public static int Color2Pixel(Color color)
10:  {
11:      return (color.A << 24) | (color.R << 16) | (color.G << 8) | color.B;
12:  }
13:  

当用户选择颜色时,我们可以调用:

1:  Pixel2Color(lastBitmap.Pixels[(int)mousePosition.Y * lastBitmap.PixelWidth + (int)mousePosition.X]);
2:  

我们要查找这个颜色的时候可以通过递归找到这个色:

 1:  for (int y = 0; y < lastBitmap.PixelHeight; y++)
 2:  {
 3:      for (int x = 0; x < lastBitmap.PixelWidth; x++)
 4:      {
 5:          if (Color2Pixel(focusColor) == lastBitmap.Pixels[y * lastBitmap.PixelWidth + x])
 6:          {
 7:              label1.Content = x.ToString() + "," + y.ToString();
 8:              Canvas.SetLeft(silverlightControl11, x);
 9:              Canvas.SetTop(silverlightControl11, y);
10:              return;
11:          }
12:      }
13:  }
14:  

以下是一个切图:

image

原文地址:https://www.cnblogs.com/jacle169/p/2810049.html