我正在使用Opencv开发用于图像处理的Android应用程序。
处理部分是通过JNI函数在Java中调用的。
我正在向c ++发送两个Opencv Mat
对象以找到匹配的点。
问题在于,有时该应用程序可以运行,有时甚至在使用相同图像时也无法运行。
在将图像发送到本机部分之前,我已经检查了图像是否为空并且有效。
这是在Log中获取堆栈跟踪
2019-04-26 12:52:06.980 24955-25666/com.grimg.coffretpfe A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 25666 (Thread-10)
2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: Build fingerprint: 'samsung/zeroltexx/zerolte:7.0/NRD90M/G925FXXU6ERF5:user/release-keys'
2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: Revision: '10'
2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: ABI: 'arm'
2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: pid: 24955, tid: 25666, name: Thread-10 >>> com.grimg.coffretpfe <<<
2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
2019-04-26 12:52:07.078 25766-25766/? A/DEBUG: r0 00000000 r1 00000000 r2 00000000 r3 b90a641c
2019-04-26 12:52:07.079 25766-25766/? A/DEBUG: r4 ffffffff r5 c09a4128 r6 b90a678c r7 b90a6670
2019-04-26 12:52:07.079 25766-25766/? A/DEBUG: r8 d33ee570 r9 97df7155 sl b90a6790 fp c06e3900
2019-04-26 12:52:07.079 25766-25766/? A/DEBUG: ip c5ec4840 sp b90a6268 lr c5eb8043 pc c5eb8042 cpsr 600f0030
2019-04-26 12:52:07.081 25766-25766/? A/DEBUG: backtrace:
2019-04-26 12:52:07.081 25766-25766/? A/DEBUG: #00 pc 00014042 /data/app/com.grimg.coffretpfe-2/lib/arm/libnative-lib.so (_Z6toGrayN2cv3MatES0_+1577)
2019-04-26 12:52:07.081 25766-25766/? A/DEBUG: #01 pc 00014d65 /data/app/com.grimg.coffretpfe-2/lib/arm/libnative-lib.so (Java_com_grimg_coffretpfe_Activities_CompareActivity_detectFeatures+124)
2019-04-26 12:52:07.081 25766-25766/? A/DEBUG: #02 pc 000adc99 /system/lib/libart.so (art_quick_generic_jni_trampoline+40)
2019-04-26 12:52:07.081 25766-25766/? A/DEBUG: #03 pc 0000b0dd /dev/ashmem/dalvik-jit-code-cache_24955_24955 (deleted)
如果这很重要,我将在Samsung s6 edge上运行我的应用程序。
我的本机部分包含两个函数toGray
,这里是签名
double toGray(Mat captured, Mat target)
我没有添加代码,因为它需要大量的处理和操作
我在Java中调用的方法是detectFeatures
extern "C" {
JNIEXPORT
jdouble
JNICALL Java_com_grimg_coffretpfe_Activities_CompareActivity_detectFeatures(
JNIEnv *env,
jobject instance,
jlong addrRgba,
jlong addrGray) {
if (addrGray != NULL && addrRgba != NULL) {
cv::Mat &mRgb = *(cv::Mat *) addrRgba;
cv::Mat &mGray = *(cv::Mat *) addrGray;
jdouble retVal;
double conv = toGray(mRgb, mGray);
retVal = (jdouble) conv;
return retVal;
}
return -1;
}
}
我在这里尝试了所有可用的解决方案,但没有任何效果,除了我对问题并不十分了解之外,所以我不知道要搜索什么。
我希望你们能帮助我解决这个问题。谢谢。
编辑
double toGray(Mat captured, Mat target) {
std::vector<cv::KeyPoint> keypointsCaptured;
std::vector<cv::KeyPoint> keypointsTarget;
cv::Mat descriptorsCaptured;
cv::Mat descriptorsTarget;
//cv::Mat captured;
std::vector<cv::DMatch> matches;
std::vector<cv::DMatch> symMatches;
//std::vector<std::vector<cv::DMatch> > matches1;
//std::vector<std::vector<cv::DMatch> > matches2;
//Mat captured, target;
/*captured = imread("/storage/emulated/0/data/img1.jpg", IMREAD_GRAYSCALE);
target = imread("/storage/emulated/0/data/img3.jpg", IMREAD_GRAYSCALE);
if (!captured.data) {
// Print error message and quit
__android_log_print(ANDROID_LOG_INFO, "sometag", "I cant do this.");
}
if (!target.data) {
// Print error message and quit
__android_log_print(ANDROID_LOG_INFO, "sometag", "cant do nuthin.");
}*/
//cvtColor(capturedR, captured, CV_RGBA2GRAY);
//cvtColor(targetR, target, CV_RGBA2GRAY);
orb = ORB::create();
//Pre-process
resize(captured, captured, Size(480, 360));
medianBlur(captured, captured, 5);
resize(target, target, Size(480, 360));
medianBlur(target, target, 5);
orb->detectAndCompute(captured, noArray(), keypointsCaptured, descriptorsCaptured);
orb->detectAndCompute(target, noArray(), keypointsTarget, descriptorsTarget);
//__android_log_print(ANDROID_LOG_INFO, "sometag", "keypoints2 size = %d", keypointsTarget.size());
//__android_log_print(ANDROID_LOG_INFO, "sometag", "keypoints size = %d", keypointsCaptured.size());
//Match images based on k nearest neighbour
std::vector<std::vector<cv::DMatch> > matches1;
matcher.knnMatch(descriptorsCaptured, descriptorsTarget,
matches1, 2);
//__android_log_print(ANDROID_LOG_INFO, "sometag", "Matches1 = %d", matches1.size());
std::vector<std::vector<cv::DMatch> > matches2;
matcher.knnMatch(descriptorsTarget, descriptorsCaptured,
matches2, 2);
//Ratio filter
ratioTest(matches1);
ratioTest(matches2);
symmetryTest(matches1, matches2, symMatches);
ransacTest(symMatches,
keypointsCaptured, keypointsTarget, matches);
const int symMatchCount = matches.size();
Point2f point1;
Point2f point2;
float median;
float meanBoy = 0;
float greatest = 0;
float lowest = 0;
int count = 0;
vector<float> angleList;
vector<Point2f> point1List;
vector<Point2f> point2List;
for (int i = 0; i < matches.size(); i++) {
point1 = keypointsCaptured[matches[i].queryIdx].pt;
point2 = keypointsTarget[matches[i].trainIdx].pt;
point1List.push_back(point1);
point2List.push_back(point2);
deltaY = ((360 - point2.y) - (360 - point1.y));
deltaX = (point2.x + 480 - point1.x);
angle = atan2(deltaY, deltaX) * 180 / PI;
cout << "ORB Matching Results" << angle << endl;
//if (angle > greatest) greatest = angle;
//if (angle < lowest) lowest = angle;
meanBoy += angle;
angleList.push_back(angle);
//std::cout << "points " << "(" << point1.x << "," <<360-point1.y<<") (" << point2.x << ","<<360-point2.y<<") angle:" <<angle << std::endl;
//std::cout << angle << std::endl;
}
// do something with the best points...
//std::cout << "Mean" << meanBoy/symMatchCount << std::endl;
vector<float> angleLCopy(angleList);
std::sort(angleLCopy.begin(), angleLCopy.end());
/* if(angleList.size() % 2 == 0)
median = (angleList[angleList.size()/2 - 1] + angleList[angleList.size()/2]) / 2;
else
median = angleList[angleList.size()/2];
*/
size_t medianIndex = angleLCopy.size() / 2;
nth_element(angleLCopy.begin(), angleLCopy.begin() + medianIndex, angleLCopy.end());
median = angleLCopy[medianIndex];
std::cout << "new Median method " << angleLCopy[medianIndex] << std::endl;
//std::cout << "greatest " << greatest << "|| lowest "<< lowest << std::endl;
//std::cout << "No of matches by shehel: " << angleList[35] << " size " << symMatchCount << std::endl;
//std::cout << "Median" << median << std::endl;
//std::cout << matches.size()<< std::endl;
count = 0;
for (auto i = matches.begin(); i != matches.end();) {
//std::cout << angleList.at(count)<< std::endl;
//if (angle > greatest) greatest = angle;
//if (angle < lowest) lowest = angle;
point1 = point1List.at(count);
point2 = point2List.at(count);
deltaY = ((360 - point2.y) - (360 - point1.y));
deltaX = ((point2.x + 480) - point1.x);
angle = atan2(deltaY, deltaX) * 180 / PI;
//angleList.push_back (angle);
cout << "Is it sorted? " << angleList.at(count) << endl;
if (angleList.at(count) > (median + 5) | angleList.at(count) < (median - 5)) {
//cout << "bitch is gone" << angleList.at(count) << endl;
matches.erase(i);
count++;
}
//{i++; count++;}
else {
cout << "Points A (" << point1.x << ", " << point1.y << ") B (" <<
point2.x + 480 << ", " << point2.y << ") Deltas of X" << deltaX << " Y " <<
deltaY << " Angle " << angle << endl;
cout << "aint going no where" << angleList.at(count) << endl;
++i;
count++;
//if (angle>0.5 | angle < -0.7)
//matches.erase(matches.begin()+i);
// do something with the best points...
}
}
return (static_cast<double>(matches.size()) / static_cast<double>(matches1.size()));
}