Camera Capture in 7 lines of code: Difference between revisions

From EMGU
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
'''This example requires [[Version_History#Emgu.CV-1.5.0.0|Emgu CV 1.5.0.0]]'''
'''This example has been tested on [[Version_History#Emgu.CV-4.6.0.0|Emgu CV 4.6.0.0]]'''


Excluding the using statements in the beginning, only 7 lines of code are required to perform a camera capture loop.
Only a few lines of code are required to perform a camera capture loop.
<source lang="csharp">
 
<syntaxhighlight lang="csharp">
using Emgu.CV;
using Emgu.CV;
using Emgu.CV.UI;
using Emgu.CV.Structure;
using System.Drawing;
using System.Windows.Forms;
...
...


ImageViewer viewer = new ImageViewer(); //create an image viewer
            String win1 = "Test Window (Press any key to close)"; //The name of the window
Capture capture = new Capture(); //create a camera captue
            CvInvoke.NamedWindow(win1); //Create the window using the specific name
Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
            using (Mat frame = new Mat())  
{  //run this until application closed (close button click on image viewer)
            using (VideoCapture capture = new VideoCapture())
  viewer.Image = capture.QueryFrame(); //draw the image obtained from camera
                while (CvInvoke.WaitKey(1) == -1)
});
                {
viewer.ShowDialog(); //show the image viewer
                    capture.Read(frame);
</source>
                    CvInvoke.Imshow(win1, frame);
                }
</syntaxhighlight>

Latest revision as of 21:11, 10 January 2023

This example has been tested on Emgu CV 4.6.0.0

Only a few lines of code are required to perform a camera capture loop.

using Emgu.CV;
...

            String win1 = "Test Window (Press any key to close)"; //The name of the window
            CvInvoke.NamedWindow(win1); //Create the window using the specific name
            using (Mat frame = new Mat()) 
            using (VideoCapture capture = new VideoCapture())
                while (CvInvoke.WaitKey(1) == -1) 
                {
                    capture.Read(frame);
                    CvInvoke.Imshow(win1, frame);
                }