cvdrawing.cpp.svn-base

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

SVN-BASE
2,174
字号
    if( npts <= 0 )
        CV_ERROR( CV_StsNullPtr, "" );

    CV_CALL( icvExtractColor( color, mat->type, buf ));

    {
        CvContour* edges = 0;
        CvSeq vtx;
        CvSeqBlock block;

        CV_CALL( st = cvCreateMemStorage( (1<<12) - 128 ));
        CV_CALL( edges = (CvContour*)cvCreateSeq( 0, sizeof(CvContour),
                                                  sizeof(CvPolyEdge), st ));

        for( int i = 0; i < contours; i++ )
        {
            if( !pts[i] )
                CV_ERROR_FROM_STATUS( CV_NULLPTR_ERR );

            if( npts[i] < 0 )
                CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );
            
            cvMakeSeqHeaderForArray( 0, sizeof(CvSeq), sizeof(CvPoint),
                                     pts[i], npts[i], &vtx, &block );

            CV_CALL( icvCollectPolyEdges( mat, &vtx, edges, buf ));
        }

        CV_CALL( icvFillEdgeCollection( mat, edges, buf ));
    }

    __END__;

    cvReleaseMemStorage( &st );
}



CV_IMPL void
cvPolyLine( void *img, CvPoint **pts, int *npts,
            int contours, int closed, double color,
            int thickness, int connectivity )
{
    CV_FUNCNAME( "cvPolyLine" );

    __BEGIN__;

    int coi = 0, i;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

    CV_CALL( mat = cvGetMat( mat, &stub, &coi ));

    if( coi != 0 )
        CV_ERROR( CV_BadCOI, icvUnsupportedFormat );

    if( contours <= 0 )
        CV_ERROR( CV_StsBadArg, "" );

    if( thickness < -1 || thickness > 255 )
        CV_ERROR( CV_StsBadArg, "" );

    if( !pts )
        CV_ERROR( CV_StsNullPtr, "" );

    if( npts <= 0 )
        CV_ERROR( CV_StsNullPtr, "" );

    CV_CALL( icvExtractColor( color, mat->type, buf ));

    for( i = 0; i < contours; i++ )
    {
        CV_CALL( icvPolyLine( mat, pts[i], npts[i], closed, thickness,
                              buf, connectivity ));
    }

    __END__;
}


CV_IMPL void
cvPolyLineAA( void * img, CvPoint **pts, int *npts,
              int contours, int closed, double color, int scale )
{
    CV_FUNCNAME( "cvPolyLineAA" );

    __BEGIN__;

    int coi = 0, i;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

    CV_CALL( mat = cvGetMat( mat, &stub, &coi ));

    if( coi != 0 )
        CV_ERROR( CV_BadCOI, icvUnsupportedFormat );

    if( contours <= 0 )
        CV_ERROR( CV_StsBadArg, "" );

    if( scale < 0 || scale > XY_SHIFT )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    if( !pts )
        CV_ERROR( CV_StsNullPtr, "" );

    if( npts <= 0 )
        CV_ERROR( CV_StsNullPtr, "" );

    CV_CALL( icvExtractColor( color, mat->type, buf ));

    for( i = 0; i < contours; i++ )
    {
        CV_CALL( icvPolyLineAA( mat, pts[i], npts[i], closed, scale, buf ));
    }

    __END__;
}


