cvlcm.cpp.svn-base

来自「非结构化路识别」· SVN-BASE 代码 · 共 728 行 · 第 1/2 页

SVN-BASE
728
字号
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

/* Hybrid linear-contour model reconstruction */
#include "_cvaux.h"

#define CV_IMPL CV_EXTERN_C

const float LCM_CONST_ZERO = 1e-6f;

/****************************************************************************************\
*                                    Auxiliary struct definitions                                 *
\****************************************************************************************/
typedef struct CvLCM
{
    CvGraph* Graph;
    CvVoronoiDiagram2D* VoronoiDiagram;
    CvMemStorage* ContourStorage;
    CvMemStorage* EdgeStorage;
    float maxWidth;
} CvLCM;

typedef struct CvLCMComplexNodeData
{
    CvVoronoiNode2D edge_node;
    CvPoint2D32f site_first_pt;
    CvPoint2D32f site_last_pt;
    CvVoronoiSite2D* site_first;
    CvVoronoiSite2D* site_last;
    CvVoronoiEdge2D* edge;
} CvLCMComplexNodeData;

typedef struct CvLCMData
{
    CvVoronoiNode2D* pnode;
    CvVoronoiSite2D* psite;
    CvVoronoiEdge2D* pedge;
} CvLCMData;


/****************************************************************************************\
*                                    Function definitions                                *
\****************************************************************************************/

#define _CV_READ_SEQ_ELEM( elem, reader, type )                       \
{                                                              \
    assert( (reader).seq->elem_size == sizeof(*elem));         \
    elem = (type)(reader).ptr;                                 \
    CV_NEXT_SEQ_ELEM( sizeof(*elem), reader )                  \
}

#define _CV_IS_SITE_REFLEX( SITE )  ((SITE) ->node[0] == (SITE) ->node[1])
#define _CV_IS_EDGE_REFLEX( EDGE )  (( (EDGE)->site[0]->node[0] == (EDGE)->site[0]->node[0] ) || \
                                      ( (EDGE)->site[1]->node[0] == (EDGE)->site[1]->node[0] ) )

