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).
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