I am working on a robotics project using an Android phone as the main processor and camera for motion detection. I received the Android binary package from OpenCV and installed it correctly. I can record images using the built-in OpenCV camera and display them on the screen. However, I am having problems using the background subtraction class. I can create a new BackgroundSubtractorMOG object in the constructor, but when I try to run the code below, it forces the shutdown. I get the error message "Only single and three-channel 8-bit images are supported in BackgroundSubtractorMOG" from the native code. I tried changing Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA to Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGB and then it doesnβt force shutdown, but all I get is a black screen. I am sure bmp is still null with FRAME_RGB because the screen remains black and the fps counter that I drew right after the bitmap (removed from the code below for clarity and as a troubleshooting step) is not displayed.
I looked at the OpenCV C ++ code for this function ( line 388 here ), and the image type error occurs if the image type is not CV_8UC1 or CV_8UC3, so I tried using java CvType.CV_8UC3 instead of Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA in capture.retrieve (), but it is forcibly closed, and I got the error "Output frame format is not supported."
I assume that I just had a problem with type conversion, but I canβt understand in what life image types compatible with OpenCV for Android change with their usual image types that are documented. Any help would be appreciated.
Variables:
private SurfaceHolder mHolder; private VideoCapture mCamera; private Mat mRgba; private Mat mFGMask; private BackgroundSubtractorMOG mBGSub;
My SurfaceView run () function:
public void run() { Bitmap bmp = null; synchronized (this) { if (mCamera == null) break; if (!mCamera.grab()) { Log.e(TAG, "mCamera.grab() failed"); break; } processFrame(mCamera); bmp = Bitmap.createBitmap(mFGMask.cols(), mFGMask.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(mFGMask, bmp); } if (bmp != null) { Canvas canvas = mHolder.lockCanvas(); if (canvas != null) { canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null); mHolder.unlockCanvasAndPost(canvas); } bmp.recycle(); } }
The processFrame () function specified in run ():
protected void processFrame(VideoCapture capture) { capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); mBGSub.apply(mRgba, mFGMask); }
Edit:
Solution that ended:
protected void processFrame(VideoCapture capture) { capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGB);
java android opencv computer-vision background-subtraction
Redpeasant
source share