[OpenCV 6] 그리기 함수

Sinaenjuni·2023년 7월 9일
0

OpenCV

목록 보기
6/25

공통 사항

선의 두께를 의미하는 thickness의 값을 -1로 하면 도형의 색을 채운다. 다만 polyline()의 경우 fillPoly() 함수를 이용해서 도형을 채워야 한다.

선의 종류(LINE_TYPE)는 다음과 같이 정의되어 있다.

enum LineTypes {
    FILLED  = -1,
    LINE_4  = 4, //!< 4-connected line
    LINE_8  = 8, //!< 8-connected line
    LINE_AA = 16 //!< antialiased line
};

직선

img: Image.
pt1: First point of the line segment.
pt2: Second point of the line segment.
color: Line color.
thickness: Line thickness.
lineType: Type of the line. See #LineTypes.
shift: Number of fractional bits in the point coordinates.
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);

화살표

img: Image.
pt1: The point the arrow starts from.
pt2: The point the arrow points to.
color: Line color.
thickness: Line thickness.
line_type: Type of the line. See #LineTypes
shift: Number of fractional bits in the point coordinates.
tipLength: The length of the arrow tip in relation to the arrow length
void arrowedLine(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int line_type=8, int shift=0, double tipLength=0.1);

마커

img: Image.
position: The point where the crosshair is positioned.
color: Line color.
markerType: The specific type of marker you want to use, see #MarkerTypes
thickness: Line thickness.
line_type: Type of the line, See #LineTypes
markerSize: The length of the marker axis [default = 20 pixels]
void drawMarker(InputOutputArray img, Point position, const Scalar& color, int markerType = MARKER_CROSS, int markerSize=20, int thickness=1, int line_type=8);
enum MarkerTypes
{
    MARKER_CROSS = 0,           //!< A crosshair marker shape
    MARKER_TILTED_CROSS = 1,    //!< A 45 degree tilted crosshair marker shape
    MARKER_STAR = 2,            //!< A star marker shape, combination of cross and tilted cross
    MARKER_DIAMOND = 3,         //!< A diamond marker shape
    MARKER_SQUARE = 4,          //!< A square marker shape
    MARKER_TRIANGLE_UP = 5,     //!< An upwards pointing triangle marker shape
    MARKER_TRIANGLE_DOWN = 6    //!< A downwards pointing triangle marker shape
};

사각형

img: Image.
pt1: Vertex of the rectangle.
pt2: Vertex of the rectangle opposite to pt1 .
color: Rectangle color or brightness (grayscale image).
thickness: Thickness of lines that make up the rectangle. Negative values, like #FILLED, mean that the function has to draw a filled rectangle.
lineType: Type of the line. See #LineTypes
shift: Number of fractional bits in the point coordinates.
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);

void rectangle(InputOutputArray img, Rect rec, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);

img: Image where the circle is drawn.
center: Center of the circle.
radius: Radius of the circle.
color: Circle color.
thickness: Thickness of the circle outline, if positive. Negative values, like #FILLED,
mean that a filled circle is to be drawn.
lineType: Type of the circle boundary. See #LineTypes
shift: Number of fractional bits in the coordinates of the center and in the radius value.
void circle(InputOutputArray img, Point center, int radius, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);

타원

img: Image.
center: Center of the ellipse.
axes: Half of the size of the ellipse main axes.
angle: Ellipse rotation angle in degrees.
startAngle: Starting angle of the elliptic arc in degrees.
endAngle: Ending angle of the elliptic arc in degrees.
color: Ellipse color.
thickness: Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that
a filled ellipse sector is to be drawn.
lineType: Type of the ellipse boundary. See #LineTypes
shift: Number of fractional bits in the coordinates of the center and values of axes.
void ellipse(InputOutputArray img, Point center, Size axes,
                        double angle, double startAngle, double endAngle,
                        const Scalar& color, int thickness = 1,
                        int lineType = LINE_8, int shift = 0);

다각형

