OpenCV drawing related operations C++

drawing related knowledge

lineType line style introduction
The line style of opencv is described by enumeration values:

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

FILLED = -1 Non-line style, for drawing closed images, set the thickness parameter to -1, which is color filling.
LINE_4 = 4 Lines processed by the Bresenham algorithm based on 4-neighborhood connections.
Effect:
insert image description here
LINE_8 = 8 Lines processed by the Bresenham algorithm based on 4-neighborhood connections.
Effect:
insert image description here
LINE_AA = 16 Gaussian filter smoothing based on the straight line.
Effect:
insert image description here

plot the correlation function

draw circle
Function prototype:

void circle(InputOutputArray img, Point center, int radius,
                       const Scalar& color, int thickness = 1,
                       int lineType = LINE_8, int shift = 0);

imgis the input image; centeris the center of the drawn circle; radiusis the radius of the drawn circle; coloris the drawn color; thicknessis the drawn line width; lineTypeis the drawn line style; shiftoffset.

draw an ellipse
Function prototype 1:

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);

imgis the input image; coloris the color to be drawn; thicknessis the line width to be drawn; lineTypeis the line style shiftto be drawn;
insert image description here

Function prototype 2:

void ellipse(InputOutputArray img, const RotatedRect& box, const Scalar& color,
                        int thickness = 1, int lineType = LINE_8);

imgis the input image; coloris the color to be drawn; thicknessis the line width to be drawn; lineTypeis the line style shiftto be drawn;
insert image description here
draw straight line
Function prototype:

void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
                     int thickness = 1, int lineType = LINE_8, int shift = 0);

imgis the input image; pt1is one end point of the line; pt2is the other end point of the line; coloris the drawn color; thicknessis the drawn line width; lineTypeis the drawn line style; shiftthe offset.
insert image description here
Draw multiple polylines
Function prototype 1:

void polylines(InputOutputArray img, InputArrayOfArrays pts,
                            bool isClosed, const Scalar& color,
                            int thickness = 1, int lineType = LINE_8, int shift = 0 );

Function prototype 2:

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 );

These two functions are only different in function style, and only function 1 is considered here.
imgis the input image; ptsis the input point set; isClosedthe flag indicates whether the point set is closed; coloris the color to draw; thicknessis the line width to draw; lineTypeis the line style to draw; shiftoffset.
insert image description here
draw rectangle
Function prototype 1:

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

Function prototype 2:

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

These two functions are only different in the way of describing the rectangle, only function 1 is considered here.
imgis the input image; pt1is the upper-left point of the rectangle; pt2is the lower-right point of the rectangle; coloris the color to be drawn; thicknessis the line width to be drawn; lineTypeis the line style to be drawn; shiftoffset.
insert image description here
Draw a filled simple polygon
Function prototype 1:

void fillConvexPoly(InputOutputArray img, InputArray points,
                                 const Scalar& color, int lineType = LINE_8,
                                 int shift = 0);

Function prototype 2:

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

These two functions are only different in function style, and only function 1 is considered here.
imgis the input image; pointsis the input point set; coloris the color to draw; thicknessis the line width to draw; lineTypeis the line style to draw; shiftoffset.
insert image description here
draw text
Function prototype:

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 );

imgis the input image; is textthe text to be drawn; is orgthe reference point of the drawn text; fontFaceis the text style of the drawn text ; is fontScalethe zoom factor of the drawn text ; is the color drawn; The font style fontFace description is a bunch of enumeration values:colorthicknesslineTypebottomLeftOrigin

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
};

List of effects:
insert image description here
BottomLeftOrigin flag description
If the value is true, the point specified by org is the upper left corner of the inserted text.
If the value is false, the point specified by org is the lower left corner of the inserted text.
insert image description here

Get the size of the text area getTextSize()
Use this function to help determine that the drawn text is within the image display area.
Function prototype:

Size getTextSize(const String& text, int fontFace,
                            double fontScale, int thickness,
                            CV_OUT int* baseLine);

example:

#include <iostream>
#include <vector>
#include <opencv2\opencv.hpp>

using namespace std;
using namespace cv;

int main() {
    
    
	Mat src = Mat::zeros(Size(400, 400), CV_8UC3);

	string text = "hello";
	int fontFace = FONT_HERSHEY_SIMPLEX;
	double fontScale = 1.0;
	int thickness = 2;
	Point org(50, 100);
	int lineType = LINE_8;
	bool bottomLeftOrigin = true;

	putText(src, text, org, fontFace, fontScale, Scalar(0, 255, 0), thickness, lineType, bottomLeftOrigin);

	int baseLine;
	Size size = getTextSize(text, fontFace, fontScale, thickness,&baseLine);

	cout << "baseLine = " << baseLine << endl;
	cout << size << endl;

	imshow("src", src);
	waitKey();

	return 0;
}

输出:
baseLine = 10
[74 x 22]

draw arrow line segment
Function prototype:

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);

imgis the input image; pt1is the starting point of the arrow line segment; pt2is the end point of the arrow line segment; coloris the drawn color; thicknessis the drawn line width; line_typeis the drawn line style; shiftis the offset; tipLengthis the length ratio of the arrow length to the entire arrow line segment.

draw various markers
Function prototype:

void drawMarker(InputOutputArray img, Point position, const Scalar& color,
                             int markerType = MARKER_CROSS, int markerSize=20, int thickness=1,
                             int line_type=8);

imgis the input image; positionis the origin of the marker; coloris the color to draw; markerTypeis the kind of marker; markerSizeis the size of the marker; thicknessis the width of the drawn line; line_typeis the style of the drawn line.

Description of mark types :
There are the following types:

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
};

example:

#include <opencv2/opencv.hpp>

int main() {
    
    
	cv::Mat img = cv::Mat::zeros(240, 240, CV_8UC3);

	cv::drawMarker(img, cv::Point(30, 30), cv::Scalar(0, 255, 0), cv::MarkerTypes::MARKER_CROSS);  //十字标记
	cv::drawMarker(img, cv::Point(60, 60), cv::Scalar(0, 0, 255), cv::MarkerTypes::MARKER_TILTED_CROSS);  //斜十字标记
	cv::drawMarker(img, cv::Point(90, 90), cv::Scalar(255, 0, 0), cv::MarkerTypes::MARKER_STAR);  //米字标记
	cv::drawMarker(img, cv::Point(120, 120), cv::Scalar(0, 255, 255), cv::MarkerTypes::MARKER_DIAMOND);  //菱形标记
	cv::drawMarker(img, cv::Point(150, 150), cv::Scalar(255, 255, 0), cv::MarkerTypes::MARKER_SQUARE);  //正方形标记
	cv::drawMarker(img, cv::Point(180, 180), cv::Scalar(255, 0, 255), cv::MarkerTypes::MARKER_TRIANGLE_UP);  //上三角标记
	cv::drawMarker(img, cv::Point(210, 210), cv::Scalar(0, 255, 0), cv::MarkerTypes::MARKER_TRIANGLE_DOWN);  //下三角标记
	cv::imshow("drawMarker", img);
	cv::waitKey();

	cv::destroyAllWindows();
	return 0;
}

Effect:

Guess you like

Origin blog.csdn.net/weixin_43003108/article/details/127104539