Java, OpenCV VideoWriter isOpened 返回始终为false。

huangapple go评论62阅读模式
英文:

Java, OpenCV VideoWriter isOpened returns always false

问题

我正在使用Java OpenJdk 14.0.2和OpenCV-440,同时在Windows 10上运行。我的JavaFX应用程序应该捕获摄像头(或任何其他视频设备)的帧并将这些帧存储为视频文件,例如avi。

以下是我的代码:

public void run() {

    Mat frame = new Mat();
    VideoCapture videoCapture = new VideoCapture(0);
    videoCapture.read(frame);

    Size frameSize = new Size((int) videoCapture.get(Videoio.CAP_PROP_FRAME_WIDTH), (int) videoCapture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
    int fourcc = VideoWriter.fourcc('x', '2', '6', '4');
    VideoWriter writer = new VideoWriter();
    //如果myuniquefile%02d.jpg使用任何一种视频扩展名而不是jpg,则不起作用(例如avi)
    writer.open("images/myuniquefile%02d.jpg", fourcc,
            videoCapture.get(Videoio.CAP_PROP_FPS), frameSize, true);
    
    while (isRunning) {
        if (videoCapture.read(frame)) {
            writer.write(frame);
        }
    }
    videoCapture.release();
    writer.release();
}

这段代码运行良好,但只要我将“.jpg”更改为.avi等扩展名,它就不再工作。对于上面的代码,VideoWriter.isOpened()返回true,对于带“.avi”的相同代码,返回false。我尝试了许多文件扩展名和编解码器(VideoWriter.fourcc)的组合,但从未成功打开。

让我们继续看我的设置,我正在使用Intellij(2020.1.2),并且openCV 440是这样链接的:
Java, OpenCV VideoWriter isOpened 返回始终为false。

唯一的附加库是javafx-sdk-14.0.2.1

我一开始参考了这个示例,但对我来说从未按照那种方式工作过。
我非常感谢任何建议。

BR Michael

英文:

I am using Java OpenJdk 14.0.2 and OpenCV-440 as well while everything is running on Windows 10. My JavaFX application is supposed to capture the frames of a webcam (or any other video device) and store the frames as a video file, for example avi.

Here is my code:

   public void run() {

        Mat frame = new Mat();
        VideoCapture videoCapture = new VideoCapture(0);
        videoCapture.read(frame);

        Size frameSize = new Size((int) videoCapture.get(Videoio.CAP_PROP_FRAME_WIDTH), (int) videoCapture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
        int fourcc = VideoWriter.fourcc('x', '2','6','4');
        VideoWriter writer = new VideoWriter();
        //if myuniquefile%02d.jpg is using any kind of video extension instead, it is not working (e.g. avi)
        writer.open("images/myuniquefile%02d.jpg", fourcc,
                videoCapture.get(Videoio.CAP_PROP_FPS), frameSize, true);
        
        while (isRunning) {
            if (videoCapture.read(frame)) {
                writer.write(frame);
            }
        }
        videoCapture.release();
        writer.release();
    }

This code is working fine, but as soon I change the ".jpg" to an extension like .avi it is not working any longer. For the code above VideoWriter.isOpened() returns true and for the same code with ".avi" it returns false. I tried a lot of combinations of file extensions and codecs (VideoWriter.fourcc), but it is never open.

Lets go ahead to my settings, I am using Intellij (2020.1.2) and openCV 440 is linked this way:
Java, OpenCV VideoWriter isOpened 返回始终为false。

The only additional lib is the javafx-sdk-14.0.2.1

I was going with this example in the first place, but it was never working that way for me.
I am very grateful for any suggestions

BR Michael

答案1

得分: 4

因为您正在创建H264编解码器的VideoWriter,但尝试获取.avi编解码器的视频。

以下是正确的.avi.mp4编解码器格式:

int fourcc = VideoWriter.fourcc('M', 'J', 'P', 'G');
writer.open("images/out.avi", fourcc,
                    videoCapture.get(Videoio.CAP_PROP_FPS), frameSize, true);

这里有一个类似的问题,还有OpenCV文档中关于VideoWriter的信息。

英文:

Because you are creating H264 codec VideoWriter but trying to get .avi codec video.

Here is the correct codec format for .avi or .mp4:

int fourcc = VideoWriter.fourcc('M', 'J','P','G');
writer.open("images/out.avi", fourcc,
                videoCapture.get(Videoio.CAP_PROP_FPS), frameSize, true);

Here is similar problem and also opencv documentation for VideoWriter

huangapple
  • 本文由 发表于 2020年8月13日 18:05:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63392771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定