CV_IMPL void
cvPutText( void *img, const char *text, CvPoint org, CvFont *font, double color )
{
    CV_FUNCNAME( "cvPutText" );

    __BEGIN__;

    int coi = 0;
    int top_bottom = 0;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

    const int shift = CV_TXT_FONT_SHIFT + CV_TXT_SIZE_SHIFT;
    const int delta = 1 << (shift - 1);

    int i, font_height, max_code;
    const short *faces;

    CV_CALL( mat = cvGetMat( mat, &stub, &coi ));

    if( coi != 0 )
        CV_ERROR( CV_BadCOI, icvUnsupportedFormat );

    if( CV_IS_IMAGE_HDR(img) && ((IplImage*)img)->origin )
        top_bottom = 1;

    if( !text || !font || !font->data )
        CV_ERROR( CV_StsNullPtr, "" );

    if( !font->data[2] && !font->data[3] )
        CV_ERROR( CV_StsNullPtr, "" );

    CV_CALL( icvExtractColor( color, mat->type, buf ));

    faces = (const short *)(((ulong)font->data[3] << sizeof(int)*8*
                             (sizeof(long)>sizeof(int))) + (ulong)font->data[2]);
    max_code = font->data[4];

    font_height = font->size.height;

    if( top_bottom )
    {
        org.y -= (font->data[1] * font_height + delta) >> shift;
        font_height = -font_height;
    }

    for( i = 0; text[i] != '\0'; i++ )
    {
        int idx = ((uchar)text[i]) * 4;
        int w;
        const short *letter;
        CvPoint pt0 = org;

        if( idx >= max_code * 4 || font->data[idx + CV_FONT_HDR_SIZE] < 0 )
            idx = 127 * 4;
        idx += CV_FONT_HDR_SIZE;
        letter = faces + font->data[idx];

        w = (font->data[idx + 1] * font->size.width + delta) >> shift;

        for( ;; )
        {
            CvPoint pt;
            int new_fl, end_fl;

            pt.x = letter[0];
            pt.y = letter[1];
            new_fl = (pt.x & 1);
            end_fl = (pt.y & 1);
            letter += 2;

            if( pt.x == SHRT_MIN )
                break;

            pt.x = org.x + ((pt.x * font->size.width +
                             pt.y * font->italic_scale + delta) >> shift);
            pt.y = org.y - ((pt.y * font_height + delta) >> shift);

            if( !new_fl )
            {
                icvThickLine( mat, pt0, pt, font->thickness, buf );
            }
            pt0 = pt;
            if( end_fl )
                break;
        }

        org.x += w + font->dx;
    }

    __END__;
}


