-
Notifications
You must be signed in to change notification settings - Fork 0
/
humoments.cpp
109 lines (93 loc) · 4.04 KB
/
humoments.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
#include "humoments.h"
myHuMoments::myHuMoments(unsigned char* binaryImage, int width, int height)
{
this->width = width;
this->height = height;
object = new unsigned char[width * height];
this->object = binaryImage;
}
myHuMoments::~myHuMoments()
{
delete[] object;
}
double myHuMoments::calcMoment(double p, double q)
{
double sum = 0;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
sum += object[x + y * width] * pow(x, p) * pow(y, q);
}
}
return sum;
}
void myHuMoments::calcOrgins()
{
orginOfX = calcMoment(1, 0) / calcMoment(0, 0);
orginOfY = calcMoment(0, 1) / calcMoment(0, 0);
}
double myHuMoments::calcOrginMoment(double p, double q)
{
double sum = 0;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
sum += object[x + y * width] * pow((x - orginOfX), p) * pow((y - orginOfY), q);
}
}
return sum;
}
double myHuMoments::calcNormalizedMoment(double p, double q)
{
double temp = (p + q) / 2 + 1;
double a = calcOrginMoment(p, q) / pow(calcOrginMoment(0, 0), temp);
return a;
}
void myHuMoments::calcInvariantMoments()
{
huMoments[0] = calcNormalizedMoment(2, 0)
+ calcNormalizedMoment(0, 2);
huMoments[1] = pow( (calcNormalizedMoment(2, 0) - calcNormalizedMoment(0, 2) ), 2)
+ 4 * ( pow(calcNormalizedMoment(1, 1), 2) );
huMoments[2] = pow( ( calcNormalizedMoment(3, 0) - 3 * calcNormalizedMoment(1, 2) ) , 2)
+ pow( ( 3 * calcNormalizedMoment(2, 1) - calcNormalizedMoment(0, 3) ) , 2);
huMoments[3] = pow( (calcNormalizedMoment(3, 0) + calcNormalizedMoment(1, 2) ), 2)
+ pow( (calcNormalizedMoment(2, 1) + calcNormalizedMoment(0, 3) ), 2);
huMoments[4] = ( calcNormalizedMoment(3, 0) - 3 * calcNormalizedMoment(1, 2) )
* ( calcNormalizedMoment(3, 0) + calcNormalizedMoment(1, 2) )
* ( pow(calcNormalizedMoment(3, 0) + calcNormalizedMoment(1, 2), 2)
- 3 * pow(calcNormalizedMoment(2, 1) + calcNormalizedMoment(0, 3), 2) )
+ ( 3 * calcNormalizedMoment(2, 1) - calcNormalizedMoment(0, 3) )
* ( calcNormalizedMoment(2, 1) + calcNormalizedMoment(0, 3) )
* ( 3 * pow( calcNormalizedMoment(3, 0) + calcNormalizedMoment(1, 2), 2)
- pow( calcNormalizedMoment(2, 1) + calcNormalizedMoment(0, 3), 2) );
huMoments[5] = ( calcNormalizedMoment(2, 0) - calcNormalizedMoment(0, 2) )
* ( pow( calcNormalizedMoment(3, 0) + calcNormalizedMoment(1, 2), 2)
- pow( calcNormalizedMoment(2, 1) + calcNormalizedMoment(0, 3), 2) )
+ ( 4 * calcNormalizedMoment(1, 1)
* ( calcNormalizedMoment(3, 0) + calcNormalizedMoment(1, 2) )
* ( calcNormalizedMoment(2, 1) + calcNormalizedMoment(0, 3) ) );
huMoments[6] = ( 3 * calcNormalizedMoment(2, 1) - calcNormalizedMoment(0, 3) )
* ( calcNormalizedMoment(3, 0) + calcNormalizedMoment(1, 2) )
* ( pow( calcNormalizedMoment(3, 0) + calcNormalizedMoment(1, 2), 2)
- 3 * pow( calcNormalizedMoment(2, 1) + calcNormalizedMoment(0, 3), 2) )
- ( calcNormalizedMoment(3, 0) - 3 * calcNormalizedMoment(1, 2) )
* ( calcNormalizedMoment(2, 1) + calcNormalizedMoment(0, 3) )
* ( 3 * pow( calcNormalizedMoment(3, 0) + calcNormalizedMoment(1, 2), 2)
- pow( calcNormalizedMoment(2, 1) + calcNormalizedMoment(0, 3), 2) );
}
double* myHuMoments::getHuMoments()
{
this->calcOrgins();
this->calcInvariantMoments();
return huMoments;
}
void myHuMoments::logTransform()
{
for (int i = 0; i < 7; i++)
{
huMoments[i] = -1 * copysign(1.0, huMoments[i]) * log10(fabs(huMoments[i]));
}
}