我调用了一个本地方法OnCameraFrame
来检查当前帧的关键点并尝试找到与模板图像的关键点的良好匹配,计算哪个索引图像具有最佳匹配并返回索引。(这是一个物体识别应用程序)。问题是它的工作速度非常慢(3-4 fps,完成此方法需要 0.3 秒),并且在 10-20 帧后,它会因SIGSEGV code=1
错误而崩溃。我意识到这可能是内存不足问题,但无法确定代码中的哪一部分会占用所有 RAM。希望你能帮忙。
JNIEXPORT jint JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_processImage(
JNIEnv* env, jlong frameAddress) {
Mat& image = *(Mat*) frameAddress;
cv::OrbFeatureDetector detector(100);
cv::OrbDescriptorExtractor extractor;
std::vector<cv::KeyPoint> queryKeypoints;
detector.detect(image, queryKeypoints);
Mat queryDescriptors;
extractor.compute(image, queryKeypoints, queryDescriptors);
queryDescriptors.convertTo(queryDescriptors, CV_32F);
vector < DMatch > matches;
flannMatcher.match(queryDescriptors, matches);
double max_dist = 0;
double min_dist = 100;
std::vector < DMatch > good_matches;
for (int i = 0; i < matches.size(); i++) {
if (matches[i].distance <= max(2 * min_dist, 0.02)) {
good_matches.push_back(matches[i]);
}
}
int * gmatchIndexes;
gmatchIndexes = new int[good_matches.size()];
for (int i = 0; i < good_matches.size(); i++) {
gmatchIndexes[i] = -1;
}
for (int kk = 0; kk < good_matches.size(); kk++) {
gmatchIndexes[good_matches[kk].imgIdx]++;
}
int maxIdx = -1;
for (int i = 0; i < good_matches.size(); i++) {
if (gmatchIndexes[i] > maxIdx) {
maxIdx = i;
}
}
int* p_answer = &maxIdx;
int answer = *p_answer;
//if (gmatchIndexes[maxIdx] > 2) {
image.release();
vector<DMatch>().swap(matches);
vector<DMatch>().swap(good_matches);
delete[] gmatchIndexes;
queryDescriptors.release();
return answer;
}
编辑:在我的代码中添加了发布/删除,但现在我得到了像 @@@ ABORTING: INVALID HEAP ADDRESS IN dlfree
和:0: gralloc_module_lock: Cannot lock buffer ID=55438 before register (0x0)