-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMeansClassifier.cpp
202 lines (182 loc) · 8.07 KB
/
KMeansClassifier.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "kMeansClassifier.hpp"
template <class T>
cv::Mat matFromVecOfVec(const std::vector<std::vector<double>>& input, int matType){
if(input.size() == 0)
return cv::Mat(cv::Size(0,0), matType);
cv::Mat matVec = cv::Mat(input.size(), input.at(0).size(), matType);
for(int i=0; i<matVec.rows; ++i)
for(int j=0; j<matVec.cols; ++j)
matVec.at<T>(i, j) = (T)input.at(i).at(j);
return matVec;
}
void fillDoubleVectorWithMat(cv::Mat& data, std::vector<std::vector<double>>& out){
for(int i=0; i<data.rows; ++i){
std::vector<double> row;
for(int j=0; j<data.cols; ++j){
row.push_back((double)data.at<double>(i, j));
}
out.push_back(row);
}
}
double cluster(std::vector<std::vector<double>>& input, int k,cv::Mat& output){
cv::Mat in = matFromVecOfVec<float>(input, CV_32F);
cv::Mat labels;
std::cout<<"Input matrix size: "<<in.rows<<"x"<<in.cols<<std::endl;
output.convertTo(output, CV_32F);
double comp = cv::kmeans(in, k, labels, cv::TermCriteria(cv::TermCriteria::MAX_ITER, 20, 0), 5, cv::KmeansFlags::KMEANS_PP_CENTERS, output);
std::cout<<"Output matrix size: "<<output.rows<<"x"<<output.cols<<", channels: "<<output.channels()<<std::endl;
output.convertTo(output, CV_64F);
return comp;
}
double l2Dist(std::vector<double>& a, std::vector<double>& b){
std::vector<double> auxiliary;
std::transform (a.begin(), a.end(), b.begin(), std::back_inserter(auxiliary),//
[](double element1, double element2) {return pow((element1-element2),2);});
return std::sqrt(std::accumulate(auxiliary.begin(), auxiliary.end(), 0.0));
}
double closerInMat(cv::Mat& mat, std::vector<double>& descriptor){
cv::Mat descMat;
descMat.push_back( cv::Mat(descriptor, false).reshape(1,1));
double minDist = cv::norm(mat.row(0), descMat, cv::NORM_L2);
double secondDist = 1e15;
for(int i = 1; i < mat.rows; i++){
double dist = cv::norm(mat.row(i), descMat, cv::NORM_L2);
if(dist < minDist){
secondDist = minDist;
minDist = dist;
}
}
const double ratio = 2.;
if(minDist < ratio * secondDist)
return minDist;
else
return 1e15;
return minDist;
}
double closerInVector(std::vector<std::vector<double>>& vector, std::vector<double>& descriptor){
double minDist = l2Dist(vector[0], descriptor);
for(int i = 1; i < vector.size(); i++){
double dist = l2Dist(vector[i], descriptor);
if(dist < minDist) {
minDist = dist;
}
}
return minDist;
}
void KMeansClassifier::clusterSeaKps(std::vector<std::vector<double>>& seaKps, int k, bool printComp = false){
double comp = cluster(seaKps, k, seaCMat);
if(printComp)
std::cout<<"Compactness for sea kps: "<<comp<<std::endl;
}
void KMeansClassifier::clusterboatsKps(std::vector<std::vector<double>>& boatsKps, int k, bool printComp = false){
double comp = cluster(boatsKps, k, boatsCMat);
if(printComp)
std::cout<<"Compactness for boat kps: "<<comp<<std::endl;
}
void KMeansClassifier::clusterbgKps(std::vector<std::vector<double>>& bgKps, int k, bool printComp = false){
double comp = cluster(bgKps, k, bgCMat);
if(printComp)
std::cout<<"Compactness for bg kps: "<<comp<<std::endl;
}
double findBestMatch(cv::Mat& centroids, std::vector<double> descriptor){
std::vector<std::vector<double>> temp;
temp.push_back(descriptor);
cv::Mat descrMat = matFromVecOfVec<float>(temp, CV_32F);
//cv::Mat cvtCentroids;
//centroids.convertTo(cvtCentroids, CV_32F);
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::FLANNBASED);
std::vector< std::vector<cv::DMatch> > knn_matches;
matcher->knnMatch(descrMat, centroids, knn_matches, 2 );
//std::cout<<"size "<<knn_matches.size()<<std::endl;
//-- Filter matches using the Lowe's ratio test
const float ratio_thresh = 2.f;
std::vector<cv::DMatch> good_matches;
for (size_t i = 0; i < knn_matches.size(); i++)
{
if (knn_matches[i][0].distance < ratio_thresh * knn_matches[i][1].distance)
{
good_matches.push_back(knn_matches[i][0]);
}
}
if(good_matches.size() > 0){
return (double)good_matches[0].distance;
} else {
return 1e16;
}
}
int KMeansClassifier::predictLabel(std::vector<double>& descriptor){
double minSeaDist, minBoatDist,minBgDist;
minSeaDist = closerInMat(seaCMat, descriptor);
minBoatDist = closerInMat(boatsCMat, descriptor);
minBgDist = closerInMat(bgCMat, descriptor);
double bestDist = minSeaDist, secondBestDist = minSeaDist;
unsigned int bestLabel = SEA_TARGET;
if(minBoatDist < minBgDist){
if(minBoatDist < bestDist) {
bestDist = minBoatDist;
bestLabel = BOAT_TARGET;
if(minBgDist < secondBestDist)
secondBestDist = minBgDist;
} else {
secondBestDist = minBoatDist;
}
} else {
if(minBgDist < bestDist) {
bestDist = minBgDist;
bestLabel = BG_TARGET;
if(minBoatDist < secondBestDist)
secondBestDist = minBoatDist;
} else {
secondBestDist = minBgDist;
}
}
if(bestDist < decisionRatio * secondBestDist)
return bestLabel;
else
return 0;
}
std::vector<int> KMeansClassifier::predictBoatsBatch(cv::Mat& descriptors, float threshold){
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::FLANNBASED);
std::vector< std::vector<cv::DMatch> > knn_matches;
matcher->knnMatch( descriptors, boatsCMat32, knn_matches, 2 );
const float ratio_thresh = decisionRatio;
std::vector<int> labels;
for (size_t i = 0; i < knn_matches.size(); i++)
{
if (knn_matches[i][0].distance < ratio_thresh * knn_matches[i][1].distance)
if(knn_matches[i][0].distance < threshold)
{ labels.push_back(BOAT_TARGET);}
else
labels.push_back(SEA_TARGET);
else
labels.push_back(SEA_TARGET);
}
return labels;
}
void KMeansClassifier::save(cv::String& inputDirectory){
std::vector<std::vector<double>> seaCentroids, boatsCentroids, bgCentroids;
fillDoubleVectorWithMat(seaCMat, seaCentroids);
fillDoubleVectorWithMat(bgCMat, bgCentroids);
fillDoubleVectorWithMat(boatsCMat, boatsCentroids);
saveDataset(inputDirectory + "/kmclassifier/seaCentroids.txt", seaCentroids);
saveDataset(inputDirectory + "/kmclassifier/boatsCentroids.txt", boatsCentroids);
saveDataset(inputDirectory + "/kmclassifier/bgCentroids.txt", bgCentroids);
}
void KMeansClassifier::load(cv::String& inputDirectory, bool bg){
std::vector<std::vector<double>> seaCentroids, boatsCentroids, bgCentroids, vInputs, tInputs;
std::vector<uint> vOutputs, tOutputs;
loadDataset(inputDirectory + "/kmclassifier/seaCentroids.txt", seaCentroids, vOutputs, vInputs, vOutputs, tInputs, tOutputs, 10000000,0,0, false);
loadDataset(inputDirectory + "/kmclassifier/boatsCentroids.txt", boatsCentroids, vOutputs, vInputs, vOutputs, tInputs, tOutputs, 100000000,0,0, false);
if(bg){
loadDataset(inputDirectory + "/kmclassifier/bgCentroids.txt", bgCentroids, vOutputs, vInputs, vOutputs, tInputs, tOutputs, 10000000,0,0, false);
}/*loadDataset(inputDirectory + "_sea_kp_dataset.txt", seaCentroids, vOutputs, vInputs, vOutputs, tInputs, tOutputs, 1000000,0,0, true);
loadDataset(inputDirectory + "_boats_kp_dataset.txt", boatsCentroids, vOutputs, vInputs, vOutputs, tInputs, tOutputs, 1000000,0,0, true);
loadDataset(inputDirectory + "_bg_kp_dataset.txt", bgCentroids, vOutputs, vInputs, vOutputs, tInputs, tOutputs, 1000000,0,0, true);
*/
//std::cout<<"Start mat creation"<<std::endl;
bgCMat = matFromVecOfVec<double>(bgCentroids, CV_64F);
seaCMat = matFromVecOfVec<double>(seaCentroids, CV_64F);
boatsCMat = matFromVecOfVec<double>(boatsCentroids, CV_64F);
boatsCMat32 = matFromVecOfVec<float>(boatsCentroids, CV_32F);
//std::cout<<"End mat creation"<<std::endl;
}