/* Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Universite de Sherbrooke nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "rtabmap/core/util3d_correspondences.h" #include "rtabmap/core/util3d.h" #include #include #if CV_MAJOR_VERSION < 5 #include #else #include #endif #include #include namespace rtabmap { namespace util3d { void extractXYZCorrespondences(const std::multimap & words1, const std::multimap & words2, pcl::PointCloud & cloud1, pcl::PointCloud & cloud2) { const std::list & ids = uUniqueKeys(words1); for(std::list::const_iterator i=ids.begin(); i!=ids.end(); ++i) { if(words1.count(*i) == 1 && words2.count(*i) == 1) { const pcl::PointXYZ & pt1 = words1.find(*i)->second; const pcl::PointXYZ & pt2 = words2.find(*i)->second; if(pcl::isFinite(pt1) && pcl::isFinite(pt2)) { cloud1.push_back(pt1); cloud2.push_back(pt2); } } } } void extractXYZCorrespondencesRANSAC(const std::multimap & words1, const std::multimap & words2, pcl::PointCloud & cloud1, pcl::PointCloud & cloud2) { std::list > pairs; const std::list & ids = uUniqueKeys(words1); for(std::list::const_iterator i=ids.begin(); i!=ids.end(); ++i) { if(words1.count(*i) == 1 && words2.count(*i) == 1) { const pcl::PointXYZ & pt1 = words1.find(*i)->second; const pcl::PointXYZ & pt2 = words2.find(*i)->second; if(pcl::isFinite(pt1) && pcl::isFinite(pt2)) { pairs.push_back(std::pair(pt1, pt2)); } } } if(pairs.size() > 7) { // Remove outliers using fundamental matrix RANSAC std::vector status(pairs.size(), 0); //Convert Keypoints to a structure that OpenCV understands //3 dimensions (Homogeneous vectors) cv::Mat points1(1, (int)pairs.size(), CV_32FC2); cv::Mat points2(1, (int)pairs.size(), CV_32FC2); float * points1data = points1.ptr(0); float * points2data = points2.ptr(0); // Fill the points here ... int i=0; for(std::list >::const_iterator iter = pairs.begin(); iter != pairs.end(); ++iter ) { points1data[i*2] = (*iter).first.x; points1data[i*2+1] = (*iter).first.y; points2data[i*2] = (*iter).second.x; points2data[i*2+1] = (*iter).second.y; ++i; } // Find the fundamental matrix cv::Mat fundamentalMatrix = cv::findFundamentalMat( points1, points2, status, cv::FM_RANSAC, 3.0, 0.99); if(!fundamentalMatrix.empty()) { int i = 0; for(std::list >::iterator iter=pairs.begin(); iter!=pairs.end(); ++iter) { if(status[i]) { cloud1.push_back(iter->first); cloud2.push_back(iter->second); } ++i; } } } } void extractXYZCorrespondences(const std::list > & correspondences, const cv::Mat & depthImage1, const cv::Mat & depthImage2, float cx, float cy, float fx, float fy, float maxDepth, pcl::PointCloud & cloud1, pcl::PointCloud & cloud2) { cloud1.resize(correspondences.size()); cloud2.resize(correspondences.size()); int oi=0; for(std::list >::const_iterator iter = correspondences.begin(); iter!=correspondences.end(); ++iter) { pcl::PointXYZ pt1 = projectDepthTo3D(depthImage1, iter->first.x, iter->first.y, cx, cy, fx, fy, true); pcl::PointXYZ pt2 = projectDepthTo3D(depthImage2, iter->second.x, iter->second.y, cx, cy, fx, fy, true); if(pcl::isFinite(pt1) && pcl::isFinite(pt2) && (maxDepth <= 0 || (pt1.z <= maxDepth && pt2.z<=maxDepth))) { cloud1[oi] = pt1; cloud2[oi] = pt2; ++oi; } } cloud1.resize(oi); cloud2.resize(oi); } template inline void extractXYZCorrespondencesImpl(const std::list > & correspondences, const pcl::PointCloud & cloud1, const pcl::PointCloud & cloud2, pcl::PointCloud & inliers1, pcl::PointCloud & inliers2) { UASSERT(cloud1.empty() || cloud1.isOrganized()); UASSERT(cloud2.empty() || cloud2.isOrganized()); for(std::list >::const_iterator iter = correspondences.begin(); iter!=correspondences.end(); ++iter) { int u1 = int(iter->first.x+0.5f); int v1 = int(iter->first.y+0.5f); int u2 = int(iter->second.x+0.5f); int v2 = int(iter->second.y+0.5f); UASSERT(v1>=0 && v1 < (int)cloud1.height && u1>=0 && u1<(int)cloud1.width); UASSERT(v2>=0 && v2 < (int)cloud2.height && u2>=0 && u2<(int)cloud2.width); PointT pt1 = cloud1.at(v1 * cloud1.width + u1); PointT pt2 = cloud2.at(v2 * cloud2.width + u2); if(pcl::isFinite(pt1) && pcl::isFinite(pt2)) { inliers1.push_back(pcl::PointXYZ(pt1.x, pt1.y, pt1.z)); inliers2.push_back(pcl::PointXYZ(pt2.x, pt2.y, pt2.z)); } } } void extractXYZCorrespondences(const std::list > & correspondences, const pcl::PointCloud & cloud1, const pcl::PointCloud & cloud2, pcl::PointCloud & inliers1, pcl::PointCloud & inliers2) { extractXYZCorrespondencesImpl(correspondences, cloud1, cloud2, inliers1, inliers2); } void extractXYZCorrespondences(const std::list > & correspondences, const pcl::PointCloud & cloud1, const pcl::PointCloud & cloud2, pcl::PointCloud & inliers1, pcl::PointCloud & inliers2) { extractXYZCorrespondencesImpl(correspondences, cloud1, cloud2, inliers1, inliers2); } int countUniquePairs(const std::multimap & wordsA, const std::multimap & wordsB) { const std::list & ids = uUniqueKeys(wordsA); int pairs = 0; for(std::list::const_iterator i=ids.begin(); i!=ids.end(); ++i) { std::list ptsA = uValues(wordsA, *i); std::list ptsB = uValues(wordsB, *i); if(ptsA.size() == 1 && ptsB.size() == 1) { ++pairs; } } return pairs; } void filterMaxDepth(pcl::PointCloud & inliers1, pcl::PointCloud & inliers2, float maxDepth, char depthAxis, bool removeDuplicates) { std::list addedPts; if(maxDepth > 0.0f && inliers1.size() && inliers1.size() == inliers2.size()) { pcl::PointCloud tmpInliers1; pcl::PointCloud tmpInliers2; for(unsigned int i=0; i::iterator iter = addedPts.begin(); iter!=addedPts.end(); ++iter) { if(iter->x == inliers1.at(i).x && iter->y == inliers1.at(i).y && iter->z == inliers1.at(i).z) { dup = true; } } if(!dup) { addedPts.push_back(inliers1.at(i)); } } if(!dup) { tmpInliers1.push_back(inliers1.at(i)); tmpInliers2.push_back(inliers2.at(i)); } } } inliers1 = tmpInliers1; inliers2 = tmpInliers2; } } /** * if a=[1 2 3 4 6 6], b=[1 1 2 4 5 6 6], results= [(2,2) (4,4)] * realPairsCount = 5 */ void findCorrespondences( const std::multimap & wordsA, const std::multimap & wordsB, std::list > & pairs) { const std::list & ids = uUniqueKeys(wordsA); pairs.clear(); for(std::list::const_iterator i=ids.begin(); i!=ids.end(); ++i) { std::list ptsA = uValues(wordsA, *i); std::list ptsB = uValues(wordsB, *i); if(ptsA.size() == 1 && ptsB.size() == 1) { pairs.push_back(std::pair(ptsA.front().pt, ptsB.front().pt)); } } } void findCorrespondences( const std::multimap & words1, const std::multimap & words2, std::vector & inliers1, std::vector & inliers2, float maxDepth, std::vector * uniqueCorrespondences) { std::list ids = uUniqueKeys(words1); // Find pairs inliers1.resize(ids.size()); inliers2.resize(ids.size()); if(uniqueCorrespondences) { uniqueCorrespondences->resize(ids.size()); } int oi=0; for(std::list::iterator iter=ids.begin(); iter!=ids.end(); ++iter) { if(words1.count(*iter) == 1 && words2.count(*iter) == 1) { inliers1[oi] = words1.find(*iter)->second; inliers2[oi] = words2.find(*iter)->second; if(util3d::isFinite(inliers1[oi]) && util3d::isFinite(inliers2[oi]) && (inliers1[oi].x != 0 || inliers1[oi].y != 0 || inliers1[oi].z != 0) && (inliers2[oi].x != 0 || inliers2[oi].y != 0 || inliers2[oi].z != 0) && (maxDepth <= 0 || (inliers1[oi].x > 0 && inliers1[oi].x <= maxDepth && inliers2[oi].x>0 &&inliers2[oi].x<=maxDepth))) { if(uniqueCorrespondences) { uniqueCorrespondences->at(oi) = *iter; } ++oi; } } } inliers1.resize(oi); inliers2.resize(oi); if(uniqueCorrespondences) { uniqueCorrespondences->resize(oi); } } void findCorrespondences( const std::map & words1, const std::map & words2, std::vector & inliers1, std::vector & inliers2, float maxDepth, std::vector * correspondences) { std::vector ids = uKeys(words1); // Find pairs inliers1.resize(ids.size()); inliers2.resize(ids.size()); if(correspondences) { correspondences->resize(ids.size()); } int oi=0; for(std::vector::iterator iter=ids.begin(); iter!=ids.end(); ++iter) { if(words2.find(*iter) != words2.end()) { inliers1[oi] = words1.find(*iter)->second; inliers2[oi] = words2.find(*iter)->second; if(util3d::isFinite(inliers1[oi]) && util3d::isFinite(inliers2[oi]) && (inliers1[oi].x != 0 || inliers1[oi].y != 0 || inliers1[oi].z != 0) && (inliers2[oi].x != 0 || inliers2[oi].y != 0 || inliers2[oi].z != 0) && (maxDepth <= 0 || (inliers1[oi].x > 0 && inliers1[oi].x <= maxDepth && inliers2[oi].x>0 &&inliers2[oi].x<=maxDepth))) { if(correspondences) { correspondences->at(oi) = *iter; } ++oi; } } } inliers1.resize(oi); inliers2.resize(oi); if(correspondences) { correspondences->resize(oi); } } } }