opencv 调用VideoWriter.getBackendName()时出错

8yparm6h  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(164)

这是我的密码

class VideoWriterTest {
    @Test
    fun write() {
        OpenCV.loadLocally()
        println("opencv version=${Core.getVersionString()}")

        val fourcc = VideoWriter.fourcc('a', 'c', 'v', '1')
        val size1 = Size(100.0, 100.0)
        val outputFile= File("temp.mp4")
        outputFile.delete()
        val writer = VideoWriter(outputFile.path, fourcc, 30.0, size1)
        println( writer.backendName)
        assertTrue(writer.isOpened)
    }
}

当运行时,我得到这个错误。

opencv version=4.6.0

cv::Exception: OpenCV(4.6.0) /home/runner/work/opencv/opencv/opencv-4.6.0/modules/videoio/src/cap.cpp:666: error: (-215:Assertion failed) api != 0 in function 'getBackendName'

CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.6.0) /home/runner/work/opencv/opencv/opencv-4.6.0/modules/videoio/src/cap.cpp:666: error: (-215:Assertion failed) api != 0 in function 'getBackendName'
]
    at app//org.opencv.videoio.VideoWriter.getBackendName_0(Native Method)
    at app//org.opencv.videoio.VideoWriter.getBackendName(VideoWriter.java:380)
    at app//com.katalisindonesia.heavi.analytic.service.VideoWriterTest.write(VideoWriterTest.kt:26)

我用的是org.openpnp:opencv:4.6.0-0。有什么想法吗?
编辑:这段代码在macOS上工作,但在linux上不工作。
EDIT2:macOS中的输出

opencv version=4.6.0
AVFOUNDATION
ktecyv1j

ktecyv1j1#

我把代码修改成这个

class VideoWriterTest {
    @Test
    fun write() {
        OpenCV.loadLocally()
        println("opencv version=${Core.getVersionString()}")

        for (i in 0..2400) {
            if (Videoio.hasBackend(i)) {
                println("hasBackend $i")
            }
            if (Videoio.isBackendBuiltIn(i)) {
                println("hasBackendBuiltIn $i")
            }
        }

        val fourcc = VideoWriter.fourcc('M', 'J', 'P', 'G')
        val size1 = Size(100.0, 100.0)
        val outputFile = File("temp.avi")
        outputFile.delete()
        val writer = VideoWriter(outputFile.path, Videoio.CAP_OPENCV_MJPEG, fourcc, 30.0, size1)
        assertTrue(writer.isOpened)
    }
}

然后就过去了
输出:

opencv version=4.6.0
hasBackend 200
hasBackendBuiltIn 200
hasBackend 2000
hasBackendBuiltIn 2000
hasBackend 2200
hasBackendBuiltIn 2200

结论:

  1. openpnp不捆绑ffmpeg
  2. fourcc必须是大写的MJPG。当我使用小写时,它会失败。
    1.文件名必须使用.avi扩展名

相关问题