#define _CV_INITIALIZE_CVLCMDATA(STRUCT,SITE,EDGE,NODE)\
{ (STRUCT)->psite = SITE ; (STRUCT)->pedge = EDGE; (STRUCT)->pnode = NODE;}
/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvConstructLCM
//    Purpose: Function constructs hybrid model
//    Context:
//    Parameters:
//      LCM : in&out.
//    Returns: 1, if hybrid model was succesfully constructed
//             0, if some error occures
//F*/
OPENCVAPI 
int _cvConstructLCM(CvLCM* LCM);

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvConstructLCMComplexNode
//    Purpose: Function constructs Complex Node (node, which consists of
//             two points and more) of hybrid model
//    Context:
//    Parameters:
//      pLCM : in&out.
//      pLCMEdge: in, input edge of hybrid model
//      pLCMInputData: in, input parameters
//    Returns: pointer to constructed node
//F*/
OPENCVAPI
CvLCMNode* _cvConstructLCMComplexNode(CvLCM* pLCM,
                                      CvLCMEdge* pLCMEdge,
                                      CvLCMData* pLCMInputData);

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvConstructLCMSimpleNode
//    Purpose: Function constructs Simple Node (node, which consists of
//             one point) of hybrid model
//    Context:
//    Parameters:
//      pLCM : in&out.
//      pLCMEdge: in, input edge of hybrid model
//      pLCMInputData: in, input parameters
//    Returns: pointer to constructed node
//F*/
OPENCVAPI
CvLCMNode* _cvConstructLCMSimpleNode(CvLCM* pLCM,
                                    CvLCMEdge* pLCMEdge,
                                    CvLCMData* pLCMInputData);

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvConstructLCMSimpleNode
//    Purpose: Function constructs Edge of hybrid model
//    Context:
//    Parameters:
//      pLCM : in&out.
//      pLCMInputData: in, input parameters
//    Returns: pointer to constructed edge
//F*/
OPENCVAPI
CvLCMEdge* _cvConstructLCMEdge(CvLCM* pLCM,
                               CvLCMData* pLCMInputData);

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvTreatExeptionalCase
//    Purpose: Function treats triangles and regular polygons
//    Context:
//    Parameters:
//      pLCM : in, information about graph
//      pLCMInputData: in, input parameters
//    Returns: pointer to graph node
//F*/
OPENCVAPI
CvLCMNode* _cvTreatExeptionalCase(CvLCM* pLCM,
                                  CvLCMData* pLCMInputData);

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvNodeMultyplicity
//    Purpose: Function seeks all non-boundary edges incident to
//              given node and correspondent incident sites
//    Context:
//    Parameters:
//      pEdge : in, original edge
//      pNode : in, given node
//      LinkedEdges : out, matrix of incident edges
//      LinkedSites : out, matrix of incident sites
//      pSite: in, original site (pNode must be the begin point of pEdge
//              for this pSite, this property hold out far all edges)  
//    Returns: number of incident edges (must be less than 10)
//F*/
OPENCVAPI
int _cvNodeMultyplicity(CvVoronoiSite2D* pSite,
                        CvVoronoiEdge2D* pEdge,
                        CvVoronoiNode2D* pNode,
                        CvVoronoiEdge2D** LinkedEdges,
                        CvVoronoiSite2D** LinkedSites);

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvCreateLCMNode
//    Purpose: Function create graph node
//    Context:
//    Parameters:
//      pLCM : in, information about graph
//    Returns: pointer to graph node
//F*/
OPENCVAPI
CvLCMNode* _cvCreateLCMNode(CvLCM* pLCM);

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvCreateLCMEdge
//    Purpose: Function create graph edge
//    Context:
//    Parameters:
//      pLCM : in, information about graph
//    Returns: pointer to graph edge
//F*/
OPENCVAPI
CvLCMEdge* _cvCreateLCMEdge(CvLCM* pLCM);

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvCreateLCMNode
//    Purpose: Function establishs the connection between node and ege
//    Context:
//    Parameters:
//      LCMNode : in, graph node
//      LCMEdge : in, graph edge
//      LCMEdge_prev : in&out, previous edge, connected with given node
//      index: in, 
//      i    : =0, if node is initial for edge
//             =1, if node  is terminal for edge
//    Returns: 
//F*/
OPENCVAPI
void _cvAttachLCMEdgeToLCMNode(CvLCMNode* LCMNode,
                               CvLCMEdge* LCMEdge,
                               CvLCMEdge* &LCMEdge_prev,
                               int index,
                               int i);
/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvProjectionPointToSegment
//    Purpose: Function computes the ortogonal projection of PointO to
//             to segment[PointA, PointB]
//    Context:
//    Parameters:
//      PointO, PointA,PointB: in, given points
//      PrPoint : out, projection
//      dist : distance from PointO to PrPoint
//    Returns: 
//F*/
OPENCVAPI
void _cvProjectionPointToSegment(CvPoint2D32f* PointO,
                                 CvPoint2D32f* PointA,
                                 CvPoint2D32f* PointB,
                                 CvPoint2D32f* PrPoint,
                                 float* dist);

/*F///////////////////////////////////////////////////////////////////////////////////////
//    Author:  Andrey Sobolev
//    Name:    _cvPrepareData
//    Purpose: Function fills up the struct CvLCMComplexNodeData
//    Context:
//    Parameters:
//      pLCMData : in
//      pLCMCCNData : out
//    Returns: 
//F*/
OPENCVAPI
void _cvPrepareData(CvLCMComplexNodeData* pLCMCCNData,
                    CvLCMData* pLCMData);

/****************************************************************************************\
*                                    Function realization                               *
\****************************************************************************************/

