csharp: use custom fonts in Emgu.CV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// from https://stackoverflow.com/questions/35381238/how-to-use-custom-fonts-in-emgucv
string text = "涂聚文(Geovin Du)";
// 下面定义一个矩形区域
int rectWidth = text.Length * (fontSize + 10);
int rectHeight = fontSize + 10;
 
 
//Draw detected area
foreach (Rectangle face1 in faces)
{
    // 声明矩形域
   // Rectangle textArea = new Rectangle(face1.X + face1.Width, face1.Y, rectWidth, rectHeight);
    //grapPic.DrawString(text, font, whiteBrush, textArea);
    //image.Draw(textArea, new Bgr(Color.Red), 2);
    image.Draw(face1, new Bgr(Color.Red), 2); //
 
 
    //中文乱码
    CvInvoke.PutText(image, "geovindu", new System.Drawing.Point(face1.X-25, face1.Y-25), Emgu.CV.CvEnum.FontFace.HersheyScriptComplex,2, new MCvScalar(255, 255, 0), 2);
    System.Drawing.Bitmap bmp;
    bmp = new System.Drawing.Bitmap(200, 45);
    Graphics g = Graphics.FromImage(bmp);
    Font drawFont = new Font("宋体", 24, FontStyle.Bold);
    g.DrawString(text, drawFont, Brushes.Red, new PointF(0, 0));
    g.Save();
    for (int i = 0; i < 200; i++)
    {
        for (int j = 0; j < 45; j++)
        {
            Color c = bmp.GetPixel(i, j);
            if (c.R > 0 || c.B > 0 || c.G > 0)
            {
                CvInvoke.cvSet2D(image, j + 10 + 200, i + 200, new MCvScalar(c.B, c.G, c.R)); //修改对应像素值
            }
        }
    }
    //自定义字体 Geovin Du
    // 'PrivateFontCollection' is in the 'System.Drawing.Text' namespace
    var foo = new PrivateFontCollection();
    //
    string fonfile =Environment.CurrentDirectory+ @"/font/迷你繁篆书.ttf";
    foo.AddFontFile(fonfile);
    var myCustomFont = new Font((FontFamily)foo.Families[0], 36f);
    //Emgu.CV.CvEnum.FontFace.HersheyScriptComplex
    System.Drawing.Bitmap bmpd;
    bmpd = new System.Drawing.Bitmap(200, 45);
    Graphics gg = Graphics.FromImage(bmpd);
    gg.DrawString("涂聚文 Geovin Du", myCustomFont, Brushes.Red, new PointF(0, 0));
    gg.Save();
    //根据图片的大小
    for (int i = 0; i < 200; i++)
    {
        for (int j = 0; j < 45; j++)
        {
            Color c = bmpd.GetPixel(i, j);
            if (c.R > 0 || c.B > 0 || c.G > 0)
            {
                CvInvoke.cvSet2D(image, j + 10 + 500, i +500, new MCvScalar(c.B, c.G, c.R)); //修改对应像素值
            }
        }
    }
 
 
 
}
foreach (Rectangle eye1 in eyes)
{
    image.Draw(eye1, new Bgr(Color.Blue), 2);
}

  

 

https://www.nuget.org/packages/EmguCV

posted @   ®Geovin Du Dream Park™  阅读(474)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2018-07-13 PHP7.27: connect mysql 5.7 using new mysqli
2016-07-13 csharp: Speech
2015-07-13 csharp: Data binding in WPF DataGrid control
2010-07-13 CSS Vertical Text
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示