Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/shape/src/emdL1_def.hpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include <stdlib.h>
44
#include <math.h>
45
#include <vector>
46
47
/****************************************************************************************\
48
* For EMDL1 Framework *
49
\****************************************************************************************/
50
typedef struct cvEMDEdge* cvPEmdEdge;
51
typedef struct cvEMDNode* cvPEmdNode;
52
struct cvEMDNode
53
{
54
int pos[3]; // grid position
55
float d; // initial value
56
int u;
57
// tree maintenance
58
int iLevel; // level in the tree, 0 means root
59
cvPEmdNode pParent; // pointer to its parent
60
cvPEmdEdge pChild;
61
cvPEmdEdge pPEdge; // point to the edge coming out from its parent
62
};
63
struct cvEMDEdge
64
{
65
float flow; // initial value
66
int iDir; // 1:outward, 0:inward
67
// tree maintenance
68
cvPEmdNode pParent; // point to its parent
69
cvPEmdNode pChild; // the child node
70
cvPEmdEdge pNxt; // next child/edge
71
};
72
typedef std::vector<cvEMDNode> cvEMDNodeArray;
73
typedef std::vector<cvEMDEdge> cvEMDEdgeArray;
74
typedef std::vector<cvEMDNodeArray> cvEMDNodeArray2D;
75
typedef std::vector<cvEMDEdgeArray> cvEMDEdgeArray2D;
76
typedef std::vector<float> floatArray;
77
typedef std::vector<floatArray> floatArray2D;
78
79
/****************************************************************************************\
80
* EMDL1 Class *
81
\****************************************************************************************/
82
class EmdL1
83
{
84
public:
85
EmdL1()
86
{
87
m_pRoot = NULL;
88
binsDim1 = 0;
89
binsDim2 = 0;
90
binsDim3 = 0;
91
dimension = 0;
92
nMaxIt = 500;
93
94
m_pLeave = 0;
95
m_iEnter = 0;
96
nNBV = 0;
97
m_nItr = 0;
98
m_iTo = 0;
99
m_iFrom = 0;
100
m_pEnter = 0;
101
}
102
103
~EmdL1()
104
{
105
}
106
107
float getEMDL1(cv::Mat &sig1, cv::Mat &sig2);
108
void setMaxIteration(int _nMaxIt);
109
110
private:
111
//-- SubFunctions called in the EMD algorithm
112
bool initBaseTrees(int n1=0, int n2=0, int n3=0);
113
bool fillBaseTrees(float *H1, float *H2);
114
bool greedySolution();
115
bool greedySolution2();
116
bool greedySolution3();
117
void initBVTree();
118
void updateSubtree(cvPEmdNode pRoot);
119
bool isOptimal();
120
void findNewSolution();
121
void findLoopFromEnterBV();
122
float compuTotalFlow();
123
124
private:
125
int dimension;
126
int binsDim1, binsDim2, binsDim3; // the histogram contains m_n1 rows and m_n2 columns
127
int nNBV; // number of Non-Basic Variables (NBV)
128
int nMaxIt;
129
cvEMDNodeArray2D m_Nodes; // all nodes
130
cvEMDEdgeArray2D m_EdgesRight; // all edges to right
131
cvEMDEdgeArray2D m_EdgesUp; // all edges to upward
132
std::vector<cvEMDNodeArray2D> m_3dNodes; // all nodes for 3D
133
std::vector<cvEMDEdgeArray2D> m_3dEdgesRight; // all edges to right, 3D
134
std::vector<cvEMDEdgeArray2D> m_3dEdgesUp; // all edges to upward, 3D
135
std::vector<cvEMDEdgeArray2D> m_3dEdgesDeep; // all edges to deep, 3D
136
std::vector<cvPEmdEdge> m_NBVEdges; // pointers to all NON-BV edges
137
std::vector<cvPEmdNode> m_auxQueue; // auxiliary node queue
138
cvPEmdNode m_pRoot; // root of the BV Tree
139
cvPEmdEdge m_pEnter; // Enter BV edge
140
int m_iEnter; // Enter BV edge, index in m_NBVEdges
141
cvPEmdEdge m_pLeave; // Leave BV edge
142
int m_nItr; // number of iteration
143
// auxiliary variables for searching a new loop
144
std::vector<cvPEmdEdge> m_fromLoop;
145
std::vector<cvPEmdEdge> m_toLoop;
146
int m_iFrom;
147
int m_iTo;
148
};
149
150