-
Notifications
You must be signed in to change notification settings - Fork 1
/
region-ops.cpp
244 lines (220 loc) · 5.76 KB
/
region-ops.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**********************************
region-ops.cpp
Basic operations on regions of pixels
Jaime Silvela
January 2000
**********************************/
#include <stack>
#include <iostream>
#include <assert.h>
#include "image.hpp"
bool VERBOSE = false;
bool BREADTH_FIRST = true;
const int SMALLEST_WHITE_REGION = 10000;
const int SMALLEST_BLACK_REGION = 2000;
const int NUM_FEATURES = 13;
double feature_vector[NUM_FEATURES];
char *_filename;
extern void display (Image im, char *title);
bool Image::in_range(point pt)
{
return (pt >= start && pt <= end);
}
int Image::fill4_breadth_first(point first, grey g_in, grey g_out)
{
queue<point> Q;
int re=0;
int i;
point aux;
*first=g_out;
++re;
Q.push(first);
while (!Q.empty()) {
first = Q.front();
for (i=0; i<4; ++i) {
aux = first + offset_4neighbor[i];
if (in_range(aux) && *aux==g_in) {
*aux=g_out;
++re;
Q.push(aux);
}
}
Q.pop();
}
return re;
}
int Image::fill4_depth_first(point first, grey g_in, grey g_out)
{
stack<point> S;
int i, re=0;
bool is_on_top;
point aux;
*first=g_out;
S.push(first);
++re;
while (!S.empty()) {
first = S.top();
is_on_top = true;
for (i=0; i<4; ++i) {
aux = first + offset_4neighbor[i];
if (in_range(aux) && *aux==g_in) {
is_on_top = false;
*aux=g_out;
++re;
S.push(aux);
break;
}
}
if (is_on_top) S.pop();
}
return re;
}
int Image::fill4_recursive(point first, grey g_in, grey g_out)
{
point aux;
int re=1;
*first=g_out;
for (int i=0; i<4; ++i) {
aux = first + offset_4neighbor[i];
if (in_range(aux) && *aux==g_in)
re += fill4_recursive(aux, g_in, g_out);
}
return re;
}
int Image::fill(point first, grey g_in, grey g_out)
{
if (BREADTH_FIRST)
return fill4_breadth_first(first, g_in, g_out);
else
return fill4_depth_first(first, g_in, g_out);
}
// get_white_regions: puts hand pixels to white, others to black
void Image::get_white_regions()
{
point pt, last;
grey g_out = 1;
last=start-1;
int area;
while ( (pt=get_next_point_with_level(white, last))) {
assert(g_out < white);
area = fill(pt, white, g_out);
if (area > SMALLEST_WHITE_REGION) {
feature_vector[hand_area]=area;
filter_by_level(g_out);
return;
}
++g_out;
last = pt;
}
}
void Image::erase_small_whites()
{
point pt, last, winner=0;
grey wingrey=0, g_out = 1;
last=start-1;
int area;
while ( (pt=get_next_point_with_level(white, last))) {
assert(g_out < white);
area = fill(pt, white, g_out);
if (area < SMALLEST_WHITE_REGION) {
fill(pt, g_out, black);
} else if (area >= SMALLEST_WHITE_REGION) {
winner = pt;
wingrey = g_out;
}
++g_out;
last = pt;
}
fill(winner, wingrey, white);
}
// get_black_regions: gives a different greylevel to each region larger than
// a threshold area. Deletes smaller regions
void Image::get_black_regions()
{
int holes = 0;
point pt, last;
grey g_out = 1;
last=start-1;
while ((pt=get_next_point_with_level(black, last))) {
assert(g_out < white);
if (fill(pt, black, g_out) < SMALLEST_BLACK_REGION)
fill(pt, g_out, white);
else
++holes;
++g_out;
last=pt;
}
feature_vector[num_holes] = holes-1;
}
struct region {
grey original_color;
int size;
point start_point;
};
struct region make_region(grey lvl, int sz, point startpt)
{
struct region re;
re.original_color = lvl;
re.size = sz;
re.start_point = startpt;
return re;
}
vector<struct region> region_map;
void Image::get_regions_with_level(grey level)
{
point pt, last;
struct region reg;
static grey g_out = 1;
last=start-1;
while ( (pt=get_next_point_with_level(level, last)) ) {
assert(g_out < white);
reg = make_region(level, fill(pt, level, g_out), pt);
region_map.push_back(reg);
++g_out;
last=pt;
}
}
void Image::get_regions()
{
get_regions_with_level(white);
if (VERBOSE) std::cout << "Got white regions" << endl;
get_regions_with_level(black);
if (VERBOSE) std::cout << "Got black regions" << endl;
int size = region_map.size();
if (VERBOSE) std::cout << "Image has " << size << " regions" << endl;
for (int i=0; i<size; ++i) {
if (region_map[i].size < SMALLEST_BLACK_REGION)
if (region_map[i].original_color == white) {
fill( region_map[i].start_point,
*(region_map[i].start_point),
black);
region_map[i].start_point = 0;
} else {
fill( region_map[i].start_point,
*(region_map[i].start_point),
white);
region_map[i].start_point = 0;
}
else if (region_map[i].size >= SMALLEST_WHITE_REGION)
fill( region_map[i].start_point,
*(region_map[i].start_point),
region_map[i].original_color);
}
}
// number_of_holes: counts holes by counting different greylevels, one
// for each region
int Image::number_of_holes()
{
int i, re = 0;
int *levels = new int[grey_depth];
point pt;
for (i=0; i<grey_depth; ++i)
levels[i]=0;
for (pt = start; pt <= end; ++pt)
++levels[*pt];
for (i=0; i<grey_depth; ++i)
if (levels[i])
++re;
delete []levels;
return re-2;
}