本文测试环境:
win10 64位
vistual studio 2019
Emgu CV 4.6.0
环境配置准备:
1 新增控制台项目,.net framework为4.7.2
2 把win-x64目录的native目录下的文件全部拷贝到项目的运行目录Debug目录下
3 项目选择x64
4 添加项目引用Emgu.CV.dll、Emgu.CV.Platform.NetFramework.dll、System.Drawing.dll和System.Runtime.InteropServices.RuntimeInformation.dll
具体配置参考:
Emgu CV4图像处理之环境搭建1(C#)_zxy2847225301的博客-CSDN博客
下面的内容转自:EmguCV-第07讲-基本绘图函数_YADONCHEN的博客-CSDN博客
1 绘制直线
CvInvoke.Line
第一个参数img为要绘制的图像,第二个参数pt1为直线的起点,第三个参数pt2是直线的终点,第四个参数color为颜色
参考代码如下:
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmguCVDemo2
{
class Program
{
static void Main(string[] args)
{
Mat mat = new Mat("deskpic.png");
Mat pic = mat.Clone();
CvInvoke.Line(pic, new System.Drawing.Point(10, 10), new System.Drawing.Point(100, 100), new MCvScalar(0, 0, 255));
CvInvoke.Imshow("test", pic);
CvInvoke.WaitKey(0);
Console.ReadLine();
}
}
}
运行结果如下:
2 绘制圆形
CvInvoke.Circle
第一个参数img为源图片,第二个参数center为圆的中心点,第三个参数radius为圆的半径,第四个参数color为颜色
参考代码如下:
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmguCVDemo2
{
class Program
{
static void Main(string[] args)
{
Mat mat = new Mat("deskpic.png");
Mat pic = mat.Clone();
CvInvoke.Circle(pic, new System.Drawing.Point(300, 300), 200, new MCvScalar(0, 0, 255));
CvInvoke.Imshow("test", pic);
CvInvoke.WaitKey(0);
Console.ReadLine();
}
}
}
运行结果如下:
3 绘制矩形
CvInvoke.Rectangle
第一个参数img为源图片,第二个参数rect为要绘制的矩形,第三个参数color为颜色
参考代码如下:new System.Drawing.Rectangle(100, 100, 400, 200)中的第一个参数100为矩形起点的x坐标,第二个参数100为矩形起点的y坐标,第三个参数400为矩形的宽,第四个参数为矩形的高
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmguCVDemo2
{
class Program
{
static void Main(string[] args)
{
Mat mat = new Mat("deskpic.png");
Mat pic = mat.Clone();
CvInvoke.Rectangle(pic, new System.Drawing.Rectangle(100, 100, 400, 200), new MCvScalar(0,0,255));
CvInvoke.Imshow("test", pic);
CvInvoke.WaitKey(0);
Console.ReadLine();
}
}
}
运行结果如下:
4 为图片添加文字
CvInvoke.PutText
第一个参数img为源图片,第二个参数是要绘制的文本,第三个参数org为绘制文本的起点坐标,第四个参数fontFace是字体,第五个参数fontScale是字缩放的比例,第六个参数color为颜色
参考代码如下:
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmguCVDemo2
{
class Program
{
static void Main(string[] args)
{
Mat mat = new Mat("deskpic.png");
Mat pic = mat.Clone();
CvInvoke.PutText(pic, "put text", new System.Drawing.Point(100, 200), Emgu.CV.CvEnum.FontFace.HersheySimplex,0.5, new MCvScalar(0, 0, 255));
CvInvoke.Imshow("test", pic);
CvInvoke.WaitKey(0);
Console.ReadLine();
}
}
}
运行结果如下: