forked from larrylindsey/imageprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectedComponent.cpp
More file actions
241 lines (183 loc) · 5.62 KB
/
Copy pathConnectedComponent.cpp
File metadata and controls
241 lines (183 loc) · 5.62 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <boost/make_shared.hpp>
#include <boost/functional/hash.hpp>
#include <imageprocessing/exceptions.h>
#include "ConnectedComponent.h"
ConnectedComponent::ConnectedComponent() :
_boundingBox(0, 0, 0, 0),
_center(0, 0) {}
ConnectedComponent::ConnectedComponent(
boost::shared_ptr<Image> source,
double value,
boost::shared_ptr<pixel_list_type> pixelList,
unsigned int begin,
unsigned int end) :
_pixels(pixelList),
_value(value),
_boundingBox(0, 0, 0, 0),
_center(0, 0),
_source(source),
_begin(_pixels->begin() + begin),
_end(_pixels->begin() + end),
_bitmapDirty(true),
_hashValue(0),
_hashDirty(true) {
// if there is at least one pixel
if (begin != end) {
_boundingBox.minX = _begin->x;
_boundingBox.maxX = _begin->x + 1;
_boundingBox.minY = _begin->y;
_boundingBox.maxY = _begin->y + 1;
}
foreach (const util::point<unsigned int>& pixel, getPixels()) {
_boundingBox.minX = std::min(_boundingBox.minX, (int)pixel.x);
_boundingBox.maxX = std::max(_boundingBox.maxX, (int)pixel.x + 1);
_boundingBox.minY = std::min(_boundingBox.minY, (int)pixel.y);
_boundingBox.maxY = std::max(_boundingBox.maxY, (int)pixel.y + 1);
_center += pixel;
}
_center /= getSize();
}
double
ConnectedComponent::getValue() const {
return _value;
}
const util::point<double>&
ConnectedComponent::getCenter() const {
return _center;
}
const std::pair<ConnectedComponent::const_iterator, ConnectedComponent::const_iterator>
ConnectedComponent::getPixels() const {
return std::make_pair(_begin, _end);
}
const boost::shared_ptr<ConnectedComponent::pixel_list_type>
ConnectedComponent::getPixelList() const {
return _pixels;
}
unsigned int
ConnectedComponent::getSize() const {
return _end - _begin;
}
const util::rect<int>&
ConnectedComponent::getBoundingBox() const {
return _boundingBox;
}
const ConnectedComponent::bitmap_type&
ConnectedComponent::getBitmap() const {
if (_bitmapDirty) {
_bitmap.reshape(bitmap_type::size_type(_boundingBox.width(), _boundingBox.height()), false);
foreach (const util::point<int>& pixel, getPixels())
_bitmap(pixel.x - _boundingBox.minX, pixel.y - _boundingBox.minY) = true;
_bitmapDirty = false;
}
return _bitmap;
}
bool
ConnectedComponent::operator<(const ConnectedComponent& other) const {
return getSize() < other.getSize();
}
ConnectedComponent
ConnectedComponent::translate(const util::point<int>& pt)
{
boost::shared_ptr<pixel_list_type> translation = boost::make_shared<pixel_list_type>();
foreach (const util::point<unsigned int>& pixel, getPixels())
{
translation->push_back(pixel + pt);
}
return ConnectedComponent(_source, _value, translation, 0, translation->size());
}
ConnectedComponent
ConnectedComponent::intersect(const ConnectedComponent& other) {
boost::shared_ptr<pixel_list_type> intersection = boost::make_shared<pixel_list_type>();
bitmap_type::size_type size = getBitmap().shape();
foreach (const util::point<unsigned int>& pixel, other.getPixels())
if (_boundingBox.contains(pixel)) {
unsigned int x = pixel.x - _boundingBox.minX;
unsigned int y = pixel.y - _boundingBox.minY;
if (x >= size[0] || y >= size[1])
continue;
if (_bitmap(x, y))
intersection->push_back(pixel);
}
return ConnectedComponent(_source, _value, intersection, 0, intersection->size());
}
bool ConnectedComponent::intersects(const ConnectedComponent& other)
{
if (_boundingBox.intersects(other.getBoundingBox()))
{
bitmap_type::size_type size = getBitmap().shape();
foreach (const util::point<unsigned int>& pixel, other.getPixels())
{
if (_boundingBox.contains(pixel)) {
unsigned int x = pixel.x - _boundingBox.minX;
unsigned int y = pixel.y - _boundingBox.minY;
if (x >= size[0] || y >= size[1])
continue;
if (_bitmap(x, y))
{
return true;
}
}
}
}
return false;
}
bool
ConnectedComponent::operator==(const ConnectedComponent& other) const
{
util::rect<int> thisBound = getBoundingBox();
util::rect<int> otherBound = other.getBoundingBox();
if (thisBound == otherBound && hashValue() == other.hashValue())
{
// If this bound equals that bound
bitmap_type thisBitmap = getBitmap();
bitmap_type otherBitmap = other.getBitmap();
//Check that the other's bitmap contains all of our pixels.
foreach (const util::point<unsigned int> pixel, getPixels())
{
if (!otherBitmap(pixel.x - thisBound.minX, pixel.y - thisBound.minY))
{
return false;
}
}
//Check that our bitmap contains all of the other's pixels.
foreach (const util::point<unsigned int> pixel, other.getPixels())
{
if (!thisBitmap(pixel.x - otherBound.minX, pixel.y - otherBound.minY))
{
return false;
}
}
//If both conditions are true, both components contain each other, and are therefore equal.
return true;
}
else
{
// If our bound is unequal to the other's bound, then we're unequal.
return false;
}
}
std::size_t
ConnectedComponent::hashValue() const
{
if (_hashDirty) {
// Calculate geometric hash value
boost::hash_combine(_hashValue, boost::hash_value(_boundingBox.minX));
boost::hash_combine(_hashValue, boost::hash_value(_boundingBox.minY));
boost::hash_combine(_hashValue, boost::hash_value(_boundingBox.maxX));
boost::hash_combine(_hashValue, boost::hash_value(_boundingBox.maxY));
const bitmap_type thisBitmap = getBitmap();
for (int x = 0; x < thisBitmap.width(); ++x)
{
for (int y = 0; y < thisBitmap.height(); ++y)
{
if (thisBitmap(x, y))
{
boost::hash_combine(_hashValue, boost::hash_value(x + _boundingBox.minX));
boost::hash_combine(_hashValue, boost::hash_value(y + _boundingBox.minY));
}
}
}
_hashDirty = false;
}
return _hashValue;
}