簡體   English   中英

ConvexHull Android ndk和Opencv中的參數無效

[英]Invalid arguments in ConvexHull Android ndk and Opencv

我在Android應用程序hello-jni.cpp jni文件夾中有此C ++ OpenCV代碼。 我只想查找和繪制凸包,但是引起hull[i]的原因,凸包方法會生成錯誤“ 無效參數 ”。 如果我強制轉換(vector<point>(hull[i])) ,程序將運行並生成此錯誤:

libc“致命信號11(SIGSEGV)在0x00000004(代碼= 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]);

由於默認情況returnPoints 3個參數orientation為false,而當第2個參數為std::vector時,第4個參數returnPoints被忽略。


使用THRESH_OTSU時, thresh值將被忽略。


UPDATE

Android NDK似乎存在一些問題。 一個簡單的解決方法是:

  • 禁用錯誤的無效參數,或者
  • 使用以下

碼:

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