기본 콘텐츠로 건너뛰기

[OpenCV]cvDrawContours

cvDrawContours 함수는 contour를 그려주는 함수이며 thickness 0 이상일 경우에는 외곽선만을 그려주고 0 미만일 경우에는 내부를 채워준다.

 

원형

void cvDrawContours( CvArr *img, CvSeq* contour,
                                CvScalar external_color, CvScalar hole_color,
                                int max_level, int thickness=1,

                                int line_type=8, CvPoint offset=cvPoint(0,0) );
 

패러미터

  • img
    • contour를 그릴 IplImage 구조체의 포인터를 넘겨준다. 다른 그리기 함수와 마찬가지로 contour ROI로 클리핑된다.
  • contour
    • CvSeq 구조체의 포인터를 넘겨준다
  • external_color
    • 외부 contour의 색상을 설정한다.
  • hole_color
    • 내부 hole의 색상을 설정한다.
  • max_level
    • 그려질 contour의 최대 단계를 설정한다. 만약 0이면 contour만이 그려질 것이고 1이면 모든 contour를 같은 레벨에 그려준다. 2이면 다음 레벨까지 그려줄 것이다. 만약 음수이면 max_level 값에 절대값을 취한 것에서 1을 뺀 만큼의 자식 contour를 그려준다.
  • thickness
    • 라인의 두께를 설정한다. 만약 음수(CV_FILLED)이면 내부를 채운다.
  • line_type
    • 라인의 그리기 방식을 설정한다.
      • 8 - 8방향으로 연결된 라인으로 그린다.
      • 4 - 4방향으로 연결된 라인으로 그린다.
      • CV_AA - 안티앨리어싱을 적용한다.
  • offset
    • 지정된 값만큼 모든 좌표를 이동한다.

예제

#include "cv.h"

#include "highgui.h"

 

int main( int argc, char** argv )

{

    IplImage* src;

    // the first command line parameter must be file name of binary (black-n-white) image

    if( argc == 2 && (src=cvLoadImage(argv[1], 0))!= 0)

    {

        IplImage* dst = cvCreateImage( cvGetSize(src), 8, 3 );

        CvMemStorage* storage = cvCreateMemStorage(0);

        CvSeq* contour = 0;

 

        cvThreshold( src, src, 1, 255, CV_THRESH_BINARY );

        cvNamedWindow( "Source", 1 );

        cvShowImage( "Source", src );

 

        cvFindContours( src, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

        cvZero( dst );

 

        for( ; contour != 0; contour = contour->h_next )

        {

            CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );

            /* replace CV_FILLED with 1 to see the outlines */

            cvDrawContours( dst, contour, color, color, -1, CV_FILLED, 8 );

        }

 

        cvNamedWindow( "Components", 1 );

        cvShowImage( "Components", dst );

        cvWaitKey(0);

    }

}

 

댓글