forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuMoments.cpp
52 lines (38 loc) · 1.03 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
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
bool showLogTransformedHuMoments = true;
for (int i = 1; i < argc; i++)
{
// Obtain filename from command line argument
string filename(argv[i]);
// Read Image
Mat im = imread(filename,IMREAD_GRAYSCALE);
// Threshold image
threshold(im, im, 128, 255, THRESH_BINARY);
// Calculate Moments
Moments moment = moments(im, false);
// Calculate Hu Moments
double huMoments[7];
HuMoments(moment, huMoments);
// Print Hu Moments
cout << filename << ": ";
for(int i = 0; i < 7; i++)
{
if(showLogTransformedHuMoments)
{
// Log transform Hu Moments to make squash the range
cout << -1 * copysign(1.0, huMoments[i]) * log10(abs(huMoments[i])) << " ";
}
else
{
// Hu Moments without log transform.
cout << huMoments[i] << " ";
}
}
// One row per file
cout << endl;
}
}