Table of contents
1.1 Example 1-Draw line segments to form the word "王"
2.1 Example 2-Draw a rectangular border
2.2 Example 3-Draw a solid rectangle
3.1 Example 4-Drawing "Traffic Light"
4.1 Example 5-Drawing an Isosceles Trapezoid
5.1 Example 6-Drawing text OpenCvSharp
1. Drawing of line segments
OpenCvSharp provides the Cv2.Line method, which can be used to draw various line segments. The Cv2.Line method is as follows:
public static void Line(InputOutputArray img, Point pt1, Point pt2, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)
illustrate:
摘要:
Draws a line segment connecting two points
参数:
img:
The image.
pt1:
First point of the line segment.
pt2:
Second point of the line segment.
color:
Line color.
thickness:
Line thickness. [By default this is 1]
lineType:
Type of the line. [By default this is LineType.Link8]
shift:
Number of fractional bits in the point coordinates. [By default this is 0]
1.1 Example 1-Draw line segments to form the word "王"
code show as below:
Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);
Cv2.Line(canvas, new Point(50, 50), new Point(250, 50), new Scalar(255, 0, 0), 5);
Cv2.Line(canvas, new Point(50, 150), new Point(250, 150), new Scalar(0, 255, 0), 10);
Cv2.Line(canvas, new Point(50, 250), new Point(250, 250), new Scalar(0, 0, 255), 15);
Cv2.Line(canvas, new Point(150, 50), new Point(150, 250), new Scalar(0, 255, 255), 20);Cv2.ImShow("lines", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();
Effect
The starting and ending coordinates of each line segment
2. Drawing of rectangle
OpenCvSharp provides the Cv2.Rectangle method, which can draw either a rectangular border or a solid rectangle. Its function is as follows:
public static void Rectangle(InputOutputArray img, Point pt1, Point pt2, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)
说明:
摘要:
Draws simple, thick or filled rectangle
Number of references:
img:
Image.
pt1:
One of the rectangle vertices.
pt2:
Opposite rectangle vertex.
color:
Line color (RGB) or brightness (grayscale image).
thickness:
Thickness of lines that make up the rectangle. Negative values make the function
to draw a filled rectangle. [By default this is 1]
lineType:
Type of the line, see cvLine description. [By default this is LineType.Link8]
shift:
Number of fractional bits in the point coordinates. [By default this is 0]
2.1 Example 2-Draw a rectangular border
code show as below:
Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);
Cv2.Rectangle(canvas, new Point(50, 50), new Point(200, 150), new Scalar(255, 0, 0), 20);
Cv2.ImShow("Rectangle", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();
Effect
2.2 Example 3-Draw a solid rectangle
code show as below:
Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);
Cv2.Rectangle(canvas, new Point(50, 50), new Point(200, 150), new Scalar(255, 0, 0), -1);
Cv2.ImShow("Rectangle", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();
Effect
3. Drawing of circles
OpenCvSharp provides the Cv2.Circle method, which can draw either a circular border or a solid circle. The Cv2.Circle function is as follows:
public static void Circle(InputOutputArray img, int centerX, int centerY, int radius, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)
illustrate:
摘要:
Draws a circle
参数:
img:
Image where the circle is drawn.
centerX:
X-coordinate of the center of the circle.
centerY:
Y-coordinate of the center of the circle.
radius:
Radius of the circle.
color:
Circle color.
thickness:
Thickness of the circle outline if positive, otherwise indicates that a filled
circle has to be drawn. [By default this is 1]
lineType:
Type of the circle boundary. [By default this is LineType.Link8]
shift:
Number of fractional bits in the center coordinates and radius value. [By default
this is 0]
3.1 Example 4-Drawing "Traffic Light"
code show as below:
Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);
Cv2.Circle(canvas, new Point(50, 150), 40, new Scalar(0, 0, 255), -1);
Cv2.Circle(canvas, new Point(150, 150), 40, new Scalar(0, 255, 255), -1);
Cv2.Circle(canvas, new Point(250, 150), 40, new Scalar(0, 255, 0), -1);
Cv2.ImShow("TrafficLights", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();
Effect
4. Polygon drawing
OpenCvSharp provides the Polylines method to draw polygons, its function is as follows:
public static void Polylines(Mat img, IEnumerable<IEnumerable<Point>> pts, bool isClosed, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)
illustrate:
摘要:
draws one or more polygonal curves
Number of references:
img:
Canvas
pts:
Multi-shape each List of points composition > lineType: line width thickness:
shift:
4.1 Example 5-Drawing an Isosceles Trapezoid
code show as below:
Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);
List<OpenCvSharp.Point> pts1 = new List<OpenCvSharp.Point>
{
new OpenCvSharp.Point(100,50),
new OpenCvSharp.Point(200,50),
new OpenCvSharp.Point(50,250),
new OpenCvSharp.Point(250,250)
};List<List<OpenCvSharp.Point>> pts = new List<List<Point>>();
pts.Add(pts1);Cv2.Polylines(canvas, pts, true, new Scalar(0, 0, 255), 1);
Cv2.ImShow("Polylines", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();
Effect
5. Drawing of text
OpenCvSharp provides the Cv2.PutText method for text drawing. Its function is as follows:
public static void PutText(InputOutputArray img, string text, Point org, HersheyFonts fontFace, double fontScale, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, bool bottomLeftOrigin = false)
illustrate:
摘要:
renders text string in the image
Number of references:
img:
Image.
text:
Text string to be drawn.
org:
Bottom-left corner of the text string in the image.
fontFace:
Font type, see #HersheyFonts.
fontScale:
Font scale factor that is multiplied by the font-specific base size.
color:
Text color.
thickness:
Thickness of the lines used to draw a text.
lineType:
Line type. See #LineTypes
bottomLeftOrigin:
When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
5.1 Example 6-Drawing text OpenCvSharp
code show as below:
Mat canvas = Mat.Zeros(300, 300, MatType.CV_8UC3);
Cv2.PutText(canvas, "OpenCvSharp", new Point(0, 50), HersheyFonts.Italic, 1, new Scalar(0,255,0), 1, LineTypes.AntiAlias, false);
Cv2.ImShow("Text", canvas);
Cv2.WaitKey();
Cv2.DestroyAllWindows();
Effect