我有以下for循环:
for (size_t matchIdx = 0; matchIdx < matcherTypes.size(); matchIdx++)
{
std::vector< DMatchVector > matches12;
std::vector< DMatchVector > matches21;
try
{
poi.knnMatch(descriptorsInterior, descriptorsCar, cfg.getPOIMatchKNN(),
matcherTypes[matchIdx], matches21);
poi.knnMatch(descriptorsCar, descriptorsInterior, cfg.getPOIMatchKNN(),
matcherTypes[matchIdx], matches12);
}
catch (MyPOIException& mpoie)
{
std::cerr << mpoie.what() << std::endl;
}
DMatchVector matches;
filterDMatch(matches12, matches21, matches);
ElectPOI elPOI;
std::vector< size_t > cntrVec(matches.size(), 0);
elPOI.counterPOIs(matches, cntrVec);
KeyPointVector filteredKPs;
elPOI.thresholdKeyPoints(1, kpInteriorVec, cntrVec, filteredKPs);
std::cout << "keypoints found: " << kpInteriorVec.size() << std::endl
<< "keypoints matched: " << matches.size() << std::endl;
cv::Mat tmpMat;
cv::drawKeypoints(interiorImage, filteredKPs, tmpMat);
cv::imshow("filtered keypoints", tmpMat);
cv::waitKey();
} // for each matcher type
我没有指针,除了Ptr
knnMatch(...)
方法中的OpenCV poi
(在for循环之外定义):
void POI::knnMatch(const cv::Mat& queryDescriptorsIn, const cv::Mat& trainDescriptorsIn,
int knnIn, const std::string& matcherTypeIn,
std::vector< DMatchVector >& matchesOut)
{
std::vector<DMatchVector> result;
cv::Ptr< cv::DescriptorMatcher > descriptorMatcher
= cv::DescriptorMatcher::create(matcherTypeIn);
if (descriptorMatcher.empty())
{
std::string msg = "Matcher of type " + matcherTypeIn + " was not created!";
throw MyPOIException(msg);
}
descriptorMatcher->knnMatch(queryDescriptorsIn, trainDescriptorsIn, result, knnIn);
matchesOut = result;
}
我不明白为什么它会因以下错误而崩溃:
*** Error in `/home/me/projects/prj/build/prj': free(): invalid pointer: 0x000000000110dda2 ***
实际上,在显示图像之前,它会在循环的每个第二步崩溃。任何sugestions?我做错了什么?
修改
Valgrind说:
==10329== Invalid read of size 8
==10329== at 0x605479: ElectPOI::counterPOIs(std::vector<cv::DMatch, std::allocator<cv::DMatch> > const&, std::vector<unsigned long, std::allocator<unsigned long> >&) (CElectPOI.cpp:12)
==10329== by 0x5D8A87: main (main.cpp:199)
==10329== Address 0xc74ec60 is not stack'd, malloc'd or (recently) free'd
valgrind: m_mallocfree.c:303 (get_bszB_as_is): Assertion 'bszB_lo == bszB_hi' failed.
valgrind: Heap block lo/hi size mismatch: lo = 449, hi = 450.
This is probably caused by your program erroneously writing past the
end of a heap block and corrupting heap metadata. If you fix any
invalid writes reported by Memcheck, this assertion failure will
probably go away. Please try that before reporting this as a bug.
功能如下:
void ElectPOI::counterPOIs(const DMatchVector& matchesIn, std::vector< size_t >& cntrVecInOut)
{
for (size_t i = 0; i < matchesIn.size(); i++)
{
int qryIdx = matchesIn[i].queryIdx;
cntrVecInOut[qryIdx]++; // line 12
}
}