OpenCvSharp随机数与绘制文本用法

目的
RNG函数使用

PutText函数使用

GetTextSize函数使用

RNG随机数生成
生成伪随机数。注意,当初始的state一样时,每次生成的随机数序列是一样的。

PutText绘制文本
函数说明:将文本绘制到图像上(不支持中文)。无绘制的文本用问号代替。

//函数原型
void PutText(InputOutputArray img,string text,Point org,HersheyFonts fontFace,double fontScale,Scalar color,int thickness = 1,LineTypes lineType = LineTypes.Link8,bool bottomLeftOrigin = false)

 

/// <summary>
/// 绘制文本
/// </summary>
/// <param name="image"></param>
/// <param name="winName"></param>
/// <param name="rng"></param>
/// <returns></returns>
int Display_Big_End(Mat image, string winName)
{var text = "OpenCvSharp";double fontScale = 2D;int thickness = 4;//图像中心点Point center = new Point(image.Width >> 1, image.Height >> 1);Size textSize = Cv2.GetTextSize(text, HersheyFonts.HersheyComplex, fontScale, thickness, out int baseLine);//文字在图像的中心Point org = new Point((image.Width - textSize.Width) / 2, (image.Height + textSize.Height - baseLine) / 2);Mat image2;for (int i = 0; i < 255; i += 2){image2 = image - Scalar.All(i);//bottomLeftOrign=falseCv2.PutText(image2, text, org, HersheyFonts.HersheyComplex, fontScale, new Scalar(i, i, 255), thickness, lineType, false); //文本插入点Cv2.Circle(image2, org, 3, Scalar.White, -1);//图片中心点Cv2.Circle(image2, center, 5, Scalar.Green, -1);//文本的宽高Cv2.Rectangle(image2, org, new Point(org.X + textSize.Width, org.Y - textSize.Height), Scalar.Red);//基线位置Cv2.Line(image2, new Point(org.X, org.Y + baseLine), new Point(org.X + textSize.Width, org.Y + baseLine), Scalar.White, 1);//bottomLeftOrign = true,以基线为镜var newOrg = new Point(org.X, org.Y + baseLine * 2);Cv2.PutText(image2, text, newOrg, HersheyFonts.HersheyComplex, fontScale, new Scalar(i, i, 255), thickness, lineType, true);//新的文本插入点Cv2.Circle(image2, newOrg, 3, Scalar.White, -1);Cv2.ImShow(winName, image2);if (Cv2.WaitKey(DELAY) >= 0) return -1;}return 0;
}

上面文字会正常插入,红色框为GetTextSize获取的文本大小,左边白点为插入点。白色线为基线。

GetTextSize获取绘制文本的大小

函数说明:计算绘制文本的宽、高和基线y轴偏移。

//函数原型
Size GetTextSize(string text,HersheyFonts fontFace,double fontScale,int thickness,out int baseLine)

 

代码实例:

