4

我试图通过睁开或眯起眼睛来放大和缩小来自网络摄像头的实时视频流中的每一帧。我已经让眼动追踪部分工作了,但我不知道在哪里适合 ScaleTransform。以下是我现有的代码:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.CV;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Media;

namespace eyeDetection
{
   static class Program
   {
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main()
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Run();
      }

      static void Run()
      {
          ImageViewer viewer = new ImageViewer(); //create an image viewer
          Capture capture = new Capture(); //create a camera capture
          Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
              {   // run this until application closed (close button click on image viewer)
                  Image<Bgr, Byte> image = capture.QueryFrame();
                  Image<Gray, Byte> gray = image.Convert<Gray, Byte>(); //Convert it to Grayscale

                  Stopwatch watch = Stopwatch.StartNew();
                  //normalizes brightness and increases contrast of the image
                  gray._EqualizeHist();

                  //Read the HaarCascade objects
                 HaarCascade eye = new HaarCascade("haarcascade_eye.xml");

                 MCvAvgComp[][] eyeDetected = gray.DetectHaarCascade(
                     eye,
                     1.1,
                     10,
                     Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                     new Size(20, 20));

                  foreach (MCvAvgComp e in eyeDetected[0])
                  {
                      //draw the eyes detected in the 0th (gray) channel with blue color
                      image.Draw(e.rect, new Bgr(Color.Blue), 2);
                  }


                    watch.Stop();
                  //display the image 
                  viewer.Image = image; //draw the image obtained from camera
              });
          viewer.ShowDialog(); //show the image viewer
      }
   }
}
4

1 回答 1

2

这不是 WPF,它是一个 WinForms 应用程序。ImageViewer是 EmguCV 提供的一个类,它继承自System.Windows.Forms.Form,它们也没有 WPF。

您将需要创建一个新的 WPF 项目,集成您的代码,并创建您自己的 WPF 视图来承载图像,然后您可以在其中设置文档元素的转换。

如果您只想使用 WinForms 查看器,则可以引用该ImageViewer::ImageBox属性。该类ImageBox具有对缩放和平移的原生支持。它有一个ZoomScale可以以编程方式设置的属性,并且还允许您访问HorizontalScrollBarVerticalScrollBar属性来控制平移位置。

viewer.ImageBox.ZoomScale = 2.0;  // zoom in by 2x
于 2011-03-02T00:36:44.610 回答