summaryrefslogtreecommitdiff
path: root/src/imaging/Blob.h
blob: aff199457102724c9cc97a451504ab0f2f829cf5 (plain)
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
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//
//  Original author of this file is igor@c-base.org 
//

#ifndef _ConnectedComps_H_
#define _ConnectedComps_H_

#include "../api.h"
#include "Run.h"

#include "../graphics/Bitmap.h"
#include "../graphics/Pixel32.h"

#include "../base/GLMHelper.h"

#include <vector>

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

namespace avg {

class Blob;
typedef boost::shared_ptr<class Blob> BlobPtr;
typedef boost::weak_ptr<class Blob> BlobWeakPtr;
typedef std::vector<BlobPtr> BlobVector;
typedef std::vector<BlobWeakPtr> BlobWeakPtrVector;
typedef boost::shared_ptr<BlobVector> BlobVectorPtr;
typedef std::vector<IntPoint> ContourSeq;

class AVG_API Blob
{
    public:
        Blob(const Run& run);
        ~Blob();

        void addRun(const Run& run);
        void merge(const BlobPtr& other);
        RunArray* getRuns();
        void render(BitmapPtr pSrcBmp, BitmapPtr pDestBmp, Pixel32 Color, 
                int Min, int Max, bool bFinger, bool bMarkCenter, 
                Pixel32 CenterColor= Pixel32(0x00, 0x00, 0xFF, 0xFF));
        bool contains(IntPoint pt);

        void calcStats();
        void calcContour(int Precision);
        ContourSeq getContour();

        const glm::vec2& getCenter() const;
        const glm::vec2& getEstimatedNextCenter() const;
        float getArea() const;
        const IntRect & getBoundingBox() const;
        float getEccentricity() const;
        float getInertia() const;
        float getOrientation() const;
        const glm::vec2& getScaledBasis(int i) const;
        const glm::vec2& getEigenVector(int i) const;
        const glm::vec2& getEigenValues() const;

        void calcNextCenter(glm::vec2 oldCenter);
        void clearRelated();
        void addRelated(BlobPtr pBlob);
        const BlobPtr getFirstRelated(); 

        BlobPtr m_pParent;

    private:
        Blob(const Blob &);
        glm::vec2 calcCenter();
        IntRect calcBBox();
        int calcArea();
        void initRowPositions();
        IntPoint findNeighborInside(const IntPoint& Pt, int& Dir);
        bool ptIsInBlob(const IntPoint& Pt);

        RunArray m_Runs; // This array is unsorted until contours are calculated.
        std::vector<RunArray::iterator> m_RowPositions;
        BlobWeakPtrVector m_RelatedBlobs; // For fingers, this contains the hand.
                                          // For hands, this contains the fingers.

        bool m_bStatsAvailable;
        glm::vec2 m_EstimatedNextCenter;
        glm::vec2 m_Center;
        float m_Area;
        IntRect m_BoundingBox;
        float m_Eccentricity;
        float m_Inertia;
        float m_Orientation;
        glm::vec2 m_ScaledBasis[2];
        glm::vec2 m_EigenVector[2];
        glm::vec2 m_EigenValues;

        ContourSeq m_Contour;
};

BlobVectorPtr AVG_API findConnectedComponents(BitmapPtr pBmp, 
        unsigned char threshold);

}

#endif