简体   繁体   中英

How to Display Image in Emgucv

Here I am trying to display a live image stream in imagebox through the laptop webcam for which i have used emgucv-windows-x86 2.2.1.1150 . (Note: I am using windows 64bit).

I have used a button whose text is Start! initially. What we want is that when the Start! button is pressed, the camera should start working and the image stream should be visible in our ImageBox . If the stream is on, then the start button should display Pause and pressing it should pause the stream and vice versa.

The problem is that when I press the Start! button, even if there is no error at all, instead of showing live stream image it shows nothing in the ImageBox even though webcam is working.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
namespace CameraCapture
{
  public partial class cameracapture : Form
  {
    //declaring global variables
    private Capture capture;        //takes images from camera as image frames
    private bool captureInProgress; // checks if capture is executing

    public cameracapture()
    {
      InitializeComponent();
    }
    //------------------------------------------------------------------------------//
    //Process Frame() below is our user defined function in which we will create an EmguCv 
    //type image called ImageFrame. capture a frame from camera and allocate it to our 
    //ImageFrame. then show this image in ourEmguCV imageBox
    //------------------------------------------------------------------------------//

    private void ProcessFrame(object sender, EventArgs arg)
    {
      Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
      CamImageBox.Image = ImageFrame;
    }
    private void cameracapture_Load(object sender, EventArgs e)
    {

    }

    private void btnStart_Click(object sender, EventArgs e)
    {
      #region if capture is not created, create it now
      if (capture == null)
      {
        try
        {
            capture = new Capture();
        }
        catch (NullReferenceException excpt)
        {
            MessageBox.Show(excpt.Message);
        }
      }
      #endregion

      if (capture != null)
      {
        if (captureInProgress)
        {  //if camera is getting frames then stop the capture and set button Text
            // "Start" for resuming capture
            btnStart.Text = "Start!"; //
            Application.Idle -= ProcessFrame;
        }
        else
        {
            //if camera is NOT getting frames then start the capture and set button
            // Text to "Stop" for pausing capture
            btnStart.Text = "Stop";
            Application.Idle += ProcessFrame;
        }

        captureInProgress = !captureInProgress;
    }

    }
    private void ReleaseData()
    {
      if (capture != null)
        capture.Dispose();
    }
  }
}

Can't find what's is wrong with the code.

But here is a simpler way to show the concept.

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
Capture capture = new Capture(); //create a camera captue
Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
{  //run this until application closed (close button click on image viewer)
   viewer.Image = capture.QueryFrame(); //draw the image obtained from camera
});
viewer.ShowDialog(); //show the image viewer

This question might help you with your problem.

In my case, i used to get black and normal photo intermittently. The issue was my base program was instantiating the emgucv exe more than once. This was leading to multiple handles to the laptop camera at the same time. I ensured only one capture process should run at a time. This solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM