-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyPoint.cpp
More file actions
137 lines (119 loc) · 4.38 KB
/
Copy pathKeyPoint.cpp
File metadata and controls
137 lines (119 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <utility>
//
// Created by Szymon on 26.05.2019.
//
#include "KeyPoint.h"
KeyPoint::KeyPoint(double x, double y, vector<int> features) {
this->x = x;
this->y = y;
this->features = std::move(features);
this->featuresSize = this->features.size();
this->closest = nullptr;
}
int KeyPoint::featureDistance(KeyPoint &other) {
int distance = 0;
for(int i = 0; i < this->featuresSize; i++){
distance += abs(this->features[i] - other.features[i]);
}
return distance;
}
KeyPoint* KeyPoint::getClosest(vector<KeyPoint *> &keyPoints) {
if (closest == nullptr){
int minDistance = std::numeric_limits<int>::max();
KeyPoint* newClosest = nullptr;
for(int i = 0; i < keyPoints.size(); i++){
int distance = this->featureDistance(*keyPoints[i]);
if(distance < minDistance){
minDistance = distance;
newClosest = keyPoints[i];
}
}
closest = newClosest;
}
return closest;
}
KeyPoint* KeyPoint::fromString(string &text) {
vector<string> tokens = splitInputLine(text);
double x = std::stod(tokens[0]);
double y = std::stod(tokens[1]);
int nFeatures = tokens.size() - 5;
vector<int> features;
features.reserve(nFeatures);
for(int i = 5; i < tokens.size(); i++) {
features.push_back(std::stoi(tokens[i]));
}
return new KeyPoint(x, y, features);
}
std::vector<std::string> KeyPoint::splitInputLine(std::string &line) {
std::istringstream stringStream(line);
std::vector<std::string> lineTokens;
std::copy(std::istream_iterator<std::string>(stringStream),
std::istream_iterator<std::string>(),
std::back_inserter(lineTokens));
return lineTokens;
}
vector<KeyPoint *> KeyPoint::importKeyPoints(const string &fileName) {
string line;
std::ifstream file;
vector<KeyPoint*> keyPoints;
file.open(fileName, std::ios::in);
std::getline(file, line);
std::getline(file, line);
while(std::getline(file, line)){
if(!line.empty()){
keyPoints.push_back(fromString(line));
}
}
return keyPoints;
}
vector<pair<KeyPoint *, KeyPoint *>>
KeyPoint::getKeyPointPairs(vector<KeyPoint *> &firstKeyPoints, vector<KeyPoint *> &secondKeyPoints) {
vector<pair<KeyPoint*, KeyPoint*>> pairs;
for(int i = 0; i < firstKeyPoints.size(); i++){
KeyPoint* closestToFirst = firstKeyPoints[i]->getClosest(secondKeyPoints);
KeyPoint* closestToSecond = closestToFirst->getClosest(firstKeyPoints);
if(closestToSecond == firstKeyPoints[i]){
pairs.emplace_back(closestToSecond, closestToFirst);
}
}
return pairs;
}
double KeyPoint::euclideanDistance(KeyPoint& other) {
return euclideanDistance(other.x, other.y);
}
double KeyPoint::squaredEuclideanDistance(KeyPoint &other){
return squaredEuclideanDistance(other.x, other.y);
}
double KeyPoint::euclideanDistance(double otherX, double otherY) {
return sqrt(squaredEuclideanDistance(otherX, otherY));
}
double KeyPoint::squaredEuclideanDistance(double otherX, double otherY) {
double xdiff = this->x - otherX;
double ydiff = this->y - otherY;
return xdiff * xdiff + ydiff * ydiff;
}
void KeyPoint::calculateNeighbourhood(vector<KeyPoint *> &allImageKeyPoints, int neighbourhoodSize) {
this->neighbourhood.clear();
for (KeyPoint* otherKeyPoint: allImageKeyPoints) {
otherKeyPoint->len2 = this->squaredEuclideanDistance(*otherKeyPoint);
}
std::sort(allImageKeyPoints.begin(), allImageKeyPoints.end(), [](const KeyPoint* first, const KeyPoint* second) {
return first->len2 < second->len2;
});
this->neighbourhood = vector<KeyPoint*>();
this->neighbourhood.reserve(neighbourhoodSize);
for(int i = 0; neighbourhood.size() < neighbourhoodSize && i < allImageKeyPoints.size(); i++){
if(allImageKeyPoints[i]->len2 > 0){
this->neighbourhood.push_back(allImageKeyPoints[i]);
}
}
}
bool KeyPoint::neighbourhoodContains(KeyPoint *otherKeyPoint) {
return std::find(this->neighbourhood.begin(), this->neighbourhood.end(), otherKeyPoint) != this->neighbourhood.end();
}
double KeyPoint::getX() const {
return x;
}
double KeyPoint::getY() const {
return y;
}