private void VideoDevice_NewFrame(object sender, NewFrameEventArgs e) { var source = new Bitmap(e.Frame); var unmanaged = UnmanagedImage.FromManagedImage(source); Grayscale grayscaleFilter = new Grayscale(1, 0, 0); var grayscaleUnm = grayscaleFilter.Apply(unmanaged); var grayscale = grayscaleUnm.ToManagedImage(); var points = PointDetector.FindHighlitedPoints(grayscaleUnm.ImageData, e.Frame.Width, e.Frame.Height); var points3Enumerable = from point in points select PointScanner.ConvertTo3D(point, e.Frame.Width, e.Frame.Height) into point3 where Properties.Cylinder.Contains(point3) select point3; Vector3[] vertices = points3Enumerable.ToArray(); using (var gr = System.Drawing.Graphics.FromImage(source)) { foreach (var p in points) { gr.FillEllipse(Brushes.Green, p.X - 4, p.Y - 4, 8, 8); } foreach (var v in vertices) { var p2 = PointScanner.ConvertTo2D(v, e.Frame.Width, e.Frame.Height); Brush color = v.Z > 0 ? Brushes.Blue : Brushes.Red; gr.FillEllipse(color, p2.X - 2, p2.Y - 2, 4, 4); } } using (var graphics = sourceImage.CreateGraphics()) graphics.DrawImage(source, 0, 0, sourceImage.Width, sourceImage.Height); using (var graphics = grayscaleImage.CreateGraphics()) graphics.DrawImage(grayscale, 0, 0, sourceImage.Width, sourceImage.Height); }
private void CopyTexture(Bitmap frame, int stripeIndex, double modelRotation) { FaceInfo[] faces = Stripes[stripeIndex]; int w = Texture.Width - 1; int h = Texture.Height - 1; Quaternion rotation = Quaternion.CreateFromYawPitchRoll((float)modelRotation, 0, 0); BitmapData data = frame.LockBits(new Rectangle(Point.Empty, frame.Size), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); foreach (var face in faces) { Triangle src = Triangle.FromVertices( from info in face select Vector3.Transform(Vertices[info.VertexIndex], rotation) into vertex select PointScanner.ConvertTo2D(vertex, frame.Width, frame.Height)); Triangle dst = Triangle.FromVertices( from info in face select TexVertices[info.TextureIndex] into point select new Vector2(w * point.X, h * (1 - point.Y))); src.MapTexture(data, TextureData, dst); } frame.UnlockBits(data); }