我正在尝试在Java中使用BackgroundSubtractor
类OpenCV,这两个子类BackgroundSubtractorMOG2
和BackgroundSubtractorKNN
并从视频流帧中减去背景,这些帧即将到来来自bytes
JPEG
格式的网络IP摄像机,然后以这种方式将字节转换为OpenCV Mat
对象:
Mat mat = Imgcodecs.imdecode(new MatOfByte(data), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
然后将其传递给BackgroundSubtractor
apply方法:
mog2.apply(mat, foregroundImg, -1); // tried 1, 0, 0.003 etc
// or
//knn.apply(mat, foregroundImg, -1); // tried 1, 0, 0.003 etc
它会抛出异常,并显示以下消息:
Corrupt JPEG data: 2 extraneous bytes before marker 0xd9
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\core\src\array.cpp, line 2494
现在我不知道是什么原因。它是BackgroundSubtractor
的错误还是代码中有任何错误。
为了确保转换后的Mat没有问题,我使用了一些其他的OpenCV函数来测试是否显示相同的错误,但是所有其他函数都可以工作:
转换为灰色
Imgproc.cvtColor(mat, grayImg, Imgproc.COLOR_RGB2GRAY);
做阈值
Imgproc.threshold(grayImg, grayImg, 100, 255, Imgproc.THRESH_BINARY);
注意:当我使用网络摄像头(集成或USB)或从视频文件中读取帧时,所有内容都使用相同的代码。
已编辑:整个错误消息:
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\core\src\array.cpp, line 2494
CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\core\src\array.cpp:2494: error: (-206) Unrecognized or unsupported array type in function cvGetMat
]
at com.test.subtractor.VideoStream.start(VideoStream.java:107)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\core\src\array.cpp:2494: error: (-206) Unrecognized or unsupported array type in function cvGetMat
]