CV_IMPL CvGraph* cvLinearContorModelFromVoronoiDiagram(CvVoronoiDiagram2D* VoronoiDiagram,
                                                       float maxWidth)
{
    CvMemStorage* LCMstorage;
    CvSet* SiteSet;
    CvLCM LCM = {NULL, VoronoiDiagram,NULL,NULL,maxWidth};

    CV_FUNCNAME( "cvLinearContorModelFromVoronoiDiagram" );
     __BEGIN__;
    
    if( !VoronoiDiagram )
        CV_ERROR( CV_StsBadArg,"Voronoi Diagram is not defined" );
    if( maxWidth < 0 )
        CV_ERROR( CV_StsBadArg,"Treshold parameter must be non negative" );
    
    for(SiteSet = VoronoiDiagram->sites;
        SiteSet != NULL;
        SiteSet = (CvSet*)SiteSet->h_next)
        {
            if(SiteSet->v_next)
                CV_ERROR( CV_StsBadArg,"Can't operate with multiconnected domains" );
            if(SiteSet->total > 70000)
                CV_ERROR( CV_StsBadArg,"Can't operate with large domains" );
        }
            

    LCMstorage = cvCreateMemStorage(0);
    LCM.EdgeStorage = cvCreateChildMemStorage(LCMstorage);
    LCM.ContourStorage = cvCreateChildMemStorage(LCMstorage);
    LCM.Graph = cvCreateGraph(CV_SEQ_KIND_GRAPH|CV_GRAPH_FLAG_ORIENTED,
                              sizeof(CvGraph),
                              sizeof(CvLCMNode),
                              sizeof(CvLCMEdge),
                              LCMstorage);
    if(!_cvConstructLCM(&LCM))
        cvReleaseLinearContorModelStorage(&LCM.Graph);

    
    __END__;
    return LCM.Graph;
}//end of cvLinearContorModelFromVoronoiDiagram

CV_IMPL int cvReleaseLinearContorModelStorage(CvGraph** Graph)
{
    CvSeq* LCMNodeSeq, *LCMEdgeSeq;
    CvLCMNode* pLCMNode;
    CvLCMEdge* pLCMEdge;

    /*CV_FUNCNAME( "cvReleaseLinearContorModelStorage" );*/
     __BEGIN__;

    if(!Graph || !(*Graph))
        return 0;

    LCMNodeSeq = (CvSeq*)(*Graph);
    LCMEdgeSeq = (CvSeq*)(*Graph)->edges;
    if(LCMNodeSeq->total > 0)
    {
        pLCMNode = (CvLCMNode*)cvGetSeqElem(LCMNodeSeq,0,NULL);
        if(pLCMNode->contour->storage)
            cvReleaseMemStorage(&pLCMNode->contour->storage);
    }
    if(LCMEdgeSeq->total > 0)
    {
        pLCMEdge = (CvLCMEdge*)cvGetSeqElem(LCMEdgeSeq,0,NULL);
        if(pLCMEdge->chain->storage)
            cvReleaseMemStorage(&pLCMEdge->chain->storage);
    }
    if((*Graph)->storage)
        cvReleaseMemStorage(&(*Graph)->storage);
    *Graph = NULL;

    
    __END__;
    return 1;
}//end of cvReleaseLinearContorModelStorage

CV_IMPL int _cvConstructLCM(CvLCM* LCM)
{
    CvVoronoiSite2D* pSite = 0;
    CvVoronoiEdge2D* pEdge = 0, *pEdge1;
    CvVoronoiNode2D* pNode, *pNode1;
    
    CvVoronoiEdge2D* LinkedEdges[10];
    CvVoronoiSite2D* LinkedSites[10];
    
    CvSeqReader reader;
    CvLCMData LCMdata;
    int i;

    for(CvSet* SiteSet = LCM->VoronoiDiagram->sites;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?