在凸形轮廓的Android NDK和OpenCV参数无效 [英] Invalid arguments in ConvexHull Android ndk and Opencv
问题描述
我有这个C ++ OpenCV的code在Android应用程序 HELLO-jni.cpp
我的JNI文件夹中。我只是想找到并绘制凸形轮廓,但船体的原因[I]
的凸形轮廓的方法产生错误无效的参数的。如果我投(矢量<&点GT;(船体由[i]))
程序运行,并生成此错误:
libc的致命的信号11(SIGSEGV)为0x00000004(code = 1)
块引用>任何帮助真的AP preciated。
的#include< jni.h>
#包括LT&; opencv2 /核心/ core.hpp>
#包括LT&; opencv2 / highgui / highgui.hpp>
#包括LT&; opencv2 / imgproc / imgproc.hpp>
#包括LT&;机器人/ log.h>
#包括LT&; OpenCV的/ cv.h>
#包括LT&;矢量>
#包括LT&;&CMATH GT;
#包括LT&; opencv2 / opencv.hpp>
#包括LT&;&string.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;#定义LOG_TAGhellojni
#定义LOGI(...)__android_log_print(ANDROID_LOG_INFO,LOG_TAG,__ VA_ARGS__)
#定义LOGE(...)__android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__ VA_ARGS__)
#定义ORIGCOL2ANDROIDORGCOL CV_BGR2BGRA使用命名空间std;
使用命名空间的简历;为externC{ 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){ 垫&安培; mRgb = *(*垫)addrRgba;
垫&安培; mGray = *(*垫)addrGray; INT CONV = 0;
jint retVal的; 垫SRC;垫src_gray;
SRC = mRgb; cvtColor(SRC,src_gray,CV_BGR2GRAY);
模糊(src_gray,src_gray,尺寸(3,3)); 垫src_copy = src.clone();
太threshold_output;
矢量<矢量<点和GT; >轮廓;
矢量<矢量<点和GT; >船体(contours.size());
矢量< Vec4i>层次结构;
INT脱粒= 100; 阈值(src_gray,threshold_output,脱粒,255,THRESH_BINARY || CV_THRESH_OTSU); ///查找轮廓
findContours(threshold_output,轮廓,层次,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,点(0,0));
__android_log_print(ANDROID_LOG_INFO,inmethod,大小%d个***%D,contours.size(),threshold_output.cols); 的for(int i = 0; I< contours.size();我++)
{
凸形轮廓(马太福音(轮廓[I]),船体[I],假的,假的);
}
retVal的=(jint)CONV;
返回retVal的;
}
}
解决方案在初始化
船体
矢量<矢量<点和GT; >轮廓;
矢量<矢量<点和GT; >船体(contours.size());
船体
尺寸0
,因为轮廓
尺寸是0。所以,当你访问船体[I]
您正在访问出界。声明
船体
在findContours
:findContours(threshold_output,轮廓,层次,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,点(0,0));
矢量<矢量<点和GT; >船体(contours.size());您也可以简单地调用
凸形轮廓
为:凸形轮廓(轮廓[I],船体[I]);
由于第三个参数
定位
默认是假的,第四个参数returnPoints
时,第二个参数是被忽略的std ::矢量
。
脱粒
当您使用值被忽略THRESH_OTSU
。更新
似乎有一些问题与Android NDK。一个简单的解决方法是:
- 禁用错误无效的参数,或者
- 使用以下
code:
垫mHull;
凸形轮廓(马太福音(轮廓[I]),mHull,假的,真正的);
船体[I] .assign(mHull.begin&所述;点和GT;(),mHull.end&所述;点和GT;());I have this C++ OpenCV code in my jni folder of android application
hello-jni.cpp
. I just want to find and draw convexhull but cause ofhull[i]
the convexhull method generate an error "invalid arguments". If I cast(vector<point>(hull[i]))
the program runs and generates this error:libc "Fatal signal 11 (SIGSEGV) at 0x00000004 (code=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
vector<vector<Point> > contours; vector<vector<Point> > hull(contours.size());
hull
size is0
, sincecontours
size is 0. So when you accesshull[i]
you are accessing out of bounds.Declare
hull
afterfindContours
: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(contours[i], hull[i]);
since 3rd argument
orientation
is false by default, and 4th argumentreturnPoints
is ignored when 2nd argument is astd::vector
.
thresh
value is ignored when you useTHRESH_OTSU
.
UPDATE
It seems that there are some problems with 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>());
这篇关于在凸形轮廓的Android NDK和OpenCV参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!