slam14book2的ch7中3
使用了vins-mono和orbslam2的思想去实现了两个特征点均匀化的代码
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
struct node{
int left;
int right;
int up;
int down;
vector<int> featureIndex;
};
void divisionNode(node &father, std::vector<KeyPoint>& keypoints, int targetNum, vector<KeyPoint>& rekeypoints){
queue<node *> nodeQue;
nodeQue.push(&father);
int sizet = 0;
cout << keypoints.size() << endl;
while(1){
node* nowmakenode = nodeQue.front();
nodeQue.pop();
node* leftup = new node();
node* rightup = new node();
node* leftdown = new node();
node* rightdown = new node();
int h_middle = (nowmakenode->up + nowmakenode->down)/2;
int l_middle = (nowmakenode->left + nowmakenode->right)/2;
leftup->left = nowmakenode->left;
leftup->up = nowmakenode->up;
leftup->right = l_middle;
leftup->down = h_middle;
rightup->left = l_middle;
rightup->up = nowmakenode->up;
rightup->right = nowmakenode->right;
rightup->down = h_middle;
leftdown->left = nowmakenode->left;
leftdown->up = h_middle;
leftdown->right = l_middle;
leftdown->down = nowmakenode->down;
rightdown->left = l_middle;
rightdown->up = h_middle;
rightdown->right = nowmakenode->right;
rightdown->down = nowmakenode->down;
for(int i = 0; i < nowmakenode->featureIndex.size(); i++){
Point2f nowpoint = keypoints[nowmakenode->featureIndex[i]].pt;
if(nowpoint.y < l_middle){
if(nowpoint.x < h_middle){
leftup->featureIndex.push_back(nowmakenode->featureIndex[i]);
}else{
leftdown->featureIndex.push_back(nowmakenode->featureIndex[i]);
}
}else{
if(nowpoint.x < h_middle){
rightup->featureIndex.push_back(nowmakenode->featureIndex[i]);
}else{
rightdown->featureIndex.push_back(nowmakenode->featureIndex[i]);
}
}
}
if(leftup->featureIndex.size() == 0) delete leftup;
else if(leftup->featureIndex.size() == 1){
sizet++;
rekeypoints.push_back(keypoints[leftup->featureIndex[0]]);
delete leftup;
}else{
nodeQue.push(leftup);
}
if(leftdown->featureIndex.size() == 0) delete leftdown;
else if(leftdown->featureIndex.size() == 1){
sizet++;
rekeypoints.push_back(keypoints[leftdown->featureIndex[0]]);
delete leftdown;
}else{
nodeQue.push(leftdown);
}
if(rightup->featureIndex.size() == 0) delete rightup;
else if(rightup->featureIndex.size() == 1){
sizet++;
rekeypoints.push_back(keypoints[rightup->featureIndex[0]]);
delete rightup;
}else{
nodeQue.push(rightup);
}
if(rightdown->featureIndex.size() == 0) delete rightdown;
else if(rightdown->featureIndex.size() == 1){
sizet++;
rekeypoints.push_back(keypoints[rightdown->featureIndex[0]]);
delete rightdown;
}else{
nodeQue.push(rightdown);
}
cout << "nodeQue:" << nodeQue.size() << endl;
if(sizet + nodeQue.size() >= targetNum ){
while(nodeQue.size()>0){
node* makenode = nodeQue.front();
nodeQue.pop();
rekeypoints.push_back(keypoints[makenode->featureIndex[0]]);
delete makenode;
}
break;
}
if(nodeQue.size() == 0){
break;
}
}
}
int main(char argc, char **argv){
Mat img= imread("1.png");
if (!img.data)
{
cout << "error reading images " << endl;
return -1;
}
Mat gry;
cvtColor(img, gry, COLOR_BGR2GRAY);
std::vector<KeyPoint> keypoints;
Mat descriptors;
Ptr<FeatureDetector> detector = ORB::create();
Ptr<DescriptorExtractor> descriptor = ORB::create();
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create ( "BruteForce-Hamming" );
detector->detect (gry,keypoints );
Mat showImage1;
drawKeypoints( img, keypoints, showImage1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
imshow("picture",showImage1);
vector<KeyPoint> resiveID;
Mat mask = cv::Mat(img.rows, img.cols, CV_8UC1, cv::Scalar(255));
int MIN_DIST = 10;
for(int i = 0; i < keypoints.size(); i++){
Point2f pointLocation = keypoints[i].pt;
if (mask.at<uchar>(pointLocation) == 255){
resiveID.push_back(keypoints[i]);
cv::circle(mask, pointLocation, MIN_DIST, 0, -1);
}
}
Mat showImage2;
drawKeypoints( img, resiveID, showImage2, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
imshow("picture2",showImage2);
vector<KeyPoint> resivepoint2;
node father;
father.left = 0;
father.right = img.cols;
father.up = 0;
father.down = img.rows;
for(int i = 0; i < keypoints.size(); i++)
father.featureIndex.push_back(i);
divisionNode(father, keypoints, 30, resivepoint2);
cout << "orb:" << resivepoint2.size() << endl;
Mat showImage3;
drawKeypoints( img, resivepoint2, showImage3, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
imshow("picture3",showImage3);
waitKey(0);
}
实验效果记录:
原图
vins-mono思想
orb-slam2思想
|