C#图像处理OpenCV开发指南(CVStar,03)——基于.NET 6的图像处理桌面程序开发实践第一步

1 Visual Studio 2022 开发基于.NET 6的OpenCV桌面程序

1.1 为什么选择.NET 6开发桌面应用?

选择 .NET 6(最早称为 .NET Core)而非 Frameworks.NET 的理由是:(1)跨平台;已经支持Windows,Linux及其国产操作系统和国产龙芯CPU;(2).NET 完全开源;没有授权问题;(3)经过多年发展,已经成熟;

1.2 为什么选择开发桌面应用而非 Console 程序?

恰恰是我们这些从Unix,AIX,DOS等走过来的古董级程序员,不想让用户用键盘输入的方式使用软件。Console程序不过是自嗨的代码,不能称为程序,这个太low了。

1.3 如何开始创建基于.NET 6的桌面程序?

这里有个动画演示,比较清楚,可供参考学习。

最后出现这样的画面即可。

1.4 安装OpenCvSharp开发环境。

 这个请阅读另外一篇博客。

OpenCvSharp开发环境的安装、搭建之可视化教程icon-default.png?t=N7T8https://blog.csdn.net/beijinghorn/article/details/125528673

2 开发OpenCvSharp桌面程序的实践

2.1 需求分析

凡是程序,而非代码,必须有其需求与分析。

本桌面程序的基本功能需求是:

(1)可读取任意文件夹的各种图片文件,并显示于窗口;

(2)图片可自动缩放;

(3)窗口可缩放;

(4)窗口居中;

(5)图片可转为灰色图;

(6)结果可指定文件夹、文件名保存(另存为);

2.2 相关核心代码

核心代码集中于 Form1.cs 文件。下面分别做一个介绍,最后在归总。

2.2.1 定义图片文件的后缀

string[] ImgExtentions = {"*.*|*.*","JPEG|*.jpg;*.jpeg","GIF|*.gif","PNG|*.png","TIF|*.tif;*.tiff","BMP|*.bmp"
};

2.2.2 定义窗口内的各种控件

Panel? panelTop { get; set; } = null;
Panel? panelBotton { get; set; } = null;
PictureBox? picSource { get; set; } = null;
PictureBox? picResult { get; set; } = null;
Button? btnLoad { get; set; } = null;
Button? btnGray { get; set; } = null;
Button? btnSave { get; set; } = null;private int original_width { get; set; } = 0;
private int original_height { get; set; } = 0;

2.2.3 图片自适应(大小、比例) 

private void PicAutosize(PictureBox pb)
{if (pb == null) return;if (pb.Image == null) return;Image img = pb.Image;int w = original_width;int h = w * img.Height / img.Width;if (h > original_height){h = original_height;w = h * img.Width / img.Height;}pb.SizeMode = PictureBoxSizeMode.Zoom;pb.Width = w;pb.Height = h;pb.Image = img;pb.Refresh();
}

2.2.4 创建窗口内的相关控件(按钮、图片)


private void GUI()
{if (panelTop == null) panelTop = new Panel();panelTop.Parent = this;panelTop.Top = 5;panelTop.Left = 5;panelTop.Width = this.Width - 26;panelTop.Height = 55;panelTop.BorderStyle = BorderStyle.FixedSingle;if (panelBotton == null) panelBotton = new Panel();panelBotton.Parent = this;panelBotton.Top = panelTop.Top + panelTop.Height + 3;panelBotton.Left = 5;panelBotton.Width = panelTop.Width;panelBotton.Height = this.Height - panelBotton.Top - 55;panelBotton.BorderStyle = BorderStyle.FixedSingle;if (picSource == null) picSource = new PictureBox();picSource.Parent = panelBotton;picSource.Left = 5;picSource.Top = 5;picSource.Width = (panelBotton.Width - 10) / 2;picSource.Height = (panelBotton.Height - 10);picSource.BorderStyle = BorderStyle.FixedSingle;if (picResult == null) picResult = new PictureBox();picResult.Parent = panelBotton;picResult.Left = picSource.Left + picSource.Width + 5;picResult.Top = picSource.Top;picResult.Width = picSource.Width;picResult.Height = picSource.Height;picResult.BorderStyle = BorderStyle.FixedSingle;original_width = picSource.Width;original_height = picSource.Height;if (btnLoad == null) btnLoad = new Button();btnLoad.Parent = panelTop;btnLoad.Left = 5;btnLoad.Top = 5;btnLoad.Width = 90;btnLoad.Height = 38;btnLoad.Cursor = Cursors.Hand;btnLoad.Text = "Load";btnLoad.Click += Load_Image;if (btnGray == null) btnGray = new Button();btnGray.Parent = panelTop;btnGray.Left = btnLoad.Left + btnLoad.Width + 5;btnGray.Top = btnLoad.Top;btnGray.Width = 90;btnGray.Height = 38;btnGray.Cursor = Cursors.Hand;btnGray.Text = "Gray";btnGray.Click += ToGray;if (btnSave == null) btnSave = new Button();btnSave.Parent = panelTop;btnSave.Left = btnGray.Left + btnGray.Width + 5;btnSave.Top = btnLoad.Top;btnSave.Width = 90;btnSave.Height = 38;btnSave.Cursor = Cursors.Hand;btnSave.Text = "Save";btnSave.Click += Save;PicAutosize(picSource);PicAutosize(picResult);
}

