Ryc
Ryc

Reputation: 3

How do I take an Screenshot of my camera on EMGUCV 3.1?

I'm doing a very simple program on EMGU CV, so I need to take a screenshot of what my camera is recording and save it in a specific folder, here follows my code of camera capture:

        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.

Upvotes: 0

Views: 4355

Answers (2)

m. öztürk
m. öztürk

Reputation: 1

Sometimes Bgr cannot be converted to bitmap directly. Instead I do employ following lines:

 Emgu.CV.Mat capturedImage = capture.QueryFrame();
 pictureBox1.Image = capturedImage.Bitmap;

Upvotes: 0

Jurjen
Jurjen

Reputation: 1396

It seems like you just posted the standard code from the EmguCV wiki. But let me try to explain how you can show a video feed of your webcam on your computer and save a screenshot when you press a button (you'll have to create all the UI elements yourself). You'll need a form with an PictureBox element to display the image and a button to save a snapshot.

I'll explain everything in the code through comments and work from the standard EmguCV code:

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;
}

Hope this helps you. Let me know if anything is still unclear!

Upvotes: 1

Related Questions