【问题标题】:<unknown> Error occured in detectAndCompute()<unknown> detectAndCompute() 发生错误
【发布时间】:2019-07-24 18:56:23
【问题描述】:

当我在调试器下运行我的代码时,它显示了一个&lt;unknown&gt; 错误:

detector->detectAndCompute( matInput, noArray(), keypoints2, descriptors2 );

喜欢这个

`matInput` is from `inputFrame.rgba()` 

Java 代码在:

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) 

这不为空。

这是一部分 native-lib.cpp

std::vector<Mat> trainImages;

std::vector< std::vector<DMatch> > knn_matches;
std::vector<KeyPoint> keypoints1;
std::vector<KeyPoint> keypoints2;

Mat matImg;

static void createKeypointsAndDescriptors(const Mat& matInput) {

    int minHessian = 400;
    cv::Ptr<SURF> detector = SURF::create( minHessian ); 
    std::vector<KeyPoint> temp_keypoints;
    std::vector<std::vector<KeyPoint>> temp_keypoints1;
    Mat temp_descriptors;
    std::vector<Mat> temp_descriptors1;
    Mat descriptors2;
    std::vector< std::vector<DMatch> > temp_knn_matches;

    for(int i = 0; i < 2; i++) {
        detector->detectAndCompute( trainImages[i], noArray(), temp_keypoints, temp_descriptors );
        temp_keypoints1.push_back(temp_keypoints);
        temp_descriptors1.push_back(temp_descriptors);
    }

    detector->detectAndCompute( matInput, noArray(), keypoints2, descriptors2 );        

    Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);
    int max = 0;

    for(int i = 0; i < 2; i++) {

        if(temp_keypoints[i].size >= 2 && keypoints2.size() >= 2)
            matcher->knnMatch( temp_descriptors1[i], descriptors2, temp_knn_matches, 2 );

        if(max < temp_knn_matches.size()) {
            max = temp_knn_matches.size();
            keypoints1 = temp_keypoints1[i];
            matImg = trainImages[i];
            knn_matches = temp_knn_matches;
        }
    }
}

编辑

这是我的示例输入图像

这是与代码相关的图像。我使用java 代码将asset 目录中的图像发送到JNI

extern "C"
JNIEXPORT void JNICALL
Java_com_example_surfwithflann2_MainActivity_sendImages(JNIEnv *env, jobject instance,
                                                       jlongArray tempAddrObj_) {

    if(trainImages.size() == 0) {
        int length = env->GetArrayLength(tempAddrObj_);
        jlong *tempAddrObj = env->GetLongArrayElements(tempAddrObj_, NULL);

        for(int i = 0; i < length; i++) {
            cv::Mat &tempImage = *(cv::Mat *)tempAddrObj[i];
            trainImages.push_back(tempImage);
        }
        env->ReleaseLongArrayElements(tempAddrObj_, tempAddrObj, 0);
    } else {
        __android_log_print(ANDROID_LOG_DEBUG, "TAG", "already received from java");
    }

}

【问题讨论】:

    标签: android c++ opencv android-ndk java-native-interface


    【解决方案1】:

    要缩小范围,请尝试将 detectAndCompute() 拆分为 detect()compute()

    #include <stdio.h>
    #include <iostream>
    #include "opencv2/core.hpp"
    #include "opencv2/features2d.hpp"
    #include "opencv2/xfeatures2d.hpp"
    #include "opencv2/highgui.hpp"
    
    using namespace cv;
    using namespace cv::xfeatures2d;
    
    std::vector<cv::KeyPoint> keypoints2;
    cv::Mat descriptors2;
    
    static void createKeypointsAndDescriptors(const cv::Mat& matInput) {
      //-- Step 1: Detect the keypoints using SURF Detector, compute the descriptors
        double minHessian = 400;
        Ptr<SURF> detector = SURF::create();
        detector->setHessianThreshold(minHessian);
    
        detector->detect(matInput, keypoints2);
        detector->compute(matInput, keypoints2, descriptors2);
        // detector->detectAndCompute( matInput, noArray(), keypoints2, descriptors2 );        
    ...
    }
    

    链接:

    cv::xfeatures2d::SURF Class Reference
    cv::Feature2D Class Reference
    cv::xfeatures2d::AffineFeature2D Class Reference

    【讨论】:

    • 非常感谢!解决了。 matInput 不为空,但 keypoints2 和描述符为空。你知道为什么会这样吗??
    • @MJ Kwon keypoints2descriptors2 为空,因为它们未被检测到(请参阅更新的代码)。你用的是什么图片?
    • 你说的是std::vector trainImages吗?这些是正常的 jpeg 图像。请参阅编辑部分。
    猜你喜欢
    • 2013-11-18
    • 2018-02-17
    • 2023-02-02
    • 2022-01-09
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多