一、效果
二、常用函数
2.1 添加文字
//
// 摘要:
// Renders the text in the image with the specified font and color. The printed
// text is clipped by ROI rectangle. Symbols that do not belong to the specified
// font are replaced with the rectangle symbol.
//
// 参数:
// img:
// Input image
//
// text:
// String to print
//
// org:
// Coordinates of the bottom-left corner of the first letter
//
// fontFace:
// Font type.
//
// 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
//
// bottomLeftOrigin:
// When true, the image data origin is at the bottom-left corner. Otherwise, it
// is at the top-left corner.
void PutText(IInputOutputArray img, string text, Point org, FontFace fontFace, double fontScale, MCvScalar color, int thickness = 1, LineType lineType = LineType.EightConnected, bool bottomLeftOrigin = false)
2.2 绘制直线
//
// 摘要:
// Draws the line segment between pt1 and pt2 points in the image. The line is clipped
// by the image or ROI rectangle. For non-antialiased lines with integer coordinates
// the 8-connected or 4-connected Bresenham algorithm is used. Thick lines are drawn
// with rounding endings. Antialiased lines are drawn using Gaussian filtering.
//
// 参数:
// img:
// The 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: 8 (or 0) - 8-connected line. 4 - 4-connected line. CV_AA -
// antialiased line.
//
// shift:
// Number of fractional bits in the point coordinates
public static void Line(IInputOutputArray img, Point pt1, Point pt2, MCvScalar color, int thickness = 1, LineType lineType = LineType.EightConnected, int shift = 0)
2.3 绘制圆
//
// 摘要:
// Draws a simple or filled circle with given center and radius. The circle is clipped
// by ROI rectangle.
//
// 参数:
// img:
// Image where the circle is drawn
//
// center:
// Center of the circle
//
// radius:
// Radius of the circle.
//
// color:
// Color of the circle
//
// thickness:
// Thickness of the circle outline if positive, otherwise indicates that a filled
// circle has to be drawn
//
// lineType:
// Line type
//
// shift:
// Number of fractional bits in the center coordinates and radius value
public static void Circle(IInputOutputArray img, Point center, int radius, MCvScalar color, int thickness = 1, LineType lineType = LineType.EightConnected, int shift = 0)
2.4 绘制矩形
//
// 摘要:
// Draws a rectangle specified by a CvRect structure
//
// 参数:
// img:
// Image
//
// rect:
// The rectangle to be drawn
//
// color:
// Line color
//
// thickness:
// Thickness of lines that make up the rectangle. Negative values make the function
// to draw a filled rectangle.
//
// lineType:
// Type of the line
//
// shift:
// Number of fractional bits in the point coordinates
public static void Rectangle(IInputOutputArray img, Rectangle rect, MCvScalar color, int thickness = 1, LineType lineType = LineType.EightConnected, int shift = 0)
2.5 绘制椭圆
//
// 摘要:
// Draws a simple or thick elliptic arc or fills an ellipse sector. The arc is clipped
// by ROI rectangle. A piecewise-linear approximation is used for antialiased arcs
// and thick arcs. All the angles are given in degrees.
//
// 参数:
// img:
// Image
//
// box:
// The box the define the ellipse area
//
// color:
// Ellipse color
//
// thickness:
// Thickness of the ellipse arc
//
// lineType:
// Type of the ellipse boundary
//
// shift:
// Number of fractional bits in the center coordinates and axes' values
public static void Ellipse(IInputOutputArray img, RotatedRect box, MCvScalar color, int thickness = 1, LineType lineType = LineType.EightConnected, int shift = 0)
三、代码
// 1. 加载原图1
var image1 = new Image<Bgr, byte>("bird1.png");
var image0 = image1.Clone();
CvInvoke.PutText(image1,"Original",new Point(200,400),FontFace.HersheyTriplex, 3, new MCvScalar(0, 0, 255), 5);
PreviewImage1 = new WriteableBitmap(Bitmap2BitmapImage(image1.Bitmap));
// 2. 绘制直线
var image2 = new Image<Bgr, byte>(image0.Data);
CvInvoke.Line(image2, new Point(50, 50), new Point(450, 450), new MCvScalar(0, 255, 255), 5, LineType.FourConnected);
PreviewImage2 = new WriteableBitmap(Bitmap2BitmapImage(image2.Bitmap));
// 2. 圆
var image3 = new Image<Bgr, byte>(image0.Data);
CvInvoke.Circle(image3, new Point(250, 250), 150, new MCvScalar(255, 0, 0), 2, LineType.FourConnected);
PreviewImage3 = new WriteableBitmap(Bitmap2BitmapImage(image3.Bitmap));
// 4. 实心圆
var image4 = new Image<Bgr, byte>(image0.Data);
CvInvoke.Circle(image4, new Point(250, 250), 50, new MCvScalar(255, 0, 0), -1, LineType.FourConnected);
PreviewImage4 = new WriteableBitmap(Bitmap2BitmapImage(image4.Bitmap));
// 5. 矩形
var image5 = new Image<Bgr, byte>(image0.Data);
CvInvoke.Rectangle(image5, new Rectangle(100, 100, 300, 200), new MCvScalar(225, 225, 0), 5, LineType.FourConnected);
PreviewImage5 = new WriteableBitmap(Bitmap2BitmapImage(image5.Bitmap));
// 6. 实心矩形
var image6 = new Image<Bgr, byte>(image0.Data);
CvInvoke.Rectangle(image6, new Rectangle(100, 100, 300, 200), new MCvScalar(225, 225, 0), -1, LineType.FourConnected);
PreviewImage6 = new WriteableBitmap(Bitmap2BitmapImage(image6.Bitmap));
// 7. 椭圆
var image7 = new Image<Bgr, byte>(image0.Data);
CvInvoke.Ellipse(image7, new RotatedRect(new PointF(250, 250), new SizeF(100, 200), 0), new MCvScalar(200, 0, 0), 5);
PreviewImage7 = new WriteableBitmap(Bitmap2BitmapImage(image7.Bitmap));
// 8. 实心椭圆
var image8 = new Image<Bgr, byte>(image0.Data);
CvInvoke.Ellipse(image8, new RotatedRect(new PointF(250, 250), new SizeF(100, 200), 0), new MCvScalar(200, 0, 0),-1);
PreviewImage8 = new WriteableBitmap(Bitmap2BitmapImage(image8.Bitmap));
// 9. 矩形加椭圆
var image9 = new Image<Bgr, byte>(image0.Data);
CvInvoke.Ellipse(image9, new Point(250, 250), new Size(150, 100), 45, 0, 270, new MCvScalar(0, 0, 255), -2);
PreviewImage9 = new WriteableBitmap(Bitmap2BitmapImage(image9.Bitmap));