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:
LINE_8 = 8 Lines processed by the Bresenham algorithm based on 4-neighborhood connections.
Effect:
LINE_AA = 16 Gaussian filter smoothing based on the straight line.
Effect:
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);
img
is the input image; center
is the center of the drawn circle; radius
is the radius of the drawn circle; color
is the drawn color; thickness
is the drawn line width; lineType
is the drawn line style; shift
offset.
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);
img
is the input image; color
is the color to be drawn; thickness
is the line width to be drawn; lineType
is the line style shift
to be drawn;
Function prototype 2:
void ellipse(InputOutputArray img, const RotatedRect& box, const Scalar& color,
int thickness = 1, int lineType = LINE_8);
img
is the input image; color
is the color to be drawn; thickness
is the line width to be drawn; lineType
is the line style shift
to be drawn;
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);
img
is the input image; pt1
is one end point of the line; pt2
is the other end point of the line; color
is the drawn color; thickness
is the drawn line width; lineType
is the drawn line style; shift
the offset.
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.
img
is the input image; pts
is the input point set; isClosed
the flag indicates whether the point set is closed; color
is the color to draw; thickness
is the line width to draw; lineType
is the line style to draw; shift
offset.
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.
img
is the input image; pt1
is the upper-left point of the rectangle; pt2
is the lower-right point of the rectangle; color
is the color to be drawn; thickness
is the line width to be drawn; lineType
is the line style to be drawn; shift
offset.
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.
img
is the input image; points
is the input point set; color
is the color to draw; thickness
is the line width to draw; lineType
is the line style to draw; shift
offset.
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 );
img
is the input image; is text
the text to be drawn; is org
the reference point of the drawn text; fontFace
is the text style of the drawn text ; is fontScale
the zoom factor of the drawn text ; is the color drawn; The font style fontFace description is a bunch of enumeration values:color
thickness
lineType
bottomLeftOrigin
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:
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.
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);
img
is the input image; pt1
is the starting point of the arrow line segment; pt2
is the end point of the arrow line segment; color
is the drawn color; thickness
is the drawn line width; line_type
is the drawn line style; shift
is the offset; tipLength
is 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);
img
is the input image; position
is the origin of the marker; color
is the color to draw; markerType
is the kind of marker; markerSize
is the size of the marker; thickness
is the width of the drawn line; line_type
is 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: