I am trying to use BackgroundSubtractor
class of OpenCV in Java, which two sub-classes BackgroundSubtractorMOG2
and BackgroundSubtractorKNN
and going subtract the background from a video stream frame, these frame are coming from Network IP camera in bytes
in JPEG
format, after that I covert the bytes to OpenCV Mat
object in this way:
Mat mat = Imgcodecs.imdecode(new MatOfByte(data), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
and then pass it to BackgroundSubtractor
apply method:
mog2.apply(mat, foregroundImg, -1); // tried 1, 0, 0.003 etc
// or
//knn.apply(mat, foregroundImg, -1); // tried 1, 0, 0.003 etc
It will throws Exception with the following message:
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
Now I don't know what can be the reason. Is it a bug with the BackgroundSubtractor
or there is any wrong in code.
For ensuring that the converted Mat has no problem, I used some other OpenCV functions to test if says the same Error, but works will all of the other functions like:
Convert to gray
Imgproc.cvtColor(mat, grayImg, Imgproc.COLOR_RGB2GRAY);
Do threshold
Imgproc.threshold(grayImg, grayImg, 100, 255, Imgproc.THRESH_BINARY);
Note: When I use webcam (integrated or USB) or reading frames from a video file, everything going well with same code.
Edited: The whole error message:
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
]