运行时是这样滴。

 2.2.5 支持窗口、图片同步缩放的代码

private void FormResize(object? sender, EventArgs? e)
{if (this.Width < 200) { this.Width = 320; return; }if (this.Height < 200) { this.Height = 320; return; }GUI();
}

2.2.6 读取图片并显示

private void Load_Image(object? sender, EventArgs? e)
{OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.Filter = String.Join("|", ImgExtentions);if (openFileDialog.ShowDialog() == DialogResult.OK){sourceImage = openFileDialog.FileName;picSource.Image = Image.FromFile(sourceImage);picResult.Image = picSource.Image;PicAutosize(picSource);PicAutosize(picResult);}
}

2.2.7 转为灰色图片

private void ToGray(object? sender, EventArgs? e)
{Mat src = Cv2.ImRead(sourceImage);Mat dst = new Mat();Cv2.CvtColor(src, dst, ColorConversionCodes.BGR2GRAY);picResult.Image = CVUtility.Mat2Bitmap(dst);PicAutosize(picResult);
}

2.2.8 图片另存为

private void Save(object? sender, EventArgs? e)
{SaveFileDialog saveFileDialog = new SaveFileDialog();if (saveFileDialog.ShowDialog() == DialogResult.OK){picResult.Image.Save(saveFileDialog.FileName);MessageBox.Show("Image Save to " + saveFileDialog.FileName);}
}

2.3 运行效果

读取图片

转为灰度图 

图片另存为

2.4 完整的 Form1.cs 文件

