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
121 lines (85 loc) · 2.76 KB
/
Copy pathConnectedComponent.cpp
File metadata and controls
121 lines (85 loc) · 2.76 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
#include <boost/make_shared.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) {
// 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();
_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;
}
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 {
return _bitmap;
}
bool
ConnectedComponent::operator<(const ConnectedComponent& other) const {
return getSize() < other.getSize();
}
ConnectedComponent
ConnectedComponent::intersect(const ConnectedComponent& other) {
boost::shared_ptr<pixel_list_type> intersection = boost::make_shared<pixel_list_type>();
bitmap_type::size_type size = _bitmap.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());
}