1

我在带有 bootcamp 的 Macbook pro 上使用 Aforge 框架制作了可以使用 Visual Studio C# 跟踪对象的项目。问题是当我运行代码并关闭它时。然后再次运行它,代码将不再在我的相机上显示图像。此外,停止代码后,我的网络摄像头上的绿灯仍然亮着。

我的第二个问题是,当我单击“跟踪对象按钮”并将过滤器应用于图像时,由于某些原因,它会将过滤器应用于两个图片框而不是仅一个。我需要帮助来调试我的代码。我尝试重新排列应用过滤器的行,但似乎无法正确处理。

这是我的代码

namespace VideoProcessing1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Graphics g;
        Bitmap video;
        bool OnOff;
        int mode;
        int thoigianddemnguoc;

        private FilterInfoCollection CaptureDevice;
        private VideoCaptureDevice FinalFrame;

        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();
            //FinalFrame.Stop();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            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();
            g = Graphics.FromImage(video2);
            g.DrawString("Test", new Font("Arial", 20), new SolidBrush(Color.White), new PointF(2, 2));
            g.Dispose();

            if (mode == 1)
            {
                ColorFiltering colorfilter = new ColorFiltering();
                colorfilter.Red = new AForge.IntRange(0, 255);
                colorfilter.Green = new AForge.IntRange(0, 75);
                colorfilter.Green = new AForge.IntRange(0, 75);
                colorfilter.ApplyInPlace(video2);
                BlobCounter blobcounter = new BlobCounter();
                blobcounter.MinHeight = 20;
                blobcounter.MaxWidth = 20;
                blobcounter.ObjectsOrder = ObjectsOrder.Size;
                blobcounter.ProcessImage(video2);
                Rectangle[] rect = blobcounter.GetObjectsRectangles();

                if (rect.Length > 0)
                {
                    Rectangle objec = rect[0];
                    Graphics graphic = Graphics.FromImage(video2);
                    using (Pen pen = new Pen(Color.White, 3))
                    {
                        graphic.DrawRectangle(pen, objec);
                    }
                    graphic.Dispose();
                }
                pictureBox2.Image = video2;

            }
            pictureBox1.Image = video2;
        }

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

        private void button2_Click(object sender, EventArgs e)
        {
           pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
        }

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

1 回答 1

0

Stop不建议调用您的视频捕获设备。根据文档:“停止相机的正确方法是发出信号停止然后等待后台线程完成。”

你应该尝试使用这样的东西来停止:

FinalFrame.SignalToStop();
FinalFrame.WaitForStop();

如果使用不当,线程行为可能会变得不可预测,我相信您的问题就在这里。

至于你的第二个问题,问题线是

pictureBox1.Image = video2;

你已经存储video2了,pictureBox2.Image所以我假设你不想对另一个图片框这样做。

于 2015-08-13T03:55:21.107 回答