-
Notifications
You must be signed in to change notification settings - Fork 2
/
pertest.cpp
156 lines (123 loc) · 3.41 KB
/
pertest.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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <unistd.h>
#include <opencv2/opencv.hpp>
#include "neighborhood.hpp"
#define PADDING 20
using namespace std;
using namespace cv;
double per(Neighborhood &neigh, double radius) {
double p = 0.0;
int center = radius + PADDING;
for (int i = 0; i < 2 * center; ++i) {
for (int j = 0; j < 2 * center; ++j) {
Neighborhood::iterator it;
for (it = neigh.begin(); it != neigh.end(); ++it) {
Mat ee = (Mat_<double>(2, 1) << it->x, it->y);
int x = j + it->x;
int y = i + it->y;
if (it->x < 0)
continue;
if (it->x == 0 && it->y > 0)
continue;
int rx = x - center;
int ry = y - center;
double rs = sqrt(rx * rx + ry * ry);
rx = j - center;
ry = i - center;
double rt = sqrt(rx * rx + ry * ry);
if (rs < radius && rt < radius)
continue;
if (rs >= radius && rt >= radius)
continue;
p += norm(ee)
* norm(ee)
* it->dt
/ (2.0 * pow(ee.dot(ee), 3.0 / 2.0));
}
}
}
return p;
}
double per(Neighborhood &neigh, Mat &image) {
double p = 0.0;
for (int i = 0; i < image.rows; ++i) {
for (int j = 0; j < image.cols; ++j) {
Neighborhood::iterator it;
for (it = neigh.begin(); it != neigh.end(); ++it) {
Mat ee = (Mat_<double>(2, 1) << it->x, it->y);
int x = j + it->x;
int y = i + it->y;
if (it->x < 0)
continue;
if (it->x == 0 && it->y > 0)
continue;
if (x < 0 || x >= image.cols || y < 0 || y >= image.rows)
continue;
if (image.at<uchar>(i, j) == image.at<uchar>(y, x))
continue;
p += norm(ee)
* norm(ee)
* it->dt
/ (2.0 * pow(ee.dot(ee), 3.0 / 2.0));
}
}
}
return p;
}
int main(int argc, char *argv[])
{
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
double radius = atof(argv[2]);
Neighborhood neigh;
/* Size 4 */
neigh.add( 1, 0, 1.0);
neigh.add( 0, 1, 1.0);
neigh.add(-1, 0, 1.0);
neigh.add( 0,-1, 1.0);
neigh.setupAngles();
cout << "Size 4: " << per(neigh, radius) << endl;
cout << "Size 4: " << per(neigh, image) << endl;
cout << "Size 4: " << per(neigh, image) / M_PI << endl;
/* Size 8 */
neigh.add( 1, 1, 1.0);
neigh.add(-1, 1, 1.0);
neigh.add( 1,-1, 1.0);
neigh.add(-1,-1, 1.0);
neigh.setupAngles();
cout << "Size 8: " << per(neigh, radius) << endl;
cout << "Size 8: " << per(neigh, image) << endl;
cout << "Size 8: " << per(neigh, image) / M_PI << endl;
/* Size 16 */
neigh.add8(1, 2, 1.0);
neigh.setupAngles();
cout << "Size 16: " << per(neigh, radius) << endl;
cout << "Size 16: " << per(neigh, image) << endl;
cout << "Size 16: " << per(neigh, image) / M_PI << endl;
/* Size 32 */
neigh.add8(3, 1, 1.0);
neigh.add8(3, 2, 1.0);
neigh.setupAngles();
cout << "Size 32: " << per(neigh, radius) << endl;
cout << "Size 32: " << per(neigh, image) << endl;
cout << "Size 32: " << per(neigh, image) / M_PI << endl;
/* Size 48 */
neigh.add8(1, 4, 1.0);
neigh.add8(3, 4, 1.0);
neigh.setupAngles();
cout << "Size 48: " << per(neigh, radius) << endl;
cout << "Size 48: " << per(neigh, image) << endl;
cout << "Size 48: " << per(neigh, image) / M_PI << endl;
/* Size 72 */
neigh.add8(1, 5, 1.0);
neigh.add8(2, 5, 1.0);
neigh.add8(3, 5, 1.0);
neigh.setupAngles();
cout << "Size 72: " << per(neigh, radius) << endl;
cout << "Size 72: " << per(neigh, image) << endl;
cout << "Size 72: " << per(neigh, image) / M_PI << endl;
return 0;
}