ConvertHull Android ndk和Opencv中的参数无效

Invalid arguments in ConvexHull Android ndk and Opencv

本文关键字:参数 无效 Opencv Android ndk ConvertHull      更新时间:2023-10-16

我在android应用程序hello-jni.cpp的jni文件夹中有这个C++OpenCV代码。我只想找到并绘制convexull,但由于hull[i],convexhall方法生成了一个错误"无效参数"。如果我投射(vector<point>(hull[i])),程序运行并生成以下错误:

libc"0x00000004处的致命信号11(SIGSEGV)(代码=1)"

任何帮助真的非常感谢。

#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <android/log.h>
#include <opencv/cv.h>
#include <vector>
#include <cmath>
#include <opencv2/opencv.hpp>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define  LOG_TAG    "hellojni"
#define  LOGI(...)    __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)   __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define ORIGCOL2ANDROIDORGCOL CV_BGR2BGRA
using namespace std;
using namespace cv;
extern "C" {
    JNIEXPORT jint JNICALL      Java_com_elmira_getconvexhull_MainActivity_convertNativeGray(
        JNIEnv*, jobject, jlong addrRgba, jlong addrGray);
    JNIEXPORT jint JNICALL     Java_com_elmira_getconvexhull_MainActivity_convertNativeGray(
        JNIEnv*, jobject, jlong addrRgba, jlong addrGray) {
        Mat& mRgb = *(Mat*)addrRgba;
        Mat& mGray = *(Mat*)addrGray;
        int conv = 0;
        jint retVal;
        Mat src; Mat src_gray;
        src = mRgb;
        cvtColor(src, src_gray, CV_BGR2GRAY);
        blur(src_gray, src_gray, Size(3, 3));
        Mat src_copy = src.clone();
        Mat threshold_output;
        vector<vector<Point> > contours;
        vector<vector<Point> > hull(contours.size());
        vector<Vec4i> hierarchy;
        int thresh = 100;
        threshold(src_gray, threshold_output, thresh, 255, THRESH_BINARY || CV_THRESH_OTSU);
        /// Find contours
        findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

        __android_log_print(ANDROID_LOG_INFO, "inmethod", "size %d *** %d", contours.size(), threshold_output.cols);
        for (int i = 0; i < contours.size(); i++)
        {
            convexHull(Mat(contours[i]), hull[i], false, false);
        }
        retVal = (jint)conv;
        return retVal;
    }
}

初始化hull

vector<vector<Point> > contours;
vector<vector<Point> > hull(contours.size());

hull的大小是0,因为contours的大小是0。因此,当您访问hull[i]时,您的访问是越界的。

findContours:之后声明hull

 findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
 vector<vector<Point> > hull(contours.size());

您也可以简单地将convexHull称为:

 convexHull(contours[i], hull[i]);

由于第三个参数orientation默认为false,并且当第二个参数为std::vector时,第四个参数returnPoints被忽略。


使用THRESH_OTSU时会忽略thresh值。


更新

安卓NDK似乎有一些问题。一个简单的解决方法是:

  • 禁用无效参数的错误,或者
  • 使用以下内容

代码:

Mat mHull;
convexHull(Mat(contours[i]), mHull, false, true);
hull[i].assign(mHull.begin<Point>(), mHull.end<Point>());