int Width = 600;
int NUMBER = 50;
int DELAY = 50;
LineTypes lineType = LineTypes.Link8;public void Run(ParamBase paramBase)
{//注意初始值一样,每次生成的随机数序列是一样的//RNG rng = new RNG(0xFFFFFFFF);RNG rng = new RNG((ulong)DateTime.Now.Ticks);            var winName = "Random generator and text with OpenCV";try{using (Mat image = Mat.Zeros(Width, Width, MatType.CV_8UC3)){int c = 0;//随机画线c = Drawing_Random_Lines(image.Clone(), winName, rng);if (c != 0) return;//随机绘制或填充矩形c = Drawing_Random_Rectangels(image.Clone(), winName, rng);if (c != 0) return;//随机绘制或填充椭圆轮廓c = Drawing_Random_Ellipses(image.Clone(), winName, rng);if (c != 0) return;//随机显示文本c = Drawing_Random_Text(image.Clone(), winName, rng);if (c != 0) return;//测试PutText或GetTextSize函数c = Display_Big_End(image, winName);}}catch { }finally{Cv2.DestroyAllWindows();}}/// <summary>
/// 绘制文本
/// </summary>
/// <param name="image"></param>
/// <param name="winName"></param>
/// <param name="rng"></param>
/// <returns></returns>
int Display_Big_End(Mat image, string winName)
{var text = "OpenCvSharp";double fontScale = 2D;int thickness = 4;//图像中心点Point center = new Point(image.Width >> 1, image.Height >> 1);Size textSize = Cv2.GetTextSize(text, HersheyFonts.HersheyComplex, fontScale, thickness, out int baseLine);//文字在图像的中心Point org = new Point((image.Width - textSize.Width) / 2, (image.Height + textSize.Height - baseLine) / 2);Mat image2;for (int i = 0; i < 255; i += 2){image2 = image - Scalar.All(i);//bottomLeftOrign=falseCv2.PutText(image2, text, org, HersheyFonts.HersheyComplex, fontScale, new Scalar(i, i, 255), thickness, lineType, false); //文本插入点Cv2.Circle(image2, org, 3, Scalar.White, -1);//图片中心点Cv2.Circle(image2, center, 5, Scalar.Green, -1);//文本的宽高Cv2.Rectangle(image2, org, new Point(org.X + textSize.Width, org.Y - textSize.Height), Scalar.Red);//基线位置Cv2.Line(image2, new Point(org.X, org.Y + baseLine), new Point(org.X + textSize.Width, org.Y + baseLine), Scalar.White, 1);//bottomLeftOrign = true,以基线为镜var newOrg = new Point(org.X, org.Y + baseLine * 2);Cv2.PutText(image2, text, newOrg, HersheyFonts.HersheyComplex, fontScale, new Scalar(i, i, 255), thickness, lineType, true);//新的文本插入点Cv2.Circle(image2, newOrg, 3, Scalar.White, -1);Cv2.ImShow(winName, image2);if (Cv2.WaitKey(DELAY) >= 0) return -1;}return 0;
}/// <summary>
/// 随机绘制文本
/// </summary>
/// <param name="image"></param>
/// <param name="winName"></param>
/// <param name="rng"></param>
/// <returns></returns>
int Drawing_Random_Text(Mat image, string winName, RNG rng)
{Point org = new Point();for (int i = 0; i < NUMBER; i++){org.X = rng.Uniform(0, Width);org.Y = rng.Uniform(0, Width);Cv2.PutText(image, "Testing text rendering", org,(HersheyFonts)rng.Uniform(0, 8),rng.Uniform(0, 100) * 0.05 + 0.1,randomColor(rng),rng.Uniform(0, 10),lineType);Cv2.ImShow(winName, image);if (Cv2.WaitKey(DELAY) >= 0) return -1;}return 0;
}/// <summary>
/// 返回随机颜色
/// </summary>
/// <param name="rng"></param>
/// <returns></returns>
Scalar randomColor(RNG rng)
{//Scalar.RandomColor();uint iColor = (uint)rng;return new Scalar(iColor & 255, (iColor >> 8) & 255, (iColor >> 16) & 255);}
/// <summary>
/// 随机画椭圆弧
/// </summary>
/// <param name="image"></param>
/// <param name="winName"></param>
/// <param name="rng"></param>
/// <returns></returns>
int Drawing_Random_Ellipses(Mat image, string winName, RNG rng)
{Point center = new Point();Size axes = new Size();for (int i = 0; i < NUMBER; i++){center.X = rng.Uniform(0, Width);center.Y = rng.Uniform(0, Width);axes.Width = rng.Uniform(0, Width);axes.Height = rng.Uniform(0, Width);Cv2.Ellipse(image, center, axes, rng.Uniform(0, 361), rng.Uniform(0, 361), rng.Uniform(0, 361), randomColor(rng), RandomThickness(rng), lineType);Cv2.ImShow(winName, image);if (Cv2.WaitKey(DELAY) >= 0) return -1;}return 0;
}/// <summary>
/// 随机画矩形
/// </summary>
/// <param name="image"></param>
/// <param name="winName"></param>
/// <param name="rng"></param>
/// <returns></returns>
int Drawing_Random_Rectangels(Mat image, string winName, RNG rng)
{Point pt1 = new Point();Point pt2 = new Point();for (int i = 0; i < NUMBER; i++){pt1.X = rng.Uniform(0, Width);pt1.Y = rng.Uniform(0, Width);pt2.X = rng.Uniform(0, Width);pt2.Y = rng.Uniform(0, Width);Cv2.Rectangle(image, pt1, pt2, randomColor(rng), RandomThickness(rng), lineType);Cv2.ImShow(winName, image);if (Cv2.WaitKey(DELAY) >= 0) return -1;}return 0;
}/// <summary>
/// 获取颜色线宽
/// </summary>
/// <param name="rng"></param>
/// <returns></returns>
int RandomThickness(RNG rng)
{int thickness = rng.Uniform(-9, 10);if (thickness == 0) thickness = 1;return thickness;
}/// <summary>
/// 随机画线
/// </summary>
/// <param name="image"></param>
/// <param name="winName"></param>
/// <param name="rng"></param>
/// <returns></returns>
int Drawing_Random_Lines(Mat image, string winName, RNG rng)
{Point pt1 = new Point();Point pt2 = new Point();for (int i = 0; i < NUMBER; i++){pt1.X = rng.Uniform(0, Width);pt1.Y = rng.Uniform(0, Width);pt2.X = rng.Uniform(0, Width);pt2.Y = rng.Uniform(0, Width);Cv2.Line(image, pt1, pt2, randomColor(rng), rng.Uniform(1, 10), lineType);Cv2.ImShow(winName, image);if (Cv2.WaitKey(DELAY) >= 0) return -1;}return 0;
}

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2799313.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

[PDF编辑精选] 适用于 Windows 10/11 的PDF 编辑器列表

PDF 是 Portable Document Format 的缩写&#xff0c;是 Adob​​e 创建的一种文件格式&#xff0c;用于在保留格式的情况下轻松查看和共享——无论查看文档的任何人使用的软件或操作系统如何。然而&#xff0c;与PDF文档相关的一个主要问题出现了&#xff0c;那就是编辑文档内…

SpringSecurity安全框架

我们使用这个springSecurity安全框架,作用是认证,授权,将用户的权限和对应的资源进行绑定,默认的是在内存中保存的,实际开发中,是需要根据项目业务的需求对某些方法进行重写,使数据库中权限对应的资源进行绑定,就是查看当前登录的用户所扮演的角色,该角色有哪些权限 授权 1内…

什么是HTTP代理,socks5代理?它们的区别是什么?

什么是HTTP代理&#xff1f; HTTP代理是一种常见的网络代理方式&#xff0c;它通过在客户端和服务器之间建立一个中间层&#xff0c;将客户端的请求转发给服务器&#xff0c;并将服务器的响应返回给客户端。HTTP代理通常用于访问受限制的网站&#xff0c;或者在网络中隐藏客户…

【2.3深度学习开发任务实例】(1)神经网络模型的特点【大厂AI课学习笔记】

从本章开始&#xff0c;我把标题的顺序变了一下&#xff0c;大厂AI课笔记&#xff0c;放到后面。因为我发现App上&#xff0c;标题无法显示完全。 从本章开始&#xff0c;要学习深度学习开发任务的全部过程了。 我们将通过小汽车识别赛道上的标志牌&#xff0c;给出检测框&am…

UE5 摄像机晃动

1.新建camerashake蓝图类 命名为 晃动 2.调节相关参数 3.打开关卡序列 给摄像机添加 晃动 动画 4.播放

uniapp微信小程序-项目实战修改密码

图标是使用uview里面的图标&#xff0c;icfont也可以 以下是所有代码 <template><view><!-- 密码三个 --><view class"password" v-for"(item,index) in userList"><view class"contentuser"><view class&qu…

从源码解析Kruise(K8S)原地升级原理

从源码解析Kruise原地升级原理 本文从源码的角度分析 Kruise 原地升级相关功能的实现。 本篇Kruise版本为v1.5.2。 Kruise项目地址: https://github.com/openkruise/kruise 更多云原生、K8S相关文章请点击【专栏】查看&#xff01; 原地升级的概念 当我们使用deployment等Wor…

区块链游戏解说:什么是 Planet IX

作者&#xff1a;lesleyfootprint.network 编译&#xff1a;cicifootprint.network 数据源&#xff1a;Planet IX Dashboard 什么是 Planet IX Planet IX&#xff0c;一个由原生 IX TOKEN 推动的 Web3 玩赚平台。作为一款 GameFi 策略游戏&#xff0c; Planet IX 上的每项资…

抠人像可抠头发丝的模型-软语义分割(Semantic Human Matting)

软语义分割&#xff08;Semantic Human Matting&#xff09; 一、Semantic Human Matting原理二、Semantic Human Matting 项目文件介绍三、数据集介绍及下载地址四、训练流程五、项目代码下载地址 哔哩哔哩详细解说&#xff08;进主页看全集&#xff09;&#xff1a; https://…

极狐GitLab 如何重置管理员密码

在之前安装极狐GitLab 的文章中提到&#xff0c;极狐GitLab 安装成功后&#xff0c;初始登录密码会放在 /etc/gitlab/initial_root_password 文件下&#xff0c;用户可以使用初始用户名 root 及文件内的初始密码即可登录极狐GitLab 实例。 但是有些情况下&#xff0c;可能会发…

Sui主网升级至V1.18.1版本

Sui主网现已升级至V1.18.1版本&#xff0c;同时Sui协议升级至36版本。其他升级要点如下所示&#xff1a; #15794 解析错误不再停止编译&#xff0c;并且后续编译阶段的诊断信息也可能包含在编译结果中&#xff0c;所以开发者可能会看到比以前更多的编译器诊断信息。 #12337 …

基于springboot + vue实现的前后端分离-酒店管理系统

项目介绍 基于springboot vue实现的酒店管理系统一共有酒店管理员和用户这两种角色。 管理员功能 登录&#xff1a;管理员可以通过登录功能进入系统&#xff0c;确保只有授权人员可以访问系统。用户管理&#xff1a;管理员可以添加、编辑和删除酒店的用户&#xff0c;包括前…

线性代数:向量组及其线性相关性

目录 向量组 向量组的线性表示 向量组等价 相关定理 向量组的线性相关性 定理 向量组 向量组的线性表示 向量组等价 相关定理 向量组的线性相关性 定理

物联网在智慧景区中的应用:提升游客体验与运营效率

目录 一、物联网技术概述 二、物联网在智慧景区中的应用 1、智能门票系统 2、智能导览系统 3、智能安全监控系统 4、智能环保系统 三、物联网在智慧景区中提升游客体验 1、提高游览便捷性 2、个性化服务体验 3、提升游客安全感 四、物联网在智慧景区中提升运营效率 …

职业技能鉴定服务中心前端静态页面(官网+证书查询)

有个朋友想做职业技能培训&#xff0c;会发证书&#xff0c;证书可以在自己网站可查。想做一个这样的网站&#xff0c;而且要特别土&#xff0c;一眼看上去像xxx官方网站&#xff0c;像jsp .net技术开发的网站。用htmlcssjquery还原了这样子一个前端页面&#xff0c;这里分享给…

【嵌入式学习】QT-Day2-Qt基础

1> 思维导图 https://lingjun.life/wiki/EmbeddedNote/20QT 2>登录界面优化 使用手动连接&#xff0c;将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中&#xff0c;在自定义的槽函数中调用关闭函数 将登录按钮使用qt5版本的连接到自定义的槽函数中&#xff…

聚道云软件连接器:高科技企业财务自动化,提升效率准确性!

客户介绍&#xff1a; 某互联信息技术有限公司是一家专业从事信息技术服务的高科技企业&#xff0c;在业内享有较高的知名度和影响力。近年来&#xff0c;公司业务快速发展&#xff0c;对信息化建设提出了更高的要求。 客户痛点&#xff1a; 在传统情况下&#xff0c;该公司的…

CSS3中盒子居中

&#xff08;1&#xff09;利用定位&#xff08;子绝父相&#xff09;、margin-left、和margin-top实现 &#xff08;2&#xff09;利用定位&#xff08;子绝父相&#xff09;、transfrom属性实现 &#xff08;3&#xff09;利用flex布局实现盒子居中

C#算法(12)—对图像像素做X/Y方向的偏移

我们在上位机开发领域有时候需要对获取的图像的像素做整体的偏移,比如所有像素在X方向上偏移几个像素,或者所有像素在Y方向上偏移几个像素,本文就是开发了像素整体偏移算法来解决这个问题。 public partial class Form1 : Form{public Form1(){InitializeComponent()

亿道丨三防平板电脑厂商哪家好丨麒麟系统三防平板PAD

随着科技的飞速发展&#xff0c;人们对于移动设备的需求越来越高。然而&#xff0c;在不同的行业应用场景下&#xff0c;常规的智能平板往往无法满足特殊的工作要求。&#xff0c;亿道三防平板&#xff0c;将高可靠性与卓越性能高度结合&#xff0c;为各行各业提供卓越的移动解…