2011-03-01 76 views
4

我试图通过打开或眯着眼睛来放大和缩小来自摄像头的实时视频流的每个帧。我已经有了眼动追踪部分,但我无法弄清楚ScaleTransform中的哪个部分。下面是我现有的代码:如何使用WPF中的眼动跟踪来缩放图像?

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 
     } 
    } 
} 
+0

出于好奇,你是如何做眼动追踪的?你打开采购这个代码? –

+0

@Richard看起来他正在使用OpenCV项目的C#绑定:http://www.emgu.com/ – joshperry

+0

@joshperry哦,谢谢。我在编辑它之前没有看到。 –

回答

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 
+0

谢谢!我如何在WinForm中缩放图像?结合EmguCV即可。 –

+0

@李The ImageViewer具有原生缩放功能,我更新了详细的答案。 – joshperry