Emgu CV3+C#图像处理(四):使用EmguCV获取摄像头、读取视频

首先按Emgu CV3+C#图像处理(一):Emgu CV安装配置与使用中的操作配置EmguCV,并添加系统动态链接库中的
“System.Windows.Forms.dll”。

获取摄像头

获取摄像头

示例:

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

using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.UI;
using Emgu.CV.Structure;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //获取摄像头
            ImageViewer viewer = new ImageViewer();
            //捕获
            Capture capture = new Capture();
            Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
            {
                //获得的图像
                viewer.Image = capture.QueryFrame();
            });
            //显示
            viewer.ShowDialog(); 

        }
    }
}

这里写图片描述

读取视频

读取视频并获得视频帧率信息。

示例:

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

using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.UI;
using Emgu.CV.Structure;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ImageViewer viewer = new ImageViewer();
            //读取视频
            Capture capture = new Capture(@"E:\image\video\bike.avi");
            //视频帧率
            Console.WriteLine("Frame rate = " + capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps));
            Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
            {
                //获得的图像
                viewer.Image = capture.QueryFrame();
            });
            viewer.ShowDialog();
        }
    }
}

这里写图片描述


Emgu CV Library Documentation::Capture Class

  • 8
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是一个基本的使用Emgu CV实现图像分类的例程,包括数据的预处理、神经网络的构建、训练和测试: ```csharp using Emgu.CV; using Emgu.CV.ML; using Emgu.CV.Structure; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace ImageClassification { class Program { static void Main(string[] args) { // 设置数据集的路径 string dataPath = @"C:\data\images"; // 读取数据集 List<Image<Gray, byte>> images = LoadImages(dataPath); // 划分训练集和测试集 List<Image<Gray, byte>> trainImages = images.Take(500).ToList(); List<Image<Gray, byte>> testImages = images.Skip(500).ToList(); // 准备训练和测试数据 Matrix<float> trainData = PrepareData(trainImages); Matrix<float> trainLabels = PrepareLabels(trainImages); Matrix<float> testData = PrepareData(testImages); Matrix<float> testLabels = PrepareLabels(testImages); // 创建神经网络 ANN_MLP neuralNetwork = new ANN_MLP(); neuralNetwork.SetLayerSizes(new int[] { trainData.Rows, 100, 50, 10 }); neuralNetwork.TermCriteria = new MCvTermCriteria(100, 0.01); neuralNetwork.SetActivationFunction(ANN_MLP.AnnMlpActivationFunction.SigmoidSym, 0, 0); neuralNetwork.SetTrainMethod(ANN_MLP.AnnMlpTrainMethod.Backprop, 0.1, 0.1); // 训练神经网络 neuralNetwork.Train(trainData, trainLabels); // 测试神经网络 float[] expectedLabels = new float[testLabels.Rows]; float[] predictedLabels = new float[testLabels.Rows]; for (int i = 0; i < testLabels.Rows; i++) { float[] testDataRow = testData.GetRow(i); float[] testLabelRow = testLabels.GetRow(i); neuralNetwork.Predict(testDataRow, predictedLabels); expectedLabels[i] = Array.IndexOf(testLabelRow, 1); } int correctPredictions = expectedLabels.Zip(predictedLabels, (e, p) => e == p ? 1 : 0).Sum(); float accuracy = (float)correctPredictions / testLabels.Rows; Console.WriteLine($"Test accuracy: {accuracy}"); } // 加载图像 static List<Image<Gray, byte>> LoadImages(string dataPath) { List<Image<Gray, byte>> images = new List<Image<Gray, byte>>(); foreach (string imagePath in Directory.GetFiles(dataPath)) { Image<Gray, byte> image = new Image<Gray, byte>(imagePath); images.Add(image); } return images; } // 准备数据 static Matrix<float> PrepareData(List<Image<Gray, byte>> images) { int numImages = images.Count; int numPixels = images[0].Width * images[0].Height; Matrix<float> data = new Matrix<float>(numImages, numPixels); for (int i = 0; i < numImages; i++) { Image<Gray, byte> image = images[i]; float[] pixels = image.Data.SelectMany(row => row).Select(x => (float)x / 255f).ToArray(); data.SetRow(i, pixels); } return data; } // 准备标签 static Matrix<float> PrepareLabels(List<Image<Gray, byte>> images) { int numImages = images.Count; int numClasses = 10; // 假设有10个类别 Matrix<float> labels = new Matrix<float>(numImages, numClasses); for (int i = 0; i < numImages; i++) { int label = i % numClasses; labels[i, label] = 1f; } return labels; } } } ``` 这个例程实现了一个简单的多层感知器神经网络,用于对图像进行分类。它假设数据集中有10个类别,每个类别有50张图像。它将前500张图像用于训练,后50张用于测试。在训练神经网络时,它使用反向传播算法和Sigmoid激活函数,并设置了一些超参数。在测试时,它计算了分类的准确度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值