简体   繁体   中英

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

In https://github.com/shimat/opencvsharp/issues/938 they discuss and it looks as they did not find a solution. https://github.com/shimat/opencvsharp/issues/938#issuecomment-790540606 -> They move to use XamlIslands instead. In order to read over the matter, you can look over: How to take high quality photo from integrated camera in WPF .

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) eg:

    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}]");
    }

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