[英]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.