CV_IMPL void
cvInitFont( CvFont *font, CvFontFace font_face,
            double hscale, double vscale, double italic_scale, int thickness )
{
    CV_FUNCNAME( "cvInitFont" );

    __BEGIN__;

    const int shift = CV_TXT_FONT_SHIFT + CV_TXT_SIZE_SHIFT;
    const int delta = 1 << (shift - 1);

    if( !font )
        CV_ERROR_FROM_STATUS( CV_NULLPTR_ERR );
    if( font_face != CV_FONT_VECTOR0 || hscale <= 0 || vscale <= 0 || thickness < 0 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    font->data = icvTextHdrsFn0;
    font->size.width = cvRound( hscale * (1 << CV_TXT_SIZE_SHIFT) );
    font->size.height = cvRound( vscale * (1 << CV_TXT_SIZE_SHIFT) );
    font->italic_scale = cvRound( italic_scale * vscale * (1 << CV_TXT_SIZE_SHIFT) );
    font->thickness = thickness;
    font->dx = ((font->size.width * font->data[0] + delta * 4) >>
                (shift + 2)) + font->thickness;

    __END__;
}


CV_IMPL void
cvGetTextSize( const char *text_string, CvFont *font, CvSize *text_size, int *_ymin )
{
    const int shift = CV_TXT_FONT_SHIFT + CV_TXT_SIZE_SHIFT;
    const int delta = 1 << (shift - 1);

    int i, max_code, width = 0, height = 0, ymin = 0;

    CV_FUNCNAME( "cvGetTextSize" );

    __BEGIN__;

    if( !font || !font->data || !text_string || !text_size || !_ymin )
        CV_ERROR_FROM_STATUS( CV_NULLPTR_ERR );

    max_code = font->data[4];

    for( i = 0; text_string[i] != '\0'; i++ )
    {
        int idx = ((uchar) text_string[i]) * 4;
        int yplus, yminus;

        if( idx >= max_code * 4 || font->data[idx + CV_FONT_HDR_SIZE] < 0 )
            idx = 127 * 4;
        idx += CV_FONT_HDR_SIZE;

        width += font->dx + ((font->data[idx + 1] * font->size.width + delta) >> shift);
        yplus = (font->data[idx + 2] * font->size.height + delta) >> shift;
        yminus = (font->data[idx + 3] * font->size.height + delta) >> shift;

        height = CV_IMAX( height, yplus );
        ymin = CV_IMIN( ymin, yminus );
    }

    text_size->width = width;
    text_size->height = height;
    *_ymin = ymin;

    __END__;
}



/*F///////////////////////////////////////////////////////////////////////////////////////
//    Name: cvDrawContours
//    Purpose:
//      Draws one or more contours on the image
//    Context:
//    Parameters:
//      img      - destination three-channel image
//      step     - its full width in bytes
//      size     - width and height of the image in pixels
//      contour  - pointer to drawn contour(s).
//      external_color - color to draw external contours with
//      hole_color - color to draw hole contours with
//      max_level  - max level of the tree (starting from contour pointer) to draw.
//                   if it is 0, draw single contour, if 1 - draw the contour and
//                   other contours at the same level, 2 - draw two levels etc.
//    Returns:
//      CV_OK or error code
//    Notes:
//F*/
CV_IMPL void
cvDrawContours( void*  img,  CvSeq*  contour,
                double externalColor, double holeColor, 
                int  maxLevel, int thickness, int connectivity )
{
    CvSeq *contour0 = contour, *h_next = 0;
    CvMemStorage* st = 0;
    CvSeq* tseq = 0;
    CvContour* edges = 0;
    CvSeqWriter writer;
    CvTreeNodeIterator iterator;

    CV_FUNCNAME( "cvDrawContours" );

    __BEGIN__;

    int coi = 0;
    CvMat stub, *mat = (CvMat*)img;
    double ext_buf[4], hole_buf[4];

    CV_CALL( mat = cvGetMat( mat, &stub, &coi ));

    if( !contour )
        EXIT;

    if( coi != 0 )
        CV_ERROR( CV_BadCOI, icvUnsupportedFormat );

    if( thickness < -1 || thickness > 255 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    CV_CALL( icvExtractColor( externalColor, mat->type, ext_buf ));
    CV_CALL( icvExtractColor( holeColor, mat->type, hole_buf ));

    if( maxLevel < 0 )
    {
        h_next = contour->h_next;
        contour->h_next = 0;
        maxLevel = -maxLevel+1;
    }

    if( thickness < 0 )
    {
        if( contour->storage )
            st = cvCreateChildMemStorage( contour->storage );
        else
            st = cvCreateMemStorage();
        tseq = cvCreateSeq( 0, sizeof(CvContour), sizeof(CvPoint), st );
        edges = (CvContour*)cvCreateSeq( 0, sizeof(CvContour), sizeof(CvPolyEdge), st );
    }

    memset( &writer, 0, sizeof(writer));

    cvInitTreeNodeIterator( &iterator, contour, maxLevel );
    
    while( (contour = (CvSeq*)cvNextTreeNode( &iterator )) != 0 )
    {
        CvSeqReader reader;
        int i, count = contour->total;
        void* clr = (contour->flags & CV_SEQ_FLAG_HOLE) == 0 ? ext_buf : hole_buf;

        cvStartReadSeq( contour, &reader, 0 );

        if( CV_IS_SEQ_CHAIN_CONTOUR( contour ))
        {
            CvPoint pt = ((CvChain*)contour)->origin;
            CvPoint prev_pt = pt;
            char prev_code = reader.ptr ? reader.ptr[0] : '\0';

            if( thickness < 0 )
            {
                cvClearSeq( tseq );
                cvStartAppendToSeq( tseq, &writer );
                CV_WRITE_SEQ_ELEM( pt, writer );
            }

            for( i = 0; i < count; i++ )
            {
                char code;
                CV_READ_SEQ_ELEM( code, reader );

                assert( (code & ~7) == 0 );

                if( code != prev_code )
                {
                    prev_code = code;
                    if( thickness >= 0 )
                    {
                        icvThickLine( mat, prev_pt, pt, thickness, clr, connectivity );
                    }
                    else
                    {
                        CV_WRITE_SEQ_ELEM( pt, writer );
                    }
                    prev_pt = pt;
                }
            
                pt.x += icvCodeDeltas[code].x;
                pt.y += icvCodeDeltas[code].y;
            }

            if( thickness >= 0 )
            {
                icvThickLine( mat, prev_pt, ((CvChain*)contour)->origin,
                              thickness, clr, connectivity );
            }
            else
            {
                CV_WRITE_SEQ_ELEM( pt, writer );
                cvEndWriteSeq( &writer );
                CV_CALL( icvCollectPolyEdges( mat, tseq, edges, ext_buf ));
            }
        }
        else if( CV_IS_SEQ_POLYLINE( contour ))
        {
            if( thickness >= 0 )
            {
                CvPoint pt1, pt2;
                CV_ADJUST_EDGE_COUNT( count, contour );

                /* scroll the reader by 1 point */
                CV_READ_EDGE( pt1, pt2, reader );

                for( i = 0; i < count; i++ )
                {
                    CV_READ_EDGE( pt1, pt2, reader );

                    /*assert( pt1.x != 0 && pt1.y != 0 && pt2.x != 0 && pt2.y != 0 );*/

                    icvThickLine( mat, pt1

⌨️ 快捷键说明

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