-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWIR_clustering.cpp
178 lines (171 loc) · 4.86 KB
/
WIR_clustering.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
#include "WIR_clustering.h"
bool WIR_clustering::getCentroidsBRIEF(const cv::Mat& descriptors, cv::Mat& centroids, unsigned int countCentroids)
{
if (!getMostValuebleDescriptors(descriptors, centroids, countCentroids))
return false;
if ((unsigned int) centroids.rows <= countCentroids)
return true;
//compute clusters
Mat dist;
unsigned int** tmpArray = NULL;
tmpArray = new unsigned int*[countCentroids];
if(tmpArray == NULL)
return false;
unsigned int* selectCCount = NULL;
selectCCount = new unsigned int[countCentroids];
if(selectCCount == NULL)
{
delete[] tmpArray;
return false;
};
for (size_t i = 0; i<countCentroids; i++)
{
selectCCount[i] = 0;
tmpArray[i] = NULL;
tmpArray[i] = new unsigned int[8*BRIEF_DECTRIPTOR_SIZE];
if(tmpArray[i] == NULL)
{
for(size_t j = 0; j<i; j++)
delete[] tmpArray[j];
delete[] tmpArray;
delete[] selectCCount;
return false;
};
for(size_t j = 0; j<8*BRIEF_DECTRIPTOR_SIZE; j++)
tmpArray[i][j] = 0;
};
int iteration = maxIteration;
while(iteration > 0)
{
//clearnig tmpArray
for(size_t i = 0; i<countCentroids; i++)
{
for(size_t j = 0; j<8*BRIEF_DECTRIPTOR_SIZE; j++)
tmpArray[i][j] = 0;
selectCCount[i] = 0;
};
//calculating distance
cv::batchDistance(descriptors,centroids,dist,CV_32S,noArray(),NORM_HAMMING);
for(size_t i = 0; i<(unsigned int)descriptors.rows; i++)
{
unsigned int selectedComponent = 0;
for(size_t j = 0; j<countCentroids; j++)
if(dist.at<int>(i,j)<dist.at<int>(i,selectedComponent))
selectedComponent = j;
selectCCount[selectedComponent]++;
//passing bits
for(size_t j = 0; j<BRIEF_DECTRIPTOR_SIZE;j++)
{
for(int k = 0; k<8; k++)
if(getBit(descriptors.at<unsigned char>(i,j),k)>0)
tmpArray[selectedComponent][j*8+k]++;
};
};
//Normolizing
Mat tmpCentroids;
centroids.copyTo(tmpCentroids);
for(size_t i = 0; i<countCentroids; i++)
{
if(selectCCount[i]>0)
for(size_t j = 0; j<BRIEF_DECTRIPTOR_SIZE;j++)
{
tmpCentroids.at<unsigned char>(i,j) = 0;
for(int k = 0; k<8; k++)
if(tmpArray[i][j*8+k]/(double)selectCCount[i]>=0.5)
{
unsigned char tmpchar = tmpCentroids.at<unsigned char>(i,j);
tmpchar = tmpchar | (unsigned int)(1<<k);
tmpCentroids.at<unsigned char>(i,j) = tmpchar;
}
else
{
unsigned char tmpchar = tmpCentroids.at<unsigned char>(i,j);
tmpchar = tmpchar & (unsigned int)~(1<<k);
tmpCentroids.at<unsigned char>(i,j) = tmpchar;
};
};
};
cv::batchDistance(tmpCentroids,centroids,dist,CV_32S,noArray(),NORM_HAMMING);
tmpCentroids.copyTo(centroids);
//epsilon calculation
bool breakCondition = true;
for(size_t i = 0; i<countCentroids; i++)
{
breakCondition = (dist.at<int>(i,i)<epsilon) && breakCondition;
};
if(breakCondition)
break;
else
{
iteration--;
};
};
//
for(size_t j = 0; j<countCentroids; j++)
delete[] tmpArray[j];
delete[] tmpArray;
delete[] selectCCount;
return true;
};
bool WIR_clustering::getMostValuebleDescriptors(const cv::Mat& descriptors, cv::Mat& centroids, unsigned int countCentroids)
{
if(descriptors.cols!=BRIEF_DECTRIPTOR_SIZE || descriptors.type() != CV_8U || countCentroids == 0)
return false;
if ((unsigned int)descriptors.rows<=countCentroids)
{
descriptors.copyTo(centroids);
return true;
}
//initiating centroid matrix
centroids = Mat::zeros(countCentroids,BRIEF_DECTRIPTOR_SIZE, CV_8U);
//Getting centroids
//!!! REALLY IMPORTANT THING
Mat dist;
stack<DistRecord> distances;
distances.push(DistRecord());
cv::batchDistance(descriptors,descriptors,dist,CV_32S,noArray(),NORM_HAMMING);
DistRecord tmpRecord;
for (size_t i = 0; i<(unsigned int)dist.rows; i++)
for (size_t j = i; j<(unsigned int)dist.cols; j++)
{
tmpRecord = distances.top();
if(dist.at<int>(i,j)>=tmpRecord.distance)
distances.push(DistRecord(i,j,dist.at<int>(i,j)));
};
//Coping data
int tmpCount = countCentroids;
set<unsigned int> inserted_id;
while(tmpCount>0)
{
if(distances.empty())
{
int k = rand() % descriptors.rows;
if (inserted_id.count(k)==0)
{
inserted_id.insert(k);
for(size_t i = 0; i<BRIEF_DECTRIPTOR_SIZE;i++)
centroids.at<unsigned char>(tmpCount-1,i) = descriptors.at<unsigned char>(k,i); ///!!!
tmpCount--;
}
continue;
}
tmpRecord = distances.top();
if (inserted_id.count(tmpRecord.i)==0)
{
inserted_id.insert(tmpRecord.i);
for(size_t i = 0; i<BRIEF_DECTRIPTOR_SIZE;i++)
centroids.at<unsigned char>(tmpCount-1,i) = descriptors.at<unsigned char>(tmpRecord.i,i); ///!!!
tmpCount--;
}
if(tmpCount>0)
if (inserted_id.count(tmpRecord.j)==0)
{
inserted_id.insert(tmpRecord.j);
for(size_t i = 0; i<BRIEF_DECTRIPTOR_SIZE;i++)
centroids.at<unsigned char>(tmpCount-1,i) = descriptors.at<unsigned char>(tmpRecord.j,i); ///!!!
tmpCount--;
}
distances.pop();
};
return true;
}