using OpenCvSharp;#pragma warning disable CS8602namespace Legal.Truffer.CVStar
{public partial class Form1 : Form{string[] ImgExtentions = {"*.*|*.*","JPEG|*.jpg;*.jpeg","GIF|*.gif","PNG|*.png","TIF|*.tif;*.tiff","BMP|*.bmp"};private int original_width { get; set; } = 0;private int original_height { get; set; } = 0;private string sourceImage { get; set; } = "";Panel? panelTop { get; set; } = null;Panel? panelBotton { get; set; } = null;PictureBox? picSource { get; set; } = null;PictureBox? picResult { get; set; } = null;Button? btnLoad { get; set; } = null;Button? btnGray { get; set; } = null;Button? btnSave { get; set; } = null;public Form1(){InitializeComponent();this.Text = "OPENCV C#编程入手教程 POWERED BY 深度混淆(CSDN.NET)";this.StartPosition = FormStartPosition.CenterScreen;GUI();this.Resize += FormResize;}private void FormResize(object? sender, EventArgs? e){if (this.Width < 200) { this.Width = 320; return; }if (this.Height < 200) { this.Height = 320; return; }GUI();}private void GUI(){if (panelTop == null) panelTop = new Panel();panelTop.Parent = this;panelTop.Top = 5;panelTop.Left = 5;panelTop.Width = this.Width - 26;panelTop.Height = 55;panelTop.BorderStyle = BorderStyle.FixedSingle;if (panelBotton == null) panelBotton = new Panel();panelBotton.Parent = this;panelBotton.Top = panelTop.Top + panelTop.Height + 3;panelBotton.Left = 5;panelBotton.Width = panelTop.Width;panelBotton.Height = this.Height - panelBotton.Top - 55;panelBotton.BorderStyle = BorderStyle.FixedSingle;if (picSource == null) picSource = new PictureBox();picSource.Parent = panelBotton;picSource.Left = 5;picSource.Top = 5;picSource.Width = (panelBotton.Width - 10) / 2;picSource.Height = (panelBotton.Height - 10);picSource.BorderStyle = BorderStyle.FixedSingle;if (picResult == null) picResult = new PictureBox();picResult.Parent = panelBotton;picResult.Left = picSource.Left + picSource.Width + 5;picResult.Top = picSource.Top;picResult.Width = picSource.Width;picResult.Height = picSource.Height;picResult.BorderStyle = BorderStyle.FixedSingle;original_width = picSource.Width;original_height = picSource.Height;if (btnLoad == null) btnLoad = new Button();btnLoad.Parent = panelTop;btnLoad.Left = 5;btnLoad.Top = 5;btnLoad.Width = 90;btnLoad.Height = 38;btnLoad.Cursor = Cursors.Hand;btnLoad.Text = "Load";btnLoad.Click += Load_Image;if (btnGray == null) btnGray = new Button();btnGray.Parent = panelTop;btnGray.Left = btnLoad.Left + btnLoad.Width + 5;btnGray.Top = btnLoad.Top;btnGray.Width = 90;btnGray.Height = 38;btnGray.Cursor = Cursors.Hand;btnGray.Text = "Gray";btnGray.Click += ToGray;if (btnSave == null) btnSave = new Button();btnSave.Parent = panelTop;btnSave.Left = btnGray.Left + btnGray.Width + 5;btnSave.Top = btnLoad.Top;btnSave.Width = 90;btnSave.Height = 38;btnSave.Cursor = Cursors.Hand;btnSave.Text = "Save";btnSave.Click += Save;PicAutosize(picSource);PicAutosize(picResult);}private void Load_Image(object? sender, EventArgs? e){OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.Filter = String.Join("|", ImgExtentions);if (openFileDialog.ShowDialog() == DialogResult.OK){sourceImage = openFileDialog.FileName;picSource.Image = Image.FromFile(sourceImage);picResult.Image = picSource.Image;PicAutosize(picSource);PicAutosize(picResult);}}private void PicAutosize(PictureBox pb){if (pb == null) return;if (pb.Image == null) return;Image img = pb.Image;int w = original_width;int h = w * img.Height / img.Width;if (h > original_height){h = original_height;w = h * img.Width / img.Height;}pb.SizeMode = PictureBoxSizeMode.Zoom;pb.Width = w;pb.Height = h;pb.Image = img;pb.Refresh();}private void ToGray(object? sender, EventArgs? e){Mat src = Cv2.ImRead(sourceImage);Mat dst = new Mat();Cv2.CvtColor(src, dst, ColorConversionCodes.BGR2GRAY);picResult.Image = CVUtility.Mat2Bitmap(dst);PicAutosize(picResult);}private void Save(object? sender, EventArgs? e){SaveFileDialog saveFileDialog = new SaveFileDialog();saveFileDialog.Filter = String.Join("|", ImgExtentions);if (saveFileDialog.ShowDialog() == DialogResult.OK){picResult.Image.Save(saveFileDialog.FileName);MessageBox.Show("Image Save to " + saveFileDialog.FileName);}}}
}

本代码尽量为你着想,无需设计、修改 Form1.Design.cs 及资源文件。

用该文件替换原来的 Form1.cs 文件即可运行。

3 支持函数

一个基本的支持函数特意摘录出来,以后也需要用上。

using OpenCvSharp;
using OpenCvSharp.Extensions;public static partial class CVUtility
{/// <summary>/// Mat 转 Bitmap(32bits)/// </summary>/// <param name="img"></param>/// <returns></returns>public static Bitmap Mat2Bitmap(Mat img){//if (bytes == 32) return BitmapConverter.ToBitmap(img, PixelFormat.Format32bppArgb);//else if (bytes == 24) return BitmapConverter.ToBitmap(img, PixelFormat.Format24bppRgb);//else return BitmapConverter.ToBitmap(img);}
}

够详细吗?

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【数据结构】——解决topk问题

前言&#xff1a;我们前面已经学习了小堆并且也实现了小堆&#xff0c;那么我们如果要从多个数据里选出最大的几个数据该怎么办呢&#xff0c;这节课我们就来解决这个问题。我们就用建小堆的方法来解决。 首先我们来看到这个方法的时间复杂度&#xff0c;我们先取前k个数据建立…

