简体   繁体   English

ConvexHull Android ndk和Opencv中的参数无效

[英]Invalid arguments in ConvexHull Android ndk and Opencv

I have this C++ OpenCV code in my jni folder of android application hello-jni.cpp . 我在Android应用程序hello-jni.cpp jni文件夹中有此C ++ OpenCV代码。 I just want to find and draw convexhull but cause of hull[i] the convexhull method generate an error " invalid arguments ". 我只想查找和绘制凸包,但是引起hull[i]的原因,凸包方法会生成错误“ 无效参数 ”。 If I cast (vector<point>(hull[i])) the program runs and generates this error: 如果我强制转换(vector<point>(hull[i])) ,程序将运行并生成此错误:

libc "Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1)" libc“致命信号11(SIGSEGV)在0x00000004(代码= 1)”

Any help really really appreciated. 任何帮助真的很感激。

#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;
    }
}

When you initialize hull 初始化hull

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

hull size is 0 , since contours size is 0. So when you access hull[i] you are accessing out of bounds. hull尺寸为0 ,因为contours尺寸为0。因此,当您访问hull[i]您将访问超出范围。

Declare hull after findContours : findContours之后声明hull

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

You can also simply call convexHull as: 您也可以简单地将convexHull调用为:

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

since 3rd argument orientation is false by default, and 4th argument returnPoints is ignored when 2nd argument is a std::vector . 由于默认情况returnPoints 3个参数orientation为false,而当第2个参数为std::vector时,第4个参数returnPoints被忽略。


thresh value is ignored when you use THRESH_OTSU . 使用THRESH_OTSU时, thresh值将被忽略。


UPDATE UPDATE

It seems that there are some problems with Android NDK. Android NDK似乎存在一些问题。 A simple workaround is: 一个简单的解决方法是:

  • disable the error for invalid arguments, or 禁用错误的无效参数,或者
  • use the following 使用以下

Code: 码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM