美文网首页
C#:调用电脑本机摄像头

C#:调用电脑本机摄像头

作者: 大龙10 | 来源:发表于2022-09-07 16:13 被阅读0次

《学习OpenCV(中文版)》

作者:(美)布拉德斯基(Bradski,G.),
(美)克勒(Kaehler,A.) 著
出版社:清华大学出版社
出版时间:2009年10月

一、调用电脑本机摄像头

  • 1、调用电脑本机摄像头,读取视频文件
  • 2、循环顺序读取视频中的每一帧
  • 3、如何退出循环。

二、程序

参考: 
shimat的实例:(https://github.com/shimat/opencvsharp_samples/)

1、程序C220907:调用电脑本机摄像头播放视频的OpenCV程序。

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        private void button2_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0); 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var capture = new VideoCapture(0);
            int sleepTime = (int)Math.Round(1000 / capture.Fps);
            // 声明实例 Mat类
            Mat image = new Mat();

            bool stop = false;
            // 进入读取视频每镇的循环
            while (!stop)
            {
                capture.Read(image);
                //判断是否还有没有视频图像 
                if (image.Empty())
                    break;
                // 在picturebox中播放视频, 需要先转换成bitmap格式
                pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);

                if (Cv2.WaitKey(sleepTime) >= 0)
                    stop = true;
            }
            image.Release();
        }
    }
}

三、运行结果

相关文章

网友评论

      本文标题:C#:调用电脑本机摄像头

      本文链接:https://www.haomeiwen.com/subject/hspgnrtx.html