-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFostroDescriptor.cpp
More file actions
219 lines (179 loc) · 5.35 KB
/
FostroDescriptor.cpp
File metadata and controls
219 lines (179 loc) · 5.35 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "FostroImage.h"
#include "FostroPixel.h"
#include "FostroErosionFilter.h"
#include "FostroDescriptor.h"
FostroDescriptor::FostroDescriptor() {
}
FostroDescriptor::FostroDescriptor(const FostroDescriptor& other) {
}
FostroDescriptor& FostroDescriptor::operator=(const FostroDescriptor& other) {
return *this;
}
FostroDescriptor::~FostroDescriptor() {
}
void FostroDescriptor::calcDescriptors(FostroImage* image, int c){
area = calcArea(image);
perimeter = calcPerimeter(image);
calcPerimeter2OverArea(image);
calcMomentInvariants(image);
}
float FostroDescriptor::calcArea(FostroImage* image){
float area = 0;
FostroPixel* pixel;
for (unsigned long x = 0; x < image->getHeight(); ++x) {
for (unsigned long y = 0; y < image->getWidth(); ++y) {
pixel = image->getPixel(x,y);
if (pixel->getGrayscaleIntensity() > MAX_PIXEL_VAL/2) {
++area;
}
}
}
return area;
}
float FostroDescriptor::calcPerimeter(FostroImage* image){
FostroErosionFilter erosionFilter(3,3);
FostroImage* tmpImage = erosionFilter.applyFilter(image, GRAY);
tmpImage->setFilePath("erosionPerim.png");
tmpImage->saveImage();
float erodedArea = calcArea(tmpImage);
return (area - erodedArea);
}
void FostroDescriptor::calcPerimeter2OverArea(FostroImage* image){
perim2OverArea = (perimeter*perimeter)/area;
}
void FostroDescriptor::calcMomentInvariants(FostroImage* image) {
float m10 = 0;
float m01 = 0;
float m00 = 0;
float tmp;
FostroPixel* pixel;
for (unsigned long x = 0; x < image->getHeight(); ++x) {
for (unsigned long y = 0; y < image->getWidth(); ++y) {
pixel = image->getPixel(x,y);
tmp = pixel->getGrayscaleIntensity();
if (tmp > MAX_PIXEL_VAL/2) {
m00 += tmp;
m01 += y*tmp;
m10 += x*tmp;
}
}
}
float xcenter = m10/m00;
float ycenter = m01/m00;
float mu00 = m00;
float mu02 = 0;
float mu03 = 0;
float mu11 = 0;
float mu12 = 0;
float mu20 = 0;
float mu21 = 0;
float mu30 = 0;
float ydiff;
float xdiff;
for (unsigned long x = 0; x < image->getHeight(); ++x) {
for (unsigned long y = 0; y < image->getWidth(); ++y) {
pixel = image->getPixel(x,y);
tmp = pixel->getGrayscaleIntensity();
if (tmp > MAX_PIXEL_VAL/2) {
xdiff = x - xcenter;
ydiff = y - ycenter;
mu02 += ydiff*ydiff*tmp;
mu03 += ydiff*ydiff*ydiff*tmp;
mu11 += xdiff*ydiff*tmp;
mu12 += xdiff*ydiff*ydiff*tmp;
mu20 += xdiff*xdiff*tmp;
mu21 += xdiff*xdiff*ydiff*tmp;
mu30 += xdiff*xdiff*xdiff*tmp;
}
}
}
float mu00pow2 = mu00*mu00;
float mu00pow2point5 = pow(mu00,2.5f);
float n02 = mu02/mu00pow2;
float n03 = mu03/mu00pow2point5;
float n11 = mu11/mu00pow2;
float n12 = mu12/mu00pow2point5;
float n20 = mu02/mu00pow2;
float n21 = mu21/mu00pow2point5;;
float n30 = mu30/mu00pow2point5;
momentInvariants[0] = n20 + n02;
momentInvariants[1] = (n20 - n02)*(n20 - n02) + 4*n11*n11;
momentInvariants[2] = (n30 - 3*n12)*(n30 - 3*n12) + (3*n21 - n03)*(3*n21 - n03);
momentInvariants[3] = (n30 + n12)*(n30 + n12) + (n21 + n03)*(n21 + n03);
momentInvariants[4] = (n30 - 3*n12)*(n30 + n12)*((n30 + n12)*(n30 + n12) - 3*(n21 + n03)*(n21 + n03)) + (3*n21 - n03)*(n21 + n03)*(3*(n30 + n12)*(n30 + n12) - (n21 + n03)*(n21 + n03));
momentInvariants[5] = (n20 - n02)*((n30 + n12)*(n30 + n12) - (n21 + n03)*(n21 + n03)) + 4*n11*(n30 + n12)*(n21 + n03);
momentInvariants[6] = (3*n21 - n03)*(n30 + n12)*((n30 + n12)*(n30 + n12) - 3*(n21 + n03)*(n21 + n03)) - (n30 - 3*n12)*(n21 + n03)*(3*(n30 + n12)*(n30 + n12) - (n21 + n03)*(n21 + n03));
float matrix[2][2] = {{0}};
for (unsigned long x = 0; x < image->getHeight(); ++x) {
for (unsigned long y = 0; y < image->getWidth(); ++y) {
pixel = image->getPixel(x,y);
tmp = pixel->getGrayscaleIntensity();
if (tmp > MAX_PIXEL_VAL/2) {
xdiff = x - xcenter;
ydiff = y - ycenter;
matrix[0][0] += xdiff*xdiff;
matrix[0][1] += xdiff*ydiff;
matrix[1][1] += ydiff*ydiff;
}
}
}
matrix[1][0] = matrix[0][1];
float T = matrix[0][0] + matrix[1][1];
float D = matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0];
eigenVal1 = T/2 + sqrt(T*T/4 - D);
eigenVal2 = T/2 - sqrt(T*T/4 - D);
if (matrix[1][0] != 0) {
eigenVect1[0] = eigenVal1 - matrix[1][1];
eigenVect1[1] = matrix[1][0];
eigenVect2[0] = eigenVal2 - matrix[1][1];
eigenVect2[1] = matrix[1][0];
} else {
eigenVect1[0] = 1;
eigenVect1[1] = 0;
eigenVect2[0] = 0;
eigenVect2[1] = 1;
}
if (eigenVal1 > eigenVal2) {
majorAxis[0] = eigenVect1[0];
majorAxis[1] = eigenVect1[1];
minorAxis[0] = eigenVect2[0];
minorAxis[1] = eigenVect2[1];
} else {
majorAxis[0] = eigenVect2[0];
majorAxis[1] = eigenVect2[1];
minorAxis[0] = eigenVect1[0];
minorAxis[1] = eigenVect1[1];
}
}
float FostroDescriptor::getArea() {
return area;
}
float FostroDescriptor::getPerimeter() {
return perimeter;
}
float FostroDescriptor::getPerimeter2OverArea() {
return perim2OverArea;
}
float FostroDescriptor::getMomentInvariant(int index) {
return momentInvariants[index];
}
float FostroDescriptor::getMajorAxis(int index) {
return majorAxis[index];
}
float FostroDescriptor::getMinorAxis(int index) {
return minorAxis[index];
}
float FostroDescriptor::getEigenVector(int vectIndex, int index) {
if (vectIndex == 0) {
return eigenVect1[index];
} else {
return eigenVect2[index];
}
}
float FostroDescriptor::getEigenValue(int index) {
if (index == 0) {
return eigenVal1;
} else {
return eigenVal2;
}
}