如何使用c#aforge检测实时网络摄像头上的精确边缘和积分投影?

时间:2014-12-06 01:53:24

标签: c# real-time webcam projection aforge

"我一直在尝试检测图像上的精确边缘并且它已经成功,但我不知道如果我想实时检测它的边缘,这是我的代码,它没有错误,但canny窗口无法处理

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using KomCit;
using System.Drawing.Imaging;
using System.IO;
using System.Timers;
using AForge;
using AForge.Imaging.Filters;
using AForge.Video.DirectShow;
using System.Threading;

namespace canny_video
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private FilterInfoCollection CaptureDevice;
        private VideoCaptureDevice FinalFrame;

        void FinalFrame_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            CameraBox.Image = (Bitmap)eventArgs.Frame.Clone();
        }

        private static Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //timer1.Enabled = true;
            CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo Device in CaptureDevice)
            {
                listDevice.Items.Add(Device.Name);

            }
            listDevice.SelectedIndex = 0;
            FinalFrame = new VideoCaptureDevice();
        }

        private void start_Click(object sender, EventArgs e)
        {
            FinalFrame = new VideoCaptureDevice(CaptureDevice[listDevice.SelectedIndex].MonikerString);
            FinalFrame.NewFrame += new AForge.Video.NewFrameEventHandler(FinalFrame_NewFrame);
            FinalFrame.Start();
            //CannyBox.Image = (Bitmap)CameraBox.Image.Clone();    //capture image bitmap
            //Bitmap gambar = new Bitmap(CameraBox.Image);
            Bitmap gambar = new Bitmap(CameraBox.Image);
            Grayscale gray = new Grayscale(0.2125, 0.7154, 0.0721);
            CannyEdgeDetector cany = new CannyEdgeDetector(0, 70);
            Bitmap hasil = cany.Apply(gray.Apply(gambar));
            // BlobsFiltering blob = new BlobsFiltering(0, 0, 20, 20);
            //Bitmap hasil = blob.Apply(gray.Apply(gambar));
            //CannyBox.Width = gambar.Width;
            //CannyBox.Height = gambar.Height;
            CannyBox.Image = hasil;
        }



        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FinalFrame.IsRunning == true)
            {
                FinalFrame.Stop();
            }
        }
    }
}

"

我上面的问题解决了,谢谢:) 但我对积分投影有另一个问题,但我不知道怎么做。 请帮助,提前谢谢

2 个答案:

答案 0 :(得分:0)

一些意见:

  • 您的两个私有变量非常容易混淆。您的CaptureDevice是计算机上所有捕获设备的集合,而您的FinalFrame是捕获设备。
  • 接收时,您不会在位图上查找精简边缘。
  • NewFrame事件处理程序在流线程中运行,因此您应该在其中使用.BeginInvoke以使其可供UI线程使用。

你可以做什么:

  • 在_NewFrame处理程序中,使用BeginInvoke将克隆的图像传输到ProcessCameraImage方法(因此将在UI线程上运行)。
  • 在此方法中应用精确边缘检测。
  • 然后将结果分配到图片框。

注意:我不知道CPU重的边缘检测是怎样的。如果成本高昂,在UI线程上执行此操作可能会阻止用户交互。在这种情况下,您可以在流线程中执行此操作,并仅传输已处理的图像以供显示。但是,根据所需的时间,这可能会迫使相机降低其帧速率。

答案 1 :(得分:0)

谢谢你的回答,它刚刚解决了,我修改了它,它以某种方式解决了

这是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Imaging;       //Save P2
using System.IO;                    //Save P2

namespace AForgeCamera
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        Bitmap video;

        private FilterInfoCollection CaptureDevice;
        private VideoCaptureDevice FinalFrame;
        int mode;



        private void Form1_Load(object sender, EventArgs e)
        {
            CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo Device in CaptureDevice)
            {
                comboBox1.Items.Add(Device.Name);
            }
            comboBox1.SelectedIndex = 0;
            FinalFrame = new VideoCaptureDevice();
        }

        private void button1_Click(object sender, EventArgs e)      //Tombol Start
        {
            FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
            FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
            FinalFrame.Start();
        }
        void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            video = (Bitmap)eventArgs.Frame.Clone();
            Bitmap video2 = (Bitmap)eventArgs.Frame.Clone();
            if (mode==1)
            {
                Grayscale gray = new Grayscale(0.2125, 0.7154, 0.0721);
                Bitmap video3 = gray.Apply(video2);
                CannyEdgeDetector canny = new CannyEdgeDetector(0, 70);
                canny.ApplyInPlace(video3);
                pictureBox2.Image = video3;
            }
            pictureBox1.Image = video;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FinalFrame.IsRunning == true)
            {
                FinalFrame.Stop();
            }
        }

        private void btnTrackingObject_Click(object sender, EventArgs e)
        {
            mode = 1;
        }


    }
}

但是现在我正在研究下一个问题,我尝试使用积分投影来检测对象的位置,是否可以使用aforge来做呢?