在 c# 中使用 Pinovke 使用 opencv 捕获视频

Capturing video with opencv using Pinovke in c#

本文关键字:opencv 视频 使用 Pinovke      更新时间:2023-10-16

我正在使用openCV用C#编写WindowsForm应用程序。我想从网络摄像头捕获视频并在窗口中显示它,我想使用 P/Invoke 来做到这一点,我得到了 c++ 代码,但我不知道如何在 c# 中做到这一点。

#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened())  // check if we succeeded
    return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
    Mat frame;
    cap >> frame; // get a new frame from camera
    cvtColor(frame, edges, COLOR_BGR2GRAY);
    GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
    Canny(edges, edges, 0, 30, 3);
    imshow("edges", edges);
    if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}

我在 GitHub 中找到了一个链接,它将从图像人脸检测中检测人脸

与我想从网络摄像头捕获视频的方式完全相同。任何参考链接都会有所帮助。?

#region cameracapture
   if (comboBox1.Text == "Capture From Camera")
   {
     try
     {
       _capture = null;
       _capture = new Capture(0);
       _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS, 30);
       _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 240);
       _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 320);
       Time_Label.Text = "Time: ";
       Codec_lbl.Text = "Codec: ";
       Frame_lbl.Text = "Frame: ";
       webcam_frm_cnt = 0;
       cam = 1;
       Video_seek.Value = 0;
       Application.Idle += ProcessFrame;
       button1.Text = "Stop";
       comboBox1.Enabled = false;
     }
     catch (NullReferenceException excpt)
     {
        MessageBox.Show(excpt.Message);
     }
   }
#endregion cameracapture 

来源:代码项目