OpenCV FeatureDetector

我正在尝试编写一个应用SURF对象检测的代码,所以我拿了一个openCV示例(示例3),我开始更新和方法,但是当我在Galaxy S3手机上尝试时,我不断收到运行时错误,我找不到任何可以帮助我解决问题的东西,这是我的代码和我更新的内容:onCameraViewStarted()onCameraFrame()

public class Sample3Native extends Activity implements CvCameraViewListener{

private static final String TAG = "OCVSample::Activity";

private Mat                    mRgba;
private Mat                    mGrayMat;
private CameraBridgeViewBase   mOpenCvCameraView;

Mat descriptors ;           
List<Mat> descriptorsList;

FeatureDetector featureDetector;
MatOfKeyPoint keyPoints;
DescriptorExtractor descriptorExtractor;
DescriptorMatcher descriptorMatcher;**


private BaseLoaderCallback     mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");

                // Load native library after(!) OpenCV initialization
                System.loadLibrary("native_sample");

                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4);
    mGrayMat = new Mat(height, width, CvType.CV_8UC1);
    featureDetector=FeatureDetector.create(4); // SURF= 4;
    descriptorExtractor=DescriptorExtractor.create(2);//SURF = 2
    descriptorMatcher=DescriptorMatcher.create(6); //BRUTEFORCE_SL2 = 6**

}

 public Mat onCameraFrame(Mat inputFrame) {
    inputFrame.copyTo(mRgba);
    //detect_1(0, mRgba.getNativeObjAddr(), keyPoints.getNativeObjAddr());
    //Now mRgba contains the current frame ( start manipulation part)
    //detecting keypoints
    featureDetector.detect(mRgba, keyPoints);
    //draw keypoints
   // Features2d.drawKeypoints(mRgba, keyPoints, mRgba);
    //finding descriptors
    descriptorExtractor.compute(mRgba, keyPoints, descriptors);
    //Matcher between 2 images or set of images
    // Note: training set and query set are handled here! (in matcher)
   //descriptorsList = descriptorMatcher.getTrainDescriptors();
    //descriptorsList.add(descriptors);
   // descriptorMatcher.add(descriptorsList);

    //Imgproc.cvtColor(mRgba, mGrayMat, Imgproc.COLOR_RGBA2GRAY);
    //FindFeatures(mGrayMat.getNativeObjAddr(), mRgba.getNativeObjAddr());

    return mRgba;
}
}

注意:我已尝试评论除方法之外的所有内容,并且仍然在手机上给出运行时错误。featureDetector.detect(mRgba, keyPoints)onCameraFrame()


答案 1

如果我没有记错的话,OpenCV SURF功能检测器仅适用于灰度图像。因此,请尝试在调用方法中的 copyTo 之后添加以下内容:onCameraFrame()

cvtColor(mRgba, mGrayMat, COLOR_RGBA2GRAY);

答案 2

您确定以正确的方式使用 SIFT 吗?据我所知,SIFT和SURF不包括在OpenCV Android的分发包中。要使用它们,你需要编译非自由模块并在你的项目中使用它。所以,你需要做的是创建一个NDK项目,将非自由模块编译为一个独立的库。然后使用此库编译程序。然后,您应该能够构建应用程序。您可以参考本教程

获取 jni 库后,您可以轻松地将其包装到 JAVA JNI 接口。然后,您应该能够在Android应用程序中使用JAVA接口。


推荐