- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我对问题的标题感到抱歉,我遇到了一个问题,我找不到有关我得到的日志错误的任何信息。
我正在开发一个使用 Opencv 进行图像处理和匹配的 Android 应用程序。
主要的Opencv代码是使用c++完成的,并使用JNI函数导出到java。
应用过程是这样的,首先我拍摄相机图像,然后打开Opencv相机并开始将每一帧与引用图像进行匹配。
在每一帧上,我调用一个名为 detectFeatures
的 native 方法,该方法返回一个 double 值,表示两个图像匹配的百分比。
有时应用程序可以工作,有时会崩溃,我在日志中收到此错误
/data/app/com.grimg.coffretpfe-2/lib/arm/libnative-lib.so (_Z6toGrayN2cv3MatES0_+1577)
/data/app/com.grimg.coffretpfe-2/lib/arm/libnative-lib.so (Java_com_grimg_coffretpfe_Activities_CompareActivity_detectFeatures+100)
在 C++ 代码中,我有一个名为 toGray
的函数,这是它的签名
double toGray(Mat captured, Mat target)
而我在java中调用的jni方法是
extern "C"
jdouble
JNICALL Java_com_grimg_coffretpfe_Activities_CompareActivity_detectFeatures(
JNIEnv *env,
jclass type, jlong addrRgba, jlong addrGray /* this */) {
Mat &mRgb = *(Mat *) addrRgba;
Mat &mGray = *(Mat *) addrGray;
jdouble retVal;
double conv = toGray(mRgb, mGray);
retVal = (jdouble) conv;
return retVal;
}
我搜索了很多有关此错误的信息,但找不到任何相关内容。
也许你们可以帮我解决这个问题。
编辑:
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()));
}
编辑2:
cv::Mat ransacTest(
const std::vector<cv::DMatch>& matches,
const std::vector<cv::KeyPoint>& keypoints1,
const std::vector<cv::KeyPoint>& keypoints2,
std::vector<cv::DMatch>& outMatches) {
// Convert keypoints into Point2f
std::vector<cv::Point2f> points1, points2;
cv::Mat fundemental;
for (std::vector<cv::DMatch>::
const_iterator it= matches.begin();
it!= matches.end(); ++it) {
// Get the position of left keypoints
float x= keypoints1[it->queryIdx].pt.x;
float y= keypoints1[it->queryIdx].pt.y;
points1.push_back(cv::Point2f(x,y));
// Get the position of right keypoints
x= keypoints2[it->trainIdx].pt.x;
y= keypoints2[it->trainIdx].pt.y;
points2.push_back(cv::Point2f(x,y));
}
// Compute F matrix using RANSAC
std::vector<uchar> inliers(points1.size(),0);
if (points1.size()>0&&points2.size()>0){
cv::Mat fundemental= cv::findFundamentalMat(
cv::Mat(points1),cv::Mat(points2), // matching points
inliers, // match status (inlier or outlier)
CV_FM_RANSAC, // RANSAC method
distance, // distance to epipolar line
confidence); // confidence probability
// extract the surviving (inliers) matches
std::vector<uchar>::const_iterator
itIn= inliers.begin();
std::vector<cv::DMatch>::const_iterator
itM= matches.begin();
// for all matches
for ( ;itIn!= inliers.end(); ++itIn, ++itM) {
if (*itIn) { // it is a valid match
outMatches.push_back(*itM);
}
}
if (refineF) {
// The F matrix will be recomputed with
// all accepted matches
// Convert keypoints into Point2f
// for final F computation
points1.clear();
points2.clear();
for (std::vector<cv::DMatch>::
const_iterator it= outMatches.begin();
it!= outMatches.end(); ++it) {
// Get the position of left keypoints
float x= keypoints1[it->queryIdx].pt.x;
float y= keypoints1[it->queryIdx].pt.y;
points1.push_back(cv::Point2f(x,y));
// Get the position of right keypoints
x= keypoints2[it->trainIdx].pt.x;
y= keypoints2[it->trainIdx].pt.y;
points2.push_back(cv::Point2f(x,y));
}
// Compute 8-point F from all accepted matches
if (points1.size()>0&&points2.size()>0){
fundemental= cv::findFundamentalMat(
cv::Mat(points1),cv::Mat(points2), // matches
CV_FM_8POINT); // 8-point method
}
}
}
return fundemental;
}
最佳答案
当您循环遍历 toGray
函数内的匹配项时,我发现可能会发生崩溃:
matches.erase(i);
这将使迭代器i
无效。您应该将其替换为:
i = matches.erase(i);
关于java - 如何调查随机的 Android native 函数调用错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847466/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!