为什么我在 PictureBox2 中看不到框架

问题描述 投票:0回答:1

我正在尝试获取网络摄像头的实时视频帧。我的目的是仅查看 numericUpDown1 定义的帧。 即使对于工作代码和零错误,pictureBox2 中也没有任何内容。 为什么我在 PictureBox2 中看不到框架? 我很感激任何帮助。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;


using Emgu.CV;
using Emgu.CV.Structure;
using static System.Net.Mime.MediaTypeNames;

namespace CameraCapture_Test
{
    public partial class Form1 : Form
    {
        VideoCapture capture;

        double totalFrame;
        double fps;
        int frameNo;
        bool isReadingFrame;
        Mat mat = new Mat();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if(capture == null)
            {
                capture = new VideoCapture();
            }

            capture.ImageGrabbed += Capture_ImageGrabbed;
            capture.Start();

        }

        private void Capture_ImageGrabbed(object sender, EventArgs e)
        {
            capture.Read(mat);
            pictureBox1.Image = mat.ToBitmap();
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

            totalFrame = capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameCount);
            fps = capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps);



        }

        private void Stop_Click(object sender, EventArgs e)
        {
            capture.Stop();
        }

        private void getFrameBtn_Click(object sender, EventArgs e)
        {
            if (capture == null)
            {
                return;
            }
            isReadingFrame = true;
            ReadAllFrames();

        }

        private async void ReadAllFrames()
        {

            while (isReadingFrame == true && frameNo < totalFrame)
            {

                frameNo += Convert.ToInt16(numericUpDown1.Value);
                capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.PosFrames, frameNo);
                capture.Read(mat);
                pictureBox2.Image = mat.ToBitmap();
                pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                await Task.Delay(1000 / Convert.ToInt16(fps));
                label1.Text = frameNo.ToString() + "/" + totalFrame.ToString();
            }
        }

        private void formCloseBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

我是图像处理新手,所以任何建议都会非常酷。

c# video-capture emgucv bitmapimage webcam-capture
1个回答
0
投票

我通过添加计时器找到了解决方案。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;


using Emgu.CV;
using Emgu.CV.Structure;
using static System.Net.Mime.MediaTypeNames;

namespace CameraCapture_Test
{
    public partial class Form1 : Form
    {
        VideoCapture capture;

        double totalFrame;
        double fps;
        int frameNo;
        bool isReadingFrame;
        Mat mat = new Mat();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (capture == null)
            {
                capture = new VideoCapture();
            }

            capture.ImageGrabbed += Capture_ImageGrabbed;
            capture.Start();

        }

        private void Capture_ImageGrabbed(object sender, EventArgs e)
        {
            capture.Read(mat);
            pictureBox1.Image = mat.ToBitmap();
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

            totalFrame = capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameCount);
            fps = capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps);

        }

        private void Stop_Click(object sender, EventArgs e)
        {
            capture.Stop();
        }

        private void getFrameBtn_Click(object sender, EventArgs e)
        {
            if (capture == null)
            {
                return;
            }
            isReadingFrame = true;

            timer1.Enabled = true;
            timer1.Interval = Convert.ToInt16(numericUpDown1.Text);
            timer1.Start();
        }

        private async void ReadAllFrames()
        {

            pictureBox2.Image = mat.ToBitmap();
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;

        }

        private void formCloseBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ReadAllFrames();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.