标签:属性 自己 htm tor 版本 ref xxx ado system
public class Tutorial2Activity extends Activity implements CvCameraViewListener2 { …… private Mat mRgba; private Mat mIntermediateMat; private Mat mGray; private BackgroundSubtractorMOG2 mog2;
public void onCameraViewStarted(int width, int height) { mRgba = new Mat(height, width, CvType.CV_8UC4); mIntermediateMat = new Mat(height, width, CvType.CV_8UC4); mGray = new Mat(height, width, CvType.CV_8UC1); mog2 = new BackgroundSubtractorMOG2(); //OpenCV2.x初始化方法 //mog2 = video.createBackgroundSubtractorMOG2();//OpenCV3.x初始化方法 }
需要注意的是OpenCV2.x初始化BackgroundSubtractorMOG2对象是通过new来实现,但是在3.x版本中是通过mog2 = Video.createBackgroundSubtractorMOG2()来实现。
case BUTTON_GETBG: mRgba = inputFrame.rgba(); bg2.apply(inputFrame.gray(),mIntermediateMat,0.01);//前景保存在mIntermediateMat中 //bg2.getBackgroundImage(mBG); //OpenCV3.x提供获取背景函数
/** * 因为OpenCV2.4中没有提供BackgroundSubtractorMOG2类getBackgroundImage的API接口, * 所以通过FindBackground类定义获取背景的方法,FindBackground类的初始化工作和 * BackgroundSubtractorMOG2类一致,必须在Frame循环之外完成,获取背景的方法在帧循环体内。 */ public class GetBGImage extends BackgroundSubtractorMOG2{ //继承自BackgroundSubtractorMOG2,其构造方法继承父类构造方法 public GetBGImage (int history, float varThreshold, boolean bShadowDetection) { super( history, varThreshold, bShadowDetection) ; return; } //FindBackground extends BackgroundSubtractorMOG2 extends BackgroundSubtractor extends Algorithm类 //algorithm类定义了long类型的nativeobj public long getNativeObjAddr() { return nativeObj; } public void FindFeature(Mat mGr, Mat mBG){ FindFeatures(nativeObj, mGr.nativeObj, mBG.nativeObj); return; } public static native void FindFeatures(long nativeObj,long mGr_nativeObj, long mBG_nativeObj); }
2、编写Java_com_example_Myapplication_GetBGImage.cpp代码:
#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; //异常处理,可以不要 static void throwJavaException(JNIEnv *env, const std::exception *e, const char *method) { std::string what = "unknown exception"; jclass je = 0; if(e) { std::string exception_type = "std::exception"; if(dynamic_cast<const cv::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 } //方法主体,一定要以extern "C"{开头 extern "C" { JNIEXPORT void JNICALL Java_com_example_Myapplication_GetBGImage_FindFeatures(JNIEnv* , jobject , jlong self, jlong addrGray, jlong addrBG); //在java native方法中传入的参数主要有三个,分别对应self、addrGray、addrBG,其中self是自定义的GetBGImage类long类型的nativeobj,它可以作为一个指针指向你定义的GetBGImage类对象。 //当对象指针通过jni传入C++后,你可以通过GetBGImage的父类BackgroundSubtractorMOG2的指针指向这个对象,并在通过指针调用C的接口,从而实现对getBackgroundImage方法的调用。 JNIEXPORT void JNICALL Java_com_example_Myapplication _GetBGImage_FindFeatures(JNIEnv* env, jobject , jlong self, jlong addrGray, jlong addrBG) { static const char method_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(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { throwJavaException(env, 0, method_name); } return; } }
public class Tutorial2Activity extends Activity implements CvCameraViewListener2 { …… private Mat mRgba; private Mat mIntermediateMat; private Mat mGray; private GetBGImage mog2;
public void onCameraViewStarted(int width, int height) { mRgba = new Mat(height, width, CvType.CV_8UC4); mIntermediateMat = new Mat(height, width, CvType.CV_8UC1); mGray = new Mat(height, width, CvType.CV_8UC1); //GetBGImage类继承自BackgroundSubtractorMOG2,其构造方法继承父类构造方法 bg2 = new GetBGImage(30,16,false);
这里的参数请参照背景建模的需求进行调试。
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { mGray = inputFrame.gray(); bg2.FindFeature(mGray,mIntermediateMat );
标签:属性 自己 htm tor 版本 ref xxx ado system
原文地址:http://www.cnblogs.com/mar-q/p/7493397.html