Android端OpenCV的ORB特征点检测

不知道android调用opencv的可以看这里.

1、Java代码

private Mat mIntermediateMat;
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        mIntermediateMat = inputFrame.rgba();
        Orbcreate(mIntermediateMat.nativeObj);
        FAST_num=getOrbnum();
        match_num=getMatchnum();
        num_FAST.setText("ORB角点数量:" + FAST_num" );
        return mIntermediateMat;
    }
public native void Orbcreate(long rgba);
public native int getOrbnum();

2、布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/num_FAST"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="null"
        android:textSize="15sp"/>

    <org.opencv.android.JavaCameraView
        android:id="@+id/image_manipulations_activity_surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
var foo = 'bar';

3、jni的c++代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

int Orbnum=0;
extern "C"
JNIEXPORT void JNICALL
Java_com_example_camerasensor_Main2Activity_Orbcreate(JNIEnv *env, jobject thiz, jlong jrgba) {
    // TODO: implement Orbcreate()
    Mat img=*(Mat *) jrgba;
    Mat &rgba = *(Mat *) jrgba;
    vector<KeyPoint> keypoints1,keypoints2;
    Mat descriptors1,descriptors2;
    Ptr<ORB> orb = ORB::create(500, 1.2f, 8, 31, 0, 2, ORB::HARRIS_SCORE,31,20 );
    /*
 * ORB算法
 *  nfeatures - 最多提取的特征点的数量;
 *  scaleFactor - 金字塔图像之间的尺度参数,类似于SIFT中的kk;
 *  nlevels – 高斯金字塔的层数;
 *  edgeThreshold – 边缘阈值,这个值主要是根据后面的patchSize来定的,靠近边缘
 *  edgeThreshold以内的像素是不检测特征点的。
 * firstLevel - 看过SIFT都知道,我们可以指定第一层的索引值,这里默认为0。
 * scoreType - 用于对特征点进行排序的算法,你可以选择HARRIS_SCORE,也可以选择
 * FAST_SCORE,但是它也只是比前者快一点点而已。
 * patchSize – 用于计算BIREF描述子的特征点邻域大小。
 * fastThreshold 没解释
 * */

    //检测角点位置
    orb->detect(img,keypoints1);

    //根据角点位置计算brief描述子
    orb->compute(img,keypoints1,descriptors1);

//    rgba = Scalar::all(0);
    drawKeypoints(img,keypoints1,rgba, Scalar::all(-1),DrawMatchesFlags::DEFAULT);

    Orbnum=descriptors1.rows;
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_camerasensor_Main2Activity_getOrbnum(JNIEnv *env, jobject thiz) {
    // TODO: implement getOrbnum()
    return Orbnum;
}

4、效果

Android端OpenCV的ORB特征点检测_第1张图片

你可能感兴趣的:(Android端OpenCV的ORB特征点检测)