1 /***********************************************************************
2 FindBlobs - Helper function to extract all eight-connected blobs of
3 pixels from a frame that match an arbitrary property.
4 Copyright (c) 2010-2013 Oliver Kreylos
6 This file is part of the Augmented Reality Sandbox (SARndbox).
8 The Augmented Reality Sandbox is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 The Augmented Reality Sandbox is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with the Augmented Reality Sandbox; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ***********************************************************************/
23 #ifndef FINDBLOBS_INCLUDED
24 #define FINDBLOBS_INCLUDED
28 template <class PixelParam
>
29 class BlobProperty
{ // Class to accumulate additional pixel properties along with blobs
30 /* Embedded classes: */
32 typedef PixelParam Pixel
; // Underlying pixel type
35 void addPixel(unsigned int x
, unsigned int y
,
36 const PixelParam
& pixelValue
) { // Adds a pixel to the property accumulator
38 void merge(const BlobProperty
&
39 other
) { // Merges two blob property accumulators when their respecive blobs are merged
43 template <class PixelParam
>
44 struct Blob
{ // Structure for extracted blobs
45 /* Embedded classes: */
47 typedef PixelParam Pixel
; // Underlying pixel type
51 double x
, y
; // Position of blob's centroid
52 unsigned int min
[2], max
[2]; // Bounding box of blob
53 BlobProperty
<Pixel
> blobProperty
; // Additional accumulated blob property
56 template <class PixelParam
>
57 class PixelProperty
{ // Class to check whether a pixel should be considered part of a blob
58 /* Embedded classes: */
60 typedef PixelParam Pixel
; // Underlying pixel type
63 bool operator()(unsigned int x
, unsigned int y
,
64 const Pixel
& pixel
) const { // Returns true if the given pixel satisfies the property
69 template <class PixelParam
, class PixelPropertyParam
>
70 std::vector
<Blob
<PixelParam
> > findBlobs(const unsigned int size
[2], const PixelParam
* frame
,
71 const PixelPropertyParam
&
72 property
); // Extracts all connected blobs from the given frame whose pixels have the given property
74 #ifndef FINDBLOBS_IMPLEMENTATION
75 #include "FindBlobs.icpp"