/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include <stdlib.h>43#include <math.h>44#include <vector>4546/****************************************************************************************\47* For EMDL1 Framework *48\****************************************************************************************/49typedef struct cvEMDEdge* cvPEmdEdge;50typedef struct cvEMDNode* cvPEmdNode;51struct cvEMDNode52{53int pos[3]; // grid position54float d; // initial value55int u;56// tree maintenance57int iLevel; // level in the tree, 0 means root58cvPEmdNode pParent; // pointer to its parent59cvPEmdEdge pChild;60cvPEmdEdge pPEdge; // point to the edge coming out from its parent61};62struct cvEMDEdge63{64float flow; // initial value65int iDir; // 1:outward, 0:inward66// tree maintenance67cvPEmdNode pParent; // point to its parent68cvPEmdNode pChild; // the child node69cvPEmdEdge pNxt; // next child/edge70};71typedef std::vector<cvEMDNode> cvEMDNodeArray;72typedef std::vector<cvEMDEdge> cvEMDEdgeArray;73typedef std::vector<cvEMDNodeArray> cvEMDNodeArray2D;74typedef std::vector<cvEMDEdgeArray> cvEMDEdgeArray2D;75typedef std::vector<float> floatArray;76typedef std::vector<floatArray> floatArray2D;7778/****************************************************************************************\79* EMDL1 Class *80\****************************************************************************************/81class EmdL182{83public:84EmdL1()85{86m_pRoot = NULL;87binsDim1 = 0;88binsDim2 = 0;89binsDim3 = 0;90dimension = 0;91nMaxIt = 500;9293m_pLeave = 0;94m_iEnter = 0;95nNBV = 0;96m_nItr = 0;97m_iTo = 0;98m_iFrom = 0;99m_pEnter = 0;100}101102~EmdL1()103{104}105106float getEMDL1(cv::Mat &sig1, cv::Mat &sig2);107void setMaxIteration(int _nMaxIt);108109private:110//-- SubFunctions called in the EMD algorithm111bool initBaseTrees(int n1=0, int n2=0, int n3=0);112bool fillBaseTrees(float *H1, float *H2);113bool greedySolution();114bool greedySolution2();115bool greedySolution3();116void initBVTree();117void updateSubtree(cvPEmdNode pRoot);118bool isOptimal();119void findNewSolution();120void findLoopFromEnterBV();121float compuTotalFlow();122123private:124int dimension;125int binsDim1, binsDim2, binsDim3; // the histogram contains m_n1 rows and m_n2 columns126int nNBV; // number of Non-Basic Variables (NBV)127int nMaxIt;128cvEMDNodeArray2D m_Nodes; // all nodes129cvEMDEdgeArray2D m_EdgesRight; // all edges to right130cvEMDEdgeArray2D m_EdgesUp; // all edges to upward131std::vector<cvEMDNodeArray2D> m_3dNodes; // all nodes for 3D132std::vector<cvEMDEdgeArray2D> m_3dEdgesRight; // all edges to right, 3D133std::vector<cvEMDEdgeArray2D> m_3dEdgesUp; // all edges to upward, 3D134std::vector<cvEMDEdgeArray2D> m_3dEdgesDeep; // all edges to deep, 3D135std::vector<cvPEmdEdge> m_NBVEdges; // pointers to all NON-BV edges136std::vector<cvPEmdNode> m_auxQueue; // auxiliary node queue137cvPEmdNode m_pRoot; // root of the BV Tree138cvPEmdEdge m_pEnter; // Enter BV edge139int m_iEnter; // Enter BV edge, index in m_NBVEdges140cvPEmdEdge m_pLeave; // Leave BV edge141int m_nItr; // number of iteration142// auxiliary variables for searching a new loop143std::vector<cvPEmdEdge> m_fromLoop;144std::vector<cvPEmdEdge> m_toLoop;145int m_iFrom;146int m_iTo;147};148149150