I have searched a lot for this problem, but I did not find any solution for this problem.
I have a program which receives that JPEG file data through socket (in bytes), then I convert the byte array to OpenCV Mat
object as blow:
Mat mat = Imgcodecs.imdecode(new MatOfByte(byteArray), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
It converts well without any exception, then (for test case to ensure the data is OK) I convert the Mat
object to BufferedImage
and then show it in a JPanel
, everything goes well without any exception.
But when I apply:
mog2.apply(mat, foregroundImg, -1); // tried 1, 0, 0.003 etc
OR
knn.apply(mat, foregroundImg, -1); // tried 1, 0, 0.003 etc
the mat to BackgroundSubtractor
(any of BackgroundSubtractorMOG2
or BackgroundSubtractorKNN
) then it throws the exception with the following 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
Then I change the type of Mat as follow, but throws the same error
Mat newMat = new Mat();
mat.convertTo(newMat, CvType.CV_8UC3)
For test I have done some other operation on this mat to ensure that it works with other OpenCV function, but it all works well like:
Convert to gray
Imgproc.cvtColor(mat, gartImg, Imgproc.COLOR_RGB2GRAY);
Do threshold
Imgproc.threshold(gartImg, gartImg, 100, 255, Imgproc.THRESH_BINARY);
And so on... all works without any exception.
Special case: If I use a webcam or read frames from a video file, everything goes well without any exception.
Now I don't know why this error or exception happens, is this a bug with the BackgroundSubtractor
or there is anything wrong in data?
Special Note: I am using OpenCV latest version (3.0) in Java Languange