0

I'm trying to use the OpenCvSharp library to capture images from a camera, but what happens is that I only get colored stripes that move on the screen (as shown in the images).

colored stripe

The code I'm using is as follows:

csharp Copy code using OpenCvSharp;

class Program
{
    static void Main()
    {
        // Create the video capture object
        using (VideoCapture capture = new VideoCapture(0, VideoCaptureAPIs.DSHOW))
        {
            capture.Set(VideoCaptureProperties.FrameWidth, 320);
            capture.Set(VideoCaptureProperties.FrameHeight, 240);
            // Check if the camera was opened correctly
            if (!capture.IsOpened())
            {
                Console.WriteLine("Could not open the camera.");
                return;
            }

            // Create a window to display the camera video
            Cv2.NamedWindow("Camera Capture");

            // Loop to capture and display the camera video
            while (true)
            {
                // Capture a frame from the camera
                Mat frame = new Mat();
                capture.Read(frame);

                // Check if the frame is empty
                if (frame.Empty())
                    break;

                // Display the frame in the window
                Cv2.ImShow("Camera Capture", frame);

                // Check if the 'Esc' key was pressed (27 is the ASCII code for 'Esc')
                if (Cv2.WaitKey(500) == 27)
                    break;
            }

            // Close the window and release resources
            Cv2.DestroyAllWindows();
        }
    }
}

Camera I'm using: Logitech C920

Specifications of the machine I'm using:

Operating System: Windows 11 Processor: i5-10400 RAM: 16GB

Note: I'm getting colored stripes instead of the actual camera image. Can anyone help me identify and resolve the issue? Thank you!

I already checked the privacy settings of the camera and everything is ok.

The camera also works in other programs like logitech capture

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • This code works as is for me.... Granted I don't have the same camera that you have. Only suggestion I have would be to try usong other video capture APIs. And just to make sure you do have both `OpenCvSharp4` and `OpenCvSharp4.WIndows` packages installed ? – Irwene Jul 04 '23 at 15:20
  • You can give a try a library called Emgu CV, It's better than Open CV Sharp – Son of Man Jul 04 '23 at 16:31
  • @Irwene i have both packages installed – Sábio dos seis caminhos Jul 04 '23 at 16:55
  • @VibrantWaves i already try use Emgu CV , but also dont work – Sábio dos seis caminhos Jul 04 '23 at 16:58
  • @Sábiodosseiscaminhos, I posted an answer, how many cameras do you have attached to your system? – Son of Man Jul 04 '23 at 17:26
  • potentially an issue with the requested resolution... also try the MSMF backend. it's flakier than DSHOW but worth a try. -- make sure the camera isn't broken. open it in VLC or any other camera viewer. -- and DO reduce the delay for WaitKey. if you only read a frame every half second, all those frames made by the camera will queue up and cause trouble (latency at best, maybe a crash) – Christoph Rackwitz Jul 04 '23 at 20:25

1 Answers1

0

I have tried Emgu CV and it works on my end. Follow the following steps to capture frames from your camera to your application.

  1. Open your project and go to the Nuget Package Manager

  2. Search for Emgu.CV.runtime.windows in the Browse tab

  3. Install the library to your project, if it fails to install then downgrade to a version that is compatible with your .NET framework.

  4. Please get the index of the Logitech camera you have attached to your computer system, the webcam is the default and is assigned an index of 0.

  5. Subscribe to the application Idle event to capture frames from the camera in the desired index and display in your windows forms app.

See the code attached below for more details as I have tested it on my webcam and it works. I have targeted .NET framework 6.0 in my project, yours may be different.

using System.Text.RegularExpressions;
using Emgu.CV;
using Emgu.CV.Structure;

namespace CameraStream
{
    public partial class Form1 : Form
    {
        //initialize an object to use for capturing frames
        VideoCapture capture;
        public Form1()
        {
            InitializeComponent();
            //connect to the primary camera, here if you have more than one camera then just find the index of yours and substitute in the constructor below
         //the Webcam has a default index of 0
            capture = new VideoCapture(0);
           
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //subscribe to the application idle event and use it
            //to dispay captured frames in the picture box
            Application.Idle += Application_Idle;
        }

        private void Application_Idle(object? sender, EventArgs e)
        {
            //grab a frame from the camera
            var image = capture.QueryFrame().ToImage<Bgr, byte>();
            //convert the frame to something compatible with Picture Box object
            //or something that can be displayed in a picture box
            var barray = image.ToJpegData(95);
            var stream = new MemoryStream(barray);
            var bitmap = new Bitmap(stream);
            pictureBox1.Image = bitmap;
        }
        //override onclosed and release resources
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            //release memory used by the camera and application
            capture.Dispose();
        }
    }
}
Son of Man
  • 1,213
  • 2
  • 7
  • 27