using OpenCvSharp; using OpenCvSharp.Extensions; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace app { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } /// <summary> /// 画线 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLine_Click(object sender, EventArgs e) { // 创建一张大小为500*500图像 Mat src = new Mat(500, 500, MatType.CV_8UC3, new Scalar(255, 255, 255)); OpenCvSharp.Point pStart = new OpenCvSharp.Point(10, 30); OpenCvSharp.Point pEnd; pEnd.X = 300; pEnd.Y = 50; // 红色线条 Scalar color = new Scalar(0, 0, 255); /* img: 源图像 pt1: 第一个点 pt2: 第二个点 color 线条颜色 thickness: 线条的厚度(粗细) lineType: 线条类型 (枚举值) shift 点坐标中的小数位数。[默认值为0] */ Cv2.Line(src, pStart, pEnd, color, 1, LineTypes.Link8); Cv2.ImShow("原图:", src); Cv2.WaitKey(); } /// <summary> /// 绘制矩形 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRectangle_Click(object sender, EventArgs e) { // 创建一张大小为500*500图像 Mat src = new Mat(500, 500, MatType.CV_8UC3, new Scalar(255, 255, 255)); Rect rect = new Rect(100, 200, 100, 100); // 红色线条 Scalar color = new Scalar(0, 0, 255); /* img: 源图像 rect: 举行对象 color 线条颜色 thickness: 线条的厚度(粗细) lineType: 线条类型 (枚举值) shift 点坐标中的小数位数。[默认值为0] */ Cv2.Rectangle(src, rect, color, 2, LineTypes.AntiAlias); // LineTypes.AntiAlias:反锯齿效果 Cv2.ImShow("原图:", src); Cv2.WaitKey(); } /// <summary> /// 绘制圆形 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCricle_Click(object sender, EventArgs e) { // 创建一张大小为500*500图像 Mat src = new Mat(500, 500, MatType.CV_8UC3, new Scalar(255, 255, 255)); // 红色线条 Scalar color = new Scalar(0, 0, 255); OpenCvSharp.Point conter = new OpenCvSharp.Point(src.Cols / 2, src.Rows / 2); Cv2.Ellipse(src, conter, new OpenCvSharp.Size(src.Cols / 4, src.Cols / 4), 90, 0, 360, color, 2, LineTypes.Link8); Cv2.ImShow("原图:", src); Cv2.WaitKey(); } /// <summary> /// 绘制椭圆 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnEllipse_Click(object sender, EventArgs e) { // 创建一张大小为500*500图像 Mat src = new Mat(500, 500, MatType.CV_8UC3, new Scalar(255, 255, 255)); // 红色线条 Scalar color = new Scalar(0, 0, 255); /* 1.背景图片 2.中心点 椭圆的长轴和短轴 3.椭圆的旋转的角度 4.起始角度 5.结束角度 6.颜色 7.线条宽度 默认 1 8.边界类型 默认 Link8 9.中心坐标和坐标轴值的小数位数,默认0 */ Cv2.Ellipse(src, new OpenCvSharp.Point(250, 250), new OpenCvSharp.Size(100, 200), 90, 0, 360, color, 3, LineTypes.Link8); Cv2.ImShow("原图:", src); Cv2.WaitKey(); } /// <summary> /// 绘制文字 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnText_Click(object sender, EventArgs e) { // 创建一张大小为500*500图像 Mat src = new Mat(500, 500, MatType.CV_8UC3, new Scalar(255, 255, 255)); // 红色线条 Scalar color = new Scalar(0, 0, 255); Cv2.PutText(src, "Hello", new OpenCvSharp.Point(200, 200), HersheyFonts.HersheySimplex, 1, color, 2, LineTypes.Link4); Cv2.ImShow("原图:", src); Cv2.WaitKey(); } } }