【问题标题】:OpenCVsharp4 save Image at max resolutionOpenCVsharp4以最大分辨率保存图像
【发布时间】:2022-01-09 09:50:13
【问题描述】:

我正在使用 shimat 的 Opencvsharp 来构建应用程序。代码只需打开相机,保存图像并使用以下代码将其关闭。

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();
}

但是,图片以 640x480 分辨率保存,而相机能够捕捉 1280x720 分辨率的图片。 我尝试设置VideoCapture 属性,如下所示

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

但保存的图像仍然是 480p 分辨率。有没有办法将其保存为 720p 分辨率,就像默认的 Windows 相机应用一样。

另外,我不想将其保存为 480p,然后调整为 720p,因为这无助于获取需要捕获的细节。

我知道在 opencv Python 中它是可能的。我正在使用 Opencvsharp4 在 C# 中寻找类似的东西

【问题讨论】:

  • 相机型号是什么,可以分享一下吗?
  • 罗技 c270 720p webacm
  • 你也试过了吗:capture.set(cv::CAP_PROP_FRAME_WIDTH, 1280); capture.set(cv::CAP_PROP_FRAME_HEIGHT, 720);
  • @YunusTemurlenk 这是用于 C++ 的。我需要的是 C#。对于 C# 它的 capture.Set(VideoCaptureProperties.FrameHeight, 720); & capture.Set(VideoCaptureProperties.FrameWidth, 1280); ,这也不起作用。
  • 你不能指望所有的相机都能正常使用 opencv 后端。每种相机都有自己的驱动程序和opencv,有些支持,有些不支持。您可以查看this answer

标签: c# windows opencv webcam opencvsharp


【解决方案1】:

通过 OpenCvSharp 捕获时,640x480 是默认分辨率。

您必须在设备打开之前设置所需的分辨率(这在您抓取帧时隐式完成)例如:

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

【讨论】:

  • 将尝试并确认。尽管我在上面的任务中使用了类似的东西。记录时的分辨率是更新的,但 saveimage 仅将其保存为 640x480。
  • 也许尝试保存到新文件。当我使用image.SaveImage 保存时。文件分辨率与 Width 和 Height 属性一致。
猜你喜欢
  • 2015-10-20
  • 1970-01-01
  • 2012-07-11
  • 2021-09-19
  • 2016-07-25
  • 2011-09-12
  • 2013-04-11
  • 1970-01-01
相关资源
最近更新 更多