cvlee.cpp.svn-base

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

SVN-BASE
1,445
字号
/*--------------------------------------------------------------------------
    Author      : Andrey Sobolev
    Description : Function computes the image of point under 
                    given affin map
    Arguments 
            A   : in, affine maps
        pPoint  : in, pointer to point
        pImgPoint:out, pointer to image of point
      Return    : 
    --------------------------------------------------------------------------*/
template<class T> CV_INLINE
void _cvCalcPointImage(pCvPointFloat pImgPoint,pCvPointFloat pPoint,T* A);

/*--------------------------------------------------------------------------
    Author      : Andrey Sobolev
    Description : Function computes the image of vector under 
                    given affin map
    Arguments 
            A   : in, affine maps
        pVector : in, pointer to vector
        pImgVector:out, pointer to image of vector
      Return    : 
    --------------------------------------------------------------------------*/
template<class T> CV_INLINE
void _cvCalcVectorImage(pCvDirection pImgVector,pCvDirection pVector,T* A);

/*--------------------------------------------------------------------------
    Author      : Andrey Sobolev
    Description : Function computes the distance between the point 
                    and site. Internal function.
    Arguments 
        pPoint  : in, point
        pSite   : in, site
      Return    : distance
    --------------------------------------------------------------------------*/
OPENCVAPI CV_INLINE
float _cvCalcDist(pCvPointFloat pPoint, pCvVoronoiSite pSite);

/*--------------------------------------------------------------------------
    Author      : Andrey Sobolev
    Description : Function computes the distance between two points 
    Arguments 
    pPoint1,pPoint2 : in, two points
      Return    : distance
    --------------------------------------------------------------------------*/
OPENCVAPI CV_INLINE
float _cvPPDist(pCvPointFloat pPoint1,pCvPointFloat pPoint2);

/*--------------------------------------------------------------------------
    Author      : Andrey Sobolev
    Description : Function computes the distance betwin point and 
                    segment. Internal function. 
    Arguments 
          pPoint: in, point
    pPoint1,pPoint2 : in, segment [pPoint1,pPoint2]
       Return   : distance
    --------------------------------------------------------------------------*/
OPENCVAPI CV_INLINE
float _cvPLDist(pCvPointFloat pPoint,pCvPointFloat pPoint1,pCvDirection pDirection);

/*--------------------------------------------------------------------------
    Author      : Andrey Sobolev
    Description : Function solves the squar equation with real coefficients
                    T - float or double
    Arguments 
     c2,c1,c0: in, real coefficients of polynom 
               X: out, array of roots
     Return     : number of roots
    --------------------------------------------------------------------------*/
template <class T> 
int _cvSolveEqu2thR(T c2, T c1, T c0, T* X);

/*--------------------------------------------------------------------------
    Author      : Andrey Sobolev
    Description : Function solves the linear equation with real or complex coefficients
                    T - float or double or complex
    Arguments 
        c1,c0: in, real or complex coefficients of polynom 
               X: out, array of roots
     Return     : number of roots
    --------------------------------------------------------------------------*/
template <class T> CV_INLINE
int _cvSolveEqu1th(T c1, T c0, T* X);

/****************************************************************************************\
*                             Storage Block Increase                                    *
\****************************************************************************************/
/*--------------------------------------------------------------------------
    Author      : Andrey Sobolev
    Description : For each sequence function creates the memory block sufficient to store
                  all elements of sequnce
    Arguments 
        pVoronoiDiagramInt: in, pointer to struct, which contains the 
                            description of Voronoi Diagram.
        vertices_number: in, number of vertices in polygon
     Return     : 
    --------------------------------------------------------------------------*/
