0

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
]
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
  • The problem doesn't seem to be in the background subtraction part, but rather in the decoding part. Your JPEG images are corrupted (or, at least, OpenCV JPEG decoder can't decode them). – Miki Dec 28 '15 at 15:29
  • I also have the same idea as yours, in exception it says there found two extra bytes, now how to detect and delete them, or how to correctly decode the bytes to Mat object? Thanks – Bahramdun Adil Dec 28 '15 at 15:33
  • You're decoding correctly. The problem is in the stream. You should check why it's corrupted. Are you sure you wait to receive the whole image? – Miki Dec 28 '15 at 15:36
  • I am getting the bytes via a Java wrapper to the IP cam .dll files, after when I connected to the camera then I use the function something like `getJPEG(byte[] bytes);`. then it should be OK, I tried very very much, but really did not find any good solution for this problem. – Bahramdun Adil Dec 28 '15 at 15:41
  • And also how it works with the other functions although it is a corrupted image? – Bahramdun Adil Dec 28 '15 at 16:04
  • That's actually wierd, but since I cannot reproduce this, I can't tell.. – Miki Dec 28 '15 at 16:30
  • Thank you sir! I have solved one problem which says the corrupted image, I think that was an OpenCV warning. But now the second Exception still exist, which says: `OpenCV Error: Bad flag....Unrecognized or unsupported array type`, I think it is the real error. – Bahramdun Adil Dec 28 '15 at 16:34
  • Ok, that's much easier to correct. Please post the whole error – Miki Dec 28 '15 at 16:36
  • Thanks in advance if this problem corrected!! I edited the question, it was too long to put here. – Bahramdun Adil Dec 28 '15 at 16:48
  • That's not as I expected... The problem still seems not to be in the background subtraction part, but the way you create the matrices. – Miki Dec 28 '15 at 16:59
  • You can check this question about the same problem, maybe more clear than this question. http://stackoverflow.com/questions/34368799/backgroundsubtractor-throws-unrecognized-or-unsupported-array-type-in-function – Bahramdun Adil Dec 29 '15 at 01:52

0 Answers0