This article is the author original, reprint please indicate the source--negative Bibi TargetA long time ago notes, share to everyone ... Used in Opencv4androidbackground modeling classes mainly include: Backgroundsubtractor, Backgroundsubtractormog, BackgroundSubtractorMOG2, BACKGROUNDSUBTRACTORKNN, The main use of the method to make a summary. Using the Api,android programming provided by OPENCV can achieve a richer visual processing effect. After several attempts, finally combed out the OPENCV background modelling in the Android use method. Backgroundsubtractor's Java The API is significantly different in the opencv2.x and 3.x versions, 3.x provides a richer API interface, cancels the Gaussian background modeling 1 (BACKGROUNDSUBTRACTORMOG) in 2.4, retains the Gaussian background modeling 2 (BackgroundSubtractorMOG2), In KNN background modeling (BACKGROUNDSUBTRACTORKNN). It also provides many property methods to access the Backgroundsubtractor background model, such as Getbackgroundimage. Not much nonsense to say, summed up as follows: first, the Java interface to achieve background modelingBoth the opencv2.x and the 3.x,backgroundsubtractor classes must be defined before the frame loop is processed, for example, to OpenCV Tutorial2 The routine adds a foreground detection method for the BackgroundSubtractorMOG2 class. 1, define a BackgroundSubtractorMOG2 object, the Tutorial2 routine is defined as the private global variable for the used Mat class object, and the reference gives the definition:
Public class extends Implements CvCameraViewListener2 { ... Private Mat Mrgba; Private Mat Mintermediatemat; Private Mat Mgray; Private BackgroundSubtractorMOG2 mog2;
2, Mog2 object initialization, Tutorial2 routines in the Oncameraviewstarted method to initialize the Mat object, the reference to execute:
Public voidOncameraviewstarted (intWidthintheight) {Mrgba=NewMat (height, width, cvtype.cv_8uc4); Mintermediatemat=NewMat (height, width, cvtype.cv_8uc4); Mgray=NewMat (height, width, cvtype.cv_8uc1); MOG2=NewBackgroundSubtractorMOG2 ();//opencv2.x Initialization Method//mog2 = Video.createbackgroundsubtractormog2 ();//opencv3.x Initialization Method}
It is important to note that the opencv2.x initialization BackgroundSubtractorMOG2 object is implemented by new, but in the 3.x version is via MOG2 = VIDEO.CREATEBACKGROUNDSUBTRACTORMOG2 () To achieve.
3, perform the background modeling, direct the canny method in the routine to change to Backgroundsubtractor:
Case BUTTON_GETBG: = Inputframe.rgba (); Bg2.apply (Inputframe.gray (), Mintermediatemat,0.01); // The foreground is saved in the Mintermediatemat // bg2.getbackgroundimage (MBG); // opencv3.x provides the Get background function
since opencv2.x does not provide the Getbackgroundimage method, let's toss and toss it through JNI to achieve it: Second, the implementation of background modeling through JNIfirst you have to roughly JNI, not here to start speaking, recommend to look at the http://www.cnblogs.com/linguanh/p/4624768.html, prepare for work. 1. Define the Getbgimage class to inherit the BackgroundSubtractorMOG2 class and define the native method in it:
/*** Because there is no API interface for BackgroundSubtractorMOG2 class getbackgroundimage in OpenCV2.4, * so the method of getting the background is defined by the Findbackground class. The initialization of the Findbackground class is consistent with the * BackgroundSubtractorMOG2 class, and must be done outside of the frame loop to get the background method in the frame loop body. */ Public classGetbgimageextendsbackgroundsubtractormog2{//inherit from BackgroundSubtractorMOG2, its constructor method inherits the parent class construction method PublicGetbgimage (intHistoryfloatVarthreshold,Booleanbshadowdetection) { Super(History, Varthreshold, bshadowdetection); return; } //Findbackground extends BackgroundSubtractorMOG2 extends Backgroundsubtractor extends algorithm class//The algorithm class defines a long type of nativeobj Public Longgetnativeobjaddr () {returnNativeobj; } Public voidFindfeature (Mat MGr, Mat MBG) {findfeatures (Nativeobj, Mgr.nativeobj, mbg.nativeobj); return; } Public Static native voidFindfeatures (LongNativeobj,LongMgr_nativeobj,Longmbg_nativeobj);}
2, write Java_com_example_myapplication_getbgimage.cpp code:
#include <jni.h>#include<opencv2/core/core.hpp>#include<opencv2/core/core.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<opencv2/features2d/features2d.hpp>#include<vector>#include<android/log.h>#include<sys/time.h>#include<opencv2/video/background_segm.hpp>#define LOGE (...) __android_log_print (Android_log_error,"ProjectName", __va_args__) using namespace std;using namespace CV;//exception handling, you can notStatic voidThrowjavaexception (JNIEnv *env,ConstStd::exception *e,Const Char*method) {std::string what= "Unknown Exception"; Jclass JE= 0; if(e) {std::string exception_type= "Std::exception"; if(dynamic_cast<ConstCv::exception*>(e)) {Exception_type= "Cv::exception"; Je= Env->findclass ("Org/opencv/core/cvexception"); } what= Exception_type + ":" + e->what (); } if(!je) JE = env->findclass ("Java/lang/exception"); Env-Thrownew (JE, what.c_str ()); LOGE ('%s caught%s ', method, What.c_str ()); (void) method;//avoid "unused" warning}//method Body, be sure to start with extern "C" {extern "C"{JniexportvoidJnicall Java_com_example_myapplication_getbgimage_findfeatures (jnienv*, Jobject, Jlong Self, Jlong Addrgray, Jlong ADDRBG);//in the Java native method, there are three main parameters, corresponding to self, addrgray, ADDRBG, which is the custom getbgimage class long type nativeobj, It can be used as a pointer to the Getbgimage class object you define. //when the object pointer is passed into C + + via JNI, you can point to the object through the pointer of the Getbgimage's parent class BackgroundSubtractorMOG2, and call C's interface through the pointer, This enables the invocation of the Getbackgroundimage method. JniexportvoidJnicall java_com_example_myapplication _getbgimage_findfeatures (jnienv*env, Jobject, Jlong Self, Jlong Addrgray, Jlong addrbg) {Static Const CharMethod_name[] = "Findfeatures->getbackgroundimage () getfrontmaskimage ()"; Try{LOGE ('%s ', method_name); Mat& mGr = * (mat*) Addrgray; Mat& mBG = * (mat*) ADDRBG; CV::BACKGROUNDSUBTRACTORMOG2* Me = (cv::backgroundsubtractormog2*) Self; Me->operator () (MGr, mbg,0.001); Me-getbackgroundimage (MGR); return; }Catch(ConstStd::exception &e) {throwjavaexception (env,&e, method_name); } Catch (...) {throwjavaexception (env,0, method_name); } return; }}Note: C + + functions that need to be called must be declared in native, and the declared file name is consistent with the CPP file being written, for example Java_com_example_myapplication_getbgimage_ Findfeatures corresponds to the findfeatures. 3. Generate. h header files and. So library filesgenerate the. h header file (enter the command in the as Terminal: javah-d &location\app\src\main\jni-classpath &location\app\src\main\java Com.example.Myapplication.GetBGImage), if all goes well, will not error, and generate java_com_example_myapplication_getbgimage.h files;To write the android.mk file, application.mk file, please refer to Tutorial2 or related blog, a hint: if you need to MK multiple CPP files (such as 1.cpp,2.cpp), please mark the local_src_files: =1.cpp local_src_files: = 2.cpp, of course, there are more general wording, you can find it yourself, no longer described here. compile the. So file, as terminal into the App\src\main\jni directory, enter Ndk-build, If all goes well, you will be prompted to generate a series of. so files, and your project's main directory will generate Libs and obj two directories. This means that the compilation is successful, load this. So file in your mainactivity, loading method:baseloadercallback loading System.loadlibrary ("xxxx"); . So file names are generally libxxxx.soThis step is very critical, but also very error-prone, please be patient debugging, if my method is not applicable can search other people's blog to see. 4, complete the above steps, you can rest assured that the use of Getbgimage Findfeature method. Step can refer to Java background modeling, first predefined Getbgimage class object
Public class extends Implements CvCameraViewListener2 {... Private Mat Mrgba; Private Mat Mintermediatemat; Private Mat Mgray; Private Getbgimage mog2;
Initialize in the Oncameraviewstarted method, here
Public void oncameraviewstarted (intint height) { new Mat (height, width, cvtype.cv_8uc4); New Mat (height, width, cvtype.cv_8uc1); New Mat (height, width, cvtype.cv_8uc1); // the Getbgimage class inherits from the BackgroundSubtractorMOG2, and its construction method inherits the parent class construction method New Getbgimage (30,16,false);
The parameters here are to be debugged with reference to background modeling requirements.
calling Findfeature in the Oncameraframe method
Public Mat oncameraframe (cvcameraviewframe inputframe) { = inputframe.gray (); Bg2. Findfeature (Mgray,mintermediatemat);
above.
Opencv4android Background modeling (MOG, MOG2)