-
Notifications
You must be signed in to change notification settings - Fork 23
/
FindBlobs.icpp
176 lines (158 loc) · 4.89 KB
/
FindBlobs.icpp
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
/***********************************************************************
FindBlobs - Helper function to extract all eight-connected blobs of
pixels from a frame whose pixel values match a given value up to a given
tolerance.
Copyright (c) 2010-2015 Oliver Kreylos
This file is part of the Kinect 3D Video Capture Project (Kinect).
The Kinect 3D Video Capture Project is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
The Kinect 3D Video Capture Project is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Kinect 3D Video Capture Project; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#define FINDBLOBS_IMPLEMENTATION
#include "FindBlobs.h"
namespace {
template <class PixelParam>
struct LineBlob // Helper structure to assemble blobs one line at a time
{
/* Embedded classes: */
public:
typedef PixelParam Pixel; // Underlying pixel type
/* Elements: */
public:
int x1,x2;
int y;
unsigned int parent,rank;
int min[2],max[2];
double sumX,sumY,sumW;
BlobProperty<Pixel> blobProperty;
/* Methods: */
void merge(const LineBlob& other)
{
for(int i=0;i<2;++i)
{
if(min[i]>other.min[i])
min[i]=other.min[i];
if(max[i]<other.max[i])
max[i]=other.max[i];
}
sumX+=other.sumX;
sumY+=other.sumY;
sumW+=other.sumW;
blobProperty.merge(other.blobProperty);
}
};
}
template <class PixelParam>
inline
std::vector<Blob<PixelParam> >
findBlobs(const Kinect::FrameBuffer& frame,
const PixelComparer<PixelParam>& comparer)
{
/* Create a list of line blobs: */
std::vector<LineBlob<PixelParam> > lineBlobs;
unsigned int numLineBlobs=0; // Number of line blobs in the current list
unsigned int lastLineStart=0; // Index of first line blob for the previous pixel row
unsigned int lastLineEnd=0; // Index one after last line blob for the previous pixel row
/* Process all pixel rows: */
const PixelParam* frameRowPtr=frame.getData<PixelParam>();
for(int y=0;y<frame.getSize(1);++y,frameRowPtr+=frame.getSize(0))
{
/* Find all line blobs on the current line: */
int x=0;
const PixelParam* framePtr=frameRowPtr;
while(x<frame.getSize(0))
{
/* Skip non-similar pixels: */
while(x<frame.getSize(0)&&!comparer(*framePtr))
{
++x;
++framePtr;
}
if(x>=frame.getSize(0))
break;
/* Collect a new line blob: */
LineBlob<PixelParam> lb;
lb.x1=x;
while(x<frame.getSize(0)&&comparer(*framePtr))
{
lb.blobProperty.addPixel(x,y,*framePtr);
++x;
++framePtr;
}
lb.x2=x;
lb.y=y;
lb.parent=numLineBlobs;
lb.rank=0;
lb.min[0]=lb.x1;
lb.min[1]=y;
lb.max[0]=lb.x2;
lb.max[1]=y+1;
lb.sumW=double(lb.x2-lb.x1);
lb.sumX=double(lb.x1+lb.x2-1)*lb.sumW*0.5;
lb.sumY=double(y)*lb.sumW;
lineBlobs.push_back(lb);
++numLineBlobs;
/* Merge the new line blob with any line blobs it touches from the previous line: */
for(unsigned int i=lastLineStart;i<lastLineEnd;++i)
{
if(lineBlobs[i].x1<=lb.x2&&lineBlobs[i].x2>=lb.x1) // Check detects eight-connected blobs
{
/* Merge the two blobs: */
unsigned int root1=i;
while(root1!=lineBlobs[root1].parent)
root1=lineBlobs[root1].parent;
unsigned int root2=numLineBlobs-1;
while(root2!=lineBlobs[root2].parent)
root2=lineBlobs[root2].parent;
if(root1!=root2)
{
if(lineBlobs[root1].rank>lineBlobs[root2].rank)
{
lineBlobs[root2].parent=root1;
lineBlobs[root1].merge(lineBlobs[root2]);
}
else
{
lineBlobs[root1].parent=root2;
if(lineBlobs[root1].rank==lineBlobs[root2].rank)
++lineBlobs[root2].rank;
lineBlobs[root2].merge(lineBlobs[root1]);
}
}
}
}
}
/* Go to the next line: */
lastLineStart=lastLineEnd;
lastLineEnd=numLineBlobs;
}
/* Convert all line blobs that are their own parents into "real" blobs: */
std::vector<Blob<PixelParam> > result;
for(unsigned int i=0;i<numLineBlobs;++i)
{
/* Check if the line blob is a root and not just a single pixel: */
if(lineBlobs[i].parent==i&&lineBlobs[i].sumW>0.0)
{
Blob<PixelParam> b;
b.x=(lineBlobs[i].sumX+lineBlobs[i].sumW*0.5)/lineBlobs[i].sumW;
b.y=(lineBlobs[i].sumY+lineBlobs[i].sumW*0.5)/lineBlobs[i].sumW;
for(int j=0;j<2;++j)
{
b.min[j]=lineBlobs[i].min[j];
b.max[j]=lineBlobs[i].max[j];
}
b.blobProperty=lineBlobs[i].blobProperty;
result.push_back(b);
}
}
return result;
}