2015-11-13 43 views
1

我在Android應用程序hello-jni.cpp的jni文件夾中有這個C++ OpenCV代碼。我只是想找到並繪製凸起,但由於凸起的方法產生了一個錯誤「無效參數hull[i]。如果我投(vector<point>(hull[i]))程序運行,並生成該錯誤:ConvexHull Android ndk和Opencv中的參數無效

libc "Fatal signal 11 (SIGSEGV) at 0x00000004 (code=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; 
    } 
} 

回答

2

當初始化hull

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

hull尺寸爲0,因爲contours大小爲0。所以,當你訪問hull[i]您正在訪問出界。

聲明hullfindContours

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,和第四個參數returnPoints被忽略時,第二個參數是std::vector。當您使用THRESH_OTSU


thresh值被忽略。


UPDATE

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

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

代碼:

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

感謝您response.I沒有你所說的話,但現在該程序運行並生成此錯誤: OpenCV錯誤:聲明失敗(!fixedSize()|| len ==((vector *)v) - > size()/ esz)in virtu al void cv :: _ OutputArray :: create(int,int const *,int,int,bool,int)const,file/hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matrix。 CPP,行1489 –

+0

@ elnaz.bfrhn適合我。你的錯誤在哪裏? – Miki

+0

正好在這條線凸Hull(Mat(contour [i]),(vector (hull [i]))); –