1

A while ago I wrote some cool vision stuff in c++. Later I found out my code needs to be added into an existing executable that is written in c#. Therefor, I want to write the same vision algorithmes in c# in such a way that my code can be run as an executable.

I have downloaded Emgu 3.3 and I use OpenCV 3.4 and Visual Studio 2017. C# is new for me. I did the tutorial where you make your form, give it some buttons and assign functions to the buttons. That all worked fine.

Then I started to look for ways on how to import a videofeed from my webcam into my form. So I just want to get a live feed from my webcam in my form. After 3 days of frustration I have gotten totally confused with all the references. Which one do I need which one do I not need. Every code that I find and try to implement as a start seems to give a different issue. And all I want is just one clean screen with my camera feed.

So sorry for this super long story, but can someone please help me.

I know I need to create a picturebox. I just want to obtain a VideoCapture from my webcam and send this directly to the picturebox. As as start of course. From there on I can continue figuring stuff out but I need the base.

From there on I would like to perform imageprocessing on the videofeed and display another picturebox with the edited videofeed. But that's all for later!

I hope my question isn't too vague since I also do not supply some code..

Thank you

Lauran
  • 73
  • 1
  • 1
  • 9
  • "Webcam" and "Videofeed" are a bit vague. Is it a USB or Network Cam? If Network: What protocols are supported? RTSP/RTP? What Codecs are supported? ... – Fildor Mar 15 '18 at 13:21
  • It is the webcam on my laptop, a usb webcam is also available – Lauran Mar 15 '18 at 13:22
  • If my question is super stupid and there is a tutorial perfect for this than of course I would love to do the tutorial. I thought I first give it a try myself and then ask it here – Lauran Mar 15 '18 at 13:23
  • Erm, no it's not super stupid. It actually is quite interesting. I found these, maybe you can make something out of them: https://www.codeproject.com/Questions/569093/Howpluscanplusyouplusstreamplusliveplusvideoplusfr , https://stackoverflow.com/questions/342774/how-can-i-stream-webcam-video-with-c – Fildor Mar 15 '18 at 13:25
  • And even something with emgu: https://stackoverflow.com/questions/18727747/how-to-get-video-stream-from-webcam-in-emgu-cv – Fildor Mar 15 '18 at 13:26
  • Thank you. I had already tried them all though haha. The last one with the emgu gave a lot of errors. The other answer from qaplqq works though :) – Lauran Mar 16 '18 at 09:20

1 Answers1

7

A example of showing your Webcam in WinForm using VideoCapture of Emgu 3.3 is as below:

public partial class Form1 : Form
{
    VideoCapture _capture;
    private Mat _frame;

    private void ProcessFrame(object sender, EventArgs e)
    {
        if (_capture != null && _capture.Ptr != IntPtr.Zero)
        {
            _capture.Retrieve(_frame, 0);
            pictureBox1.Image = _frame.Bitmap;  
        }
    }

    public Form1()
    {
        InitializeComponent();

        _capture = new VideoCapture(0);
       
      
        _capture.ImageGrabbed += ProcessFrame;
        _frame = new Mat();
        if (_capture != null)
        {
            try
            {
                _capture.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
funie200
  • 3,688
  • 5
  • 21
  • 34
qaplqq
  • 96
  • 4
  • 1
    This one works perfectly for me as a start to continue my code! Thank you ;) – Lauran Mar 16 '18 at 09:20
  • 2
    For the other people who have the same question as I had. I also added 'using Emgu.CV' at the top of my code. And I added refferences to the Emgu.CV.World.dll and System.ServiceModel.dll files – Lauran Mar 16 '18 at 09:26
  • @Lauran Why do you need to add references to those DLL? – GTHell Nov 21 '18 at 04:07