如何在 EMGUCV 3.1 上截取相机的屏幕截图?
How do I take an Screenshot of my camera on EMGUCV 3.1?
我在EMGU CV上做一个非常简单的程序,所以我需要截取我的相机正在录制的内容并将其保存在特定文件夹中,下面是我的相机捕获代码:
ImageViewer viewer = new ImageViewer();
VideoCapture capture = new VideoCapture();
Application.Idle += new EventHandler(delegate (object sender, EventArgs e)
{
viewer.Image = capture.QueryFrame();
});
viewer.ShowDialog();
I apologize for the simple terms, I still really noob in programming.
您似乎刚刚发布了来自 EmguCV wiki 的标准代码。但让我试着解释一下如何在计算机上显示网络摄像头的视频源并在按下按钮时保存屏幕截图(您必须自己创建所有 UI 元素)。您需要一个带有用于显示图像的 PictureBox 元素和用于保存快照的按钮的表单。
我将通过注释和标准 EmguCV 代码来解释代码中的所有内容:
private Capture capture;
private bool takeSnapshot = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Make sure we only initialize webcam capture if the capture element is still null
if (capture == null)
{
try
{
// Start grabbing data, change the number if you want to use another camera
capture = new Capture(0);
}
catch (NullReferenceException excpt)
{
// No camera has been found
MessageBox.Show(excpt.Message);
}
}
// This makes sure the image will be fitted into your picturebox
originalImageContainer.SizeMode = PictureBoxSizeMode.StretchImage;
// When the capture is initialized, start processing the images in the PorcessFrame method
if (capture != null)
Application.Idle += ProcessFrame;
}
// You registered this method, so whenever the application is Idle, this method will be called.
// This allows you to process a new frame during that time.
private void ProcessFrame(object sender, EventArgs arg)
{
// Get the newest webcam frame
Image<Bgr, double> capturedImage = capture.QueryFrame();
// Show it in your PictureBox. If you don't want to convert to Bitmap you should use an ImageBox (which is an EmguCV element)
originalImageContainer.Image = capturedImage.ToBitmap();
// If the button was clicked indicating you want a snapshot, save the image
if(takeSnapshot)
{
// Save the image
capturedImage.Save(@"C:\your\picture\path\image.jpg");
// Set the bool to false again to make sure we only take one snapshot
takeSnapshot = !takeSnapshot;
}
}
//When clicking the save button
private void SaveButton_Click(object sender, EventArgs e)
{
// Set the bool to true, so that on the next frame processing the frame will be saved
takeSnapshot = !takeSnapshot;
}
希望对您有所帮助。如果还有什么不清楚的,请告诉我!
有时Bgr
不能直接转位图。相反,我确实雇用了
以下几行:
Emgu.CV.Mat capturedImage = capture.QueryFrame();
pictureBox1.Image = capturedImage.Bitmap;
我在EMGU CV上做一个非常简单的程序,所以我需要截取我的相机正在录制的内容并将其保存在特定文件夹中,下面是我的相机捕获代码:
ImageViewer viewer = new ImageViewer();
VideoCapture capture = new VideoCapture();
Application.Idle += new EventHandler(delegate (object sender, EventArgs e)
{
viewer.Image = capture.QueryFrame();
});
viewer.ShowDialog();
I apologize for the simple terms, I still really noob in programming.
您似乎刚刚发布了来自 EmguCV wiki 的标准代码。但让我试着解释一下如何在计算机上显示网络摄像头的视频源并在按下按钮时保存屏幕截图(您必须自己创建所有 UI 元素)。您需要一个带有用于显示图像的 PictureBox 元素和用于保存快照的按钮的表单。
我将通过注释和标准 EmguCV 代码来解释代码中的所有内容:
private Capture capture;
private bool takeSnapshot = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Make sure we only initialize webcam capture if the capture element is still null
if (capture == null)
{
try
{
// Start grabbing data, change the number if you want to use another camera
capture = new Capture(0);
}
catch (NullReferenceException excpt)
{
// No camera has been found
MessageBox.Show(excpt.Message);
}
}
// This makes sure the image will be fitted into your picturebox
originalImageContainer.SizeMode = PictureBoxSizeMode.StretchImage;
// When the capture is initialized, start processing the images in the PorcessFrame method
if (capture != null)
Application.Idle += ProcessFrame;
}
// You registered this method, so whenever the application is Idle, this method will be called.
// This allows you to process a new frame during that time.
private void ProcessFrame(object sender, EventArgs arg)
{
// Get the newest webcam frame
Image<Bgr, double> capturedImage = capture.QueryFrame();
// Show it in your PictureBox. If you don't want to convert to Bitmap you should use an ImageBox (which is an EmguCV element)
originalImageContainer.Image = capturedImage.ToBitmap();
// If the button was clicked indicating you want a snapshot, save the image
if(takeSnapshot)
{
// Save the image
capturedImage.Save(@"C:\your\picture\path\image.jpg");
// Set the bool to false again to make sure we only take one snapshot
takeSnapshot = !takeSnapshot;
}
}
//When clicking the save button
private void SaveButton_Click(object sender, EventArgs e)
{
// Set the bool to true, so that on the next frame processing the frame will be saved
takeSnapshot = !takeSnapshot;
}
希望对您有所帮助。如果还有什么不清楚的,请告诉我!
有时Bgr
不能直接转位图。相反,我确实雇用了
以下几行:
Emgu.CV.Mat capturedImage = capture.QueryFrame();
pictureBox1.Image = capturedImage.Bitmap;