Sid133
Sid133

Reputation: 364

OpenCVsharp4 save Image at max resolution

I am using Opencvsharp from shimat for building an application. Code simply opens camera, saves the image and close it using below code.

using OpenCvSharp;

VideoCapture capture;
Mat frame;

private void btn_Camera_Click(object sender, EventArgs e)
{

    capture = new VideoCapture();          
    frame = new Mat();
    capture.Open(1);
    capture.Read(frame);

    if (capture.Read(frame))
    {
        frame.SaveImage("@test.jpg");
    }

    capture.Release();
}

However the picture is saved at 640x480 resolution whereas the camera is capable of capturing 1280x720 resolution pictures. I tried setting the VideoCapture properties like below

capture.Set(VideoCaptureProperties.FrameHeight, 720);
capture.Set(VideoCaptureProperties.FrameWidth, 1280);

But still the saved image is of 480p resolution. Is there a way to save it at 720p resolution, like the default windows camera app does.

Also I don't want to save it in 480p and then resize to 720p as that doesn't help in getting the details that needs to captured.

I know in opencv Python its possible. Am looking for something similar in C# with Opencvsharp4

Upvotes: 5

Views: 5435

Answers (1)

Peter Wishart
Peter Wishart

Reputation: 12270

When capturing via OpenCvSharp, 640x480 is the default resolution.

You must set the desired resolution before the device is opened (which is done implicitly when you grab frames) e.g.:

    int frameWidth = 1280;
    int frameHeight = 720;
    int cameraDeviceId = 1;
    var videoCapture = VideoCapture.FromCamera(cameraDeviceId);
    if (!videoCapture.Set(VideoCaptureProperties.FrameWidth, frameWidth))
    {
        logger.LogWarning($"Failed to set FrameWidth to {frameWidth}");
    }
    if (!videoCapture.Set(VideoCaptureProperties.FrameHeight, frameHeight))
    {
        logger.LogWarning($"Failed to set FrameHeight to {frameHeight}");
    }
    using (videoCapture)
    {
        videoCapture.Grab();
        var image = videoCapture.RetrieveMat();
        logger.LogInformation($"Image size [{image.Width} x {image.Height}]");
    }

Upvotes: 5

Related Questions