void _cvSetSeqBlockSize(CvVoronoiDiagramInt* pVoronoiDiagramInt,int vertices_number)
{
    int N = 2*vertices_number;
    cvSetSeqBlockSize(pVoronoiDiagramInt->SiteSeq,N*pVoronoiDiagramInt->SiteSeq->elem_size);
    cvSetSeqBlockSize(pVoronoiDiagramInt->EdgeSeq,3*N*pVoronoiDiagramInt->EdgeSeq->elem_size);
    cvSetSeqBlockSize(pVoronoiDiagramInt->NodeSeq,5*N*pVoronoiDiagramInt->NodeSeq->elem_size);
    cvSetSeqBlockSize(pVoronoiDiagramInt->ParabolaSeq,N*pVoronoiDiagramInt->ParabolaSeq->elem_size);
    cvSetSeqBlockSize(pVoronoiDiagramInt->DirectionSeq,3*N*pVoronoiDiagramInt->DirectionSeq->elem_size);
    cvSetSeqBlockSize(pVoronoiDiagramInt->ChainSeq,N*pVoronoiDiagramInt->DirectionSeq->elem_size);
    cvSetSeqBlockSize(pVoronoiDiagramInt->HoleSeq,100*pVoronoiDiagramInt->HoleSeq->elem_size);
}

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


CV_IMPL int   cvVoronoiDiagramFromContour(CvSeq* ContourSeq,    
                                           CvVoronoiDiagram2D** VoronoiDiagram,
                                           CvMemStorage* VoronoiStorage,
                                           CvLeeParameters contour_type,
                                           int contour_orientation,
                                           int attempt_number)
{
    CV_FUNCNAME( "CvVoronoiDiagramFromContour" );
    __BEGIN__;

    CvSet* SiteSeq = NULL;
    CvSeq* CurContourSeq = NULL;
    CvVoronoiDiagramInt VoronoiDiagramInt;
    CvSeqWriter NodeWriter, EdgeWriter;
    CvMemStorage* storage;

    if( !ContourSeq )
        CV_ERROR( CV_StsBadArg,"Contour sequence is empty" );

    if(!VoronoiStorage)
        CV_ERROR( CV_StsBadArg,"Storage is not initialized" );
   
    if( contour_type < 0 || contour_type > 2)
        CV_ERROR( CV_StsBadArg,"Unsupported parameter: type" );

    if( contour_orientation != 1 &&  contour_orientation != -1)
        CV_ERROR( CV_StsBadArg,"Unsupported parameter: orientation" );

    storage = cvCreateChildMemStorage(VoronoiStorage);
    (*VoronoiDiagram) = (CvVoronoiDiagram2D*)cvCreateSet(0,sizeof(CvVoronoiDiagram2D),sizeof(CvVoronoiNode2D), storage);
    storage = cvCreateChildMemStorage(VoronoiStorage);
    (*VoronoiDiagram)->edges = cvCreateSet(0,sizeof(CvSet),sizeof(CvVoronoiEdge2D), storage);
    cvStartAppendToSeq((CvSeq*)(*VoronoiDiagram)->edges, &EdgeWriter);
    cvStartAppendToSeq((CvSeq*)(*VoronoiDiagram), &NodeWriter);

        for(CurContourSeq = ContourSeq;\
            CurContourSeq != NULL;\
            CurContourSeq = CurContourSeq->h_next)
        {
            if(_cvLee(CurContourSeq, &VoronoiDiagramInt,VoronoiStorage,contour_type,contour_orientation,attempt_number))
    
            {
                if(!_cvConvert(*VoronoiDiagram,VoronoiDiagramInt,SiteSeq,NodeWriter,EdgeWriter,VoronoiStorage,contour_orientation))
                    goto exit;
            }
            else if(CurContourSeq->total >= 3)
                goto exit;
        }

        cvEndWriteSeq(&EdgeWriter);
        cvEndWriteSeq(&NodeWriter);
        if(SiteSeq != NULL)
            return 1;

    
    __END__;
    return 0;
}//end of cvVoronoiDiagramFromContour 