img: Image.
pts: Array of polygonal curves.
isClosed: Flag indicating whether the drawn polylines are closed or not. If they are closed,
the function draws a line from the last vertex of each curve to its first vertex.
color: Polyline color.
thickness: Thickness of the polyline edges.
lineType: Type of the line segments. See #LineTypes
shift: Number of fractional bits in the vertex coordinates.
void polylines(InputOutputArray img, InputArrayOfArrays pts,
                            bool isClosed, const Scalar& color,
                            int thickness = 1, int lineType = LINE_8, int shift = 0 );
void polylines(InputOutputArray img, const Point* const* pts, const int* npts,
                          int ncontours, bool isClosed, const Scalar& color,
                          int thickness = 1, int lineType = LINE_8, int shift = 0 );

void fillPoly(InputOutputArray img, InputArrayOfArrays pts,
                           const Scalar& color, int lineType = LINE_8, int shift = 0,
                           Point offset = Point() );
void fillPoly(InputOutputArray img, const Point** pts,
                         const int* npts, int ncontours,
                         const Scalar& color, int lineType = LINE_8, int shift = 0,
                         Point offset = Point() );

문자열

문자열을 프레임에 그려주는 함수이다. 한글이나 한자와 같은 유니코드 문자는 출력할 수 없다. 그리고 문자열의 위치를 나타내는 점의 기준은 좌측 하단이 기준점이 된다.

void putText( InputOutputArray img, const String& text, Point org,
                         int fontFace, double fontScale, Scalar color,
                         int thickness = 1, int lineType = LINE_8,
                         bool bottomLeftOrigin = false );
enum HersheyFonts {
    FONT_HERSHEY_SIMPLEX        = 0, 
    //!< normal size sans-serif font
    FONT_HERSHEY_PLAIN          = 1, 
    //!< small size sans-serif font
    FONT_HERSHEY_DUPLEX         = 2, 
    //!< normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX)
    FONT_HERSHEY_COMPLEX        = 3, 
    //!< normal size serif font
    FONT_HERSHEY_TRIPLEX        = 4, 
    //!< normal size serif font (more complex than FONT_HERSHEY_COMPLEX)
    FONT_HERSHEY_COMPLEX_SMALL  = 5, 
    //!< smaller version of FONT_HERSHEY_COMPLEX
    FONT_HERSHEY_SCRIPT_SIMPLEX = 6, 
    //!< hand-writing style font
    FONT_HERSHEY_SCRIPT_COMPLEX = 7, 
    //!< more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEX
    FONT_ITALIC                 = 16 
    //!< flag for italic font
};

Example code

void ex_figure(){
    Mat frame(1000,1000, CV_32FC3, Scalar(0,0,0,0));

    Point point1(100,100), point2(200,100);

    line(frame, point1, point2, Scalar(255,0,0), 10, LINE_AA);
    arrowedLine(frame, point1 + Point(0, 100), point2 + Point(0, 100), Scalar(255,0,0), 10, LINE_AA);
    drawMarker(frame, point1 + Point(0, 200), Scalar(255,0,0));

    rectangle(frame, point1 + Point(0, 300), point2 + Point(0, 500), Scalar(0,255,0), LINE_AA);
    circle(frame, point1 + Point(0, 600), 10, Scalar(0, 255, 0), 1, LINE_AA);
    ellipse(frame, RotatedRect(point1 + Point(100, 700), Size(200, 100), 0), Scalar(0, 255, 0), 1, LINE_AA);

    polylines(frame, Vec<Point, 4>(point1+Point(200,0), 
                                    point1+Point(200,100),
                                    point2+Point(200,100),
                                    point2+Point(200,0)
                                    ), false, Scalar(0, 255, 0));
    fillPoly(frame, Vec<Point, 4>(point1+Point(400,0), 
                                    point1+Point(400,100),
                                    point2+Point(400,100),
                                    point2+Point(400,0)
                                    ), Scalar(0, 255, 0), LINE_AA);

    circle(frame, Point(500,500), 5, Scalar(0,0,255), -1, LINE_AA); // 텍스트의 기준점은 좌측 하단이다.
    putText(frame, "OpenCV", Point(500, 500), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 255), 2, LINE_AA);

    imshow("frame", frame);
    waitKey();
    destroyAllWindows();

}

0개의 댓글