c语言-数据在内存中的存储

文章目录 1. 整数在内存中的存储2. 大小端字节序和字节序判断3. 浮点数在内存中的存储 1. 整数在内存中的存储 1.整数的2进制表示方法有三种&#xff0c;即 原码、反码和补码 2. 三种表示方法均有符号位和数值位两部分&#xff0c;符号位都是用0表示“正”&#xff0c;用1表示“…

Offer来了:Java面试核心知识点精讲

系列文章目录 送书第一期 《用户画像&#xff1a;平台构建与业务实践》 送书活动之抽奖工具的打造 《获取博客评论用户抽取幸运中奖者》 送书第二期 《Spring Cloud Alibaba核心技术与实战案例》 送书第三期 《深入浅出Java虚拟机》 送书第四期 《AI时代项目经理成长之道》 …

vue3 setup语法糖,常用的几个:defineProps、defineEmits、defineExpose、

vue3和vue2组件之间传参的不同 <script setup> 是在单文件组件 (SFC) 中使用组合式 API 的编译时语法糖。 <script setup> 中的代码会在每次组件实例被创建的时候执行。 任何在 <script setup> 声明的顶层的绑定 (包括变量&#xff0c;函数声明&#xff0…

网站公安备案流程

1.公安备案网址 https://beian.mps.gov.cn/ 选择用户登录->法人用户登录 左边的码下载APP&#xff0c;登上去之后用APP扫右边的码&#xff0c;人脸识别

springboot实现邮箱发送功能

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 邮箱效果图一、pom配置二、页面编写三、配置yml四、邮件工具类五、测试发送 邮箱效果图 1.可以利用在出现问题进行邮箱提醒 2.编写html 用于在邮箱中展示的样式 提示…

基于IDEA+MySQL+Tomcat开发的高校毕业生就业信息管理系统

基于IDEAMySQLTomcat开发的高校毕业生就业信息管理系统 项目介绍&#x1f481;&#x1f3fb; 高校毕业生信息管理系统背景介绍 随着高等教育的发展和信息化技术的普及&#xff0c;高校毕业生信息管理面临着越来越多的挑战。为了解决这些问题&#xff0c;我们开发了高校毕业生信…

校园跑腿的核心功能

校园跑腿是指在校园内提供各种代办和服务的便利服务。 1. 快速送货&#xff1a;校园跑腿可以提供快速的送货服务&#xff0c;包括食品、快递、文件等物品的送达。 2. 打印复印&#xff1a;校园跑腿可以提供打印、复印等文档处理服务&#xff0c;方便学生和教职工处理各种文档…

C语言基础程序设计题

1.个人所得税计算 应纳税款的计算公式如下&#xff1a;收入<&#xff1d;1000元部分税率为0&#xff05;&#xff0c;2000元>&#xff1d;收入>1000元的部分税率为5&#xff05;&#xff0c;3000元>&#xff1d;收入>2000元的部分税率为10&#xff05;&#xf…

通用闪存存储(UFS)市场研究,预计2029年将达到1,930.99百万美元

UFS产品定义及统计范围 UFS &#xff08;Universal Flash Storage&#xff0c;通用闪存存储&#xff09;&#xff0c;UFS是一种高性能接口&#xff0c;设计用于需要最小化功耗的应用&#xff0c;包括智能手机和平板电脑等移动系统以及汽车应用&#xff0c;其高速串行接口和优化…

RocketMQ源码剖析之createUniqID方法

目录 版本信息&#xff1a; 写在前面&#xff1a; 源码剖析&#xff1a; 总计&#xff1a; 版本信息&#xff1a; RocketMQ-5.1.3 源码地址&#xff1a;https://github.com/apache/rocketmq 写在前面&#xff1a; 首先&#xff0c;笔者先吐槽一下RocketMQ的官方&#xff0…

AMIS【部署 01】amis前端低代码框架可视化编辑器amis-editor本地部署流程

amis-editor本地部署流程 1.amis-editor是什么1.1 amis是什么1.2 amis-editor是什么 2.amis-editor本地部署2.1 准备阶段2.2 源码修改2.3 构建项目2.4 nginx配置2.5 启动nginx 3.总结 官网仅贴出了本地运行这个项目的步骤&#xff1a; # 1.安装依赖 npm i # 2.等编译完成后本地…