CV_IMPL int   cvVoronoiDiagramFromImage(IplImage* pImage,
                                         CvSeq** ContourSeq,
                                         CvVoronoiDiagram2D** VoronoiDiagram,
                                         CvMemStorage* VoronoiStorage,
                                         CvLeeParameters regularization_method,
                                         float approx_precision)
{
    CV_FUNCNAME( "cvVoronoiDiagramFromContour" );
    int RESULT = 0;
    
    __BEGIN__;

    IplImage* pWorkImage = NULL;
    CvSize image_size;
    int i, multiplicator = 3;
    
    CvChainApproxMethod approx_method;
    CvMemStorage* ApproxContourStorage = NULL;
    CvSeq* ApproxContourSeq = NULL;

    if( !ContourSeq )
        CV_ERROR( CV_StsBadArg,"Contour sequence is not initialized" );

    if( (*ContourSeq)->total != 0)
        CV_ERROR( CV_StsBadArg,"Contour sequence is not empty" );

    if(!VoronoiStorage)
        CV_ERROR( CV_StsBadArg,"Storage is not initialized" );

    if(!pImage)
        CV_ERROR( CV_StsBadArg,"Image is not initialized" );

    if(pImage->nChannels != 1 || pImage->depth != 8)
        CV_ERROR( CV_StsBadArg,"Unsupported image format" );

    if(approx_precision<0 && approx_precision != CV_LEE_AUTO)
        CV_ERROR( CV_StsBadArg,"Unsupported presision value" );
    
    switch(regularization_method)
    {
        case CV_LEE_ERODE:  image_size.width = pImage->width;
                            image_size.height = pImage->height;
                            pWorkImage = cvCreateImage(image_size,8,1);
                            cvErode(pImage,pWorkImage,0,1);
                            approx_method = CV_CHAIN_APPROX_TC89_L1;
                            break;
        case CV_LEE_ZOOM:   image_size.width = multiplicator*pImage->width;
                            image_size.height = multiplicator*pImage->height;
                            pWorkImage = cvCreateImage(image_size,8,1);
                            cvResize(pImage, pWorkImage, CV_INTER_NN);
                            approx_method = CV_CHAIN_APPROX_TC89_L1;
                            break;
        case CV_LEE_NON:    pWorkImage = pImage;
                            approx_method = CV_CHAIN_APPROX_TC89_L1;
                            break;
        default:            CV_ERROR( CV_StsBadArg,"Unsupported regularisation method" );
                            break;

    }

    cvFindContours(pWorkImage, (*ContourSeq)->storage, ContourSeq, \
                            sizeof(CvContour), CV_RETR_CCOMP, approx_method );

    if(pWorkImage && pWorkImage != pImage )
        cvReleaseImage(&pWorkImage);
    
    ApproxContourStorage = cvCreateMemStorage(0);
    if(approx_precision > 0)
    {
        ApproxContourSeq = cvApproxPoly(*ContourSeq, sizeof(CvContour), ApproxContourStorage,\
                                        CV_POLY_APPROX_DP,approx_precision,1);
    
        RESULT = cvVoronoiDiagramFromContour(ApproxContourSeq,VoronoiDiagram,VoronoiStorage,CV_LEE_INT,-1,10);
    }
    else if(approx_precision == CV_LEE_AUTO)
    {
        ApproxContourSeq = *ContourSeq;
        for(i = 1; i < 50; i++)
        {
            RESULT = cvVoronoiDiagramFromContour(ApproxContourSeq,VoronoiDiagram,VoronoiStorage,CV_LEE_INT,-1,1);
            if(RESULT)
                break;
            ApproxContourSeq = cvApproxPoly(ApproxContourSeq, sizeof(CvContour),ApproxContourStorage,\
                                            CV_POLY_APPROX_DP,(float)i,1);
        }
    }
    else
        RESULT = cvVoronoiDiagramFromContour(*ContourSeq,VoronoiDiagram,VoronoiStorage,CV_LEE_INT,-1,10);
/*
    if(ApproxContourSeq)
    {
        cvClearMemStorage( (*ContourSeq)->storage );
        *ContourSeq = cvCreateSeq(0,sizeof(CvSeq),sizeof(CvPoint),(*ContourSeq)->storage);
        CvSeqReader reader;
        CvSeqWriter writer;
        CvPoint Point;
        cvStartAppendToSeq(*ContourSeq, &writer);
        cvStartReadSeq(ApproxContourSeq, &reader);
        for(int i = 0;i < ApproxContourSeq->total;i++)
        {
            CV_READ_SEQ_ELEM(Point,reader);
            Point.y = 600 - Point.y;
            CV_WRITE_SEQ_ELEM(Point,writer);
        }
        cvEndWriteSeq(&writer);
    }
    */

    cvReleaseMemStorage(&ApproxContourStorage);
    
    
    __END__;
    return RESULT;                                         
}//end of cvVoronoiDiagramFromImage

CV_IMPL void cvR

⌨️ 快捷键说明

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