C# 证件照替换底色与设置背景图---PaddleSegSharp

本文主要是介绍C# 证件照替换底色与设置背景图---PaddleSegSharp,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

PaddleSegSharp 是一个基于百度飞桨PaddleSeg项目的人像分割模块而开发的.NET的工具类库。

PaddleSegSharp 中PaddleSeg.dll文件是基于开源项目最新发布版本PaddleSeg PaddleSeg的版本修改而成的C++动态库,基于opencv的x64编译而成的。

PaddleSeg是基于飞桨PaddlePaddle的端到端图像分割套件,内置45+模型算法及140+预训练模型,支持配置化驱动和API调用开发方式,打通数据标注、模型开发、训练、压缩、部署的全流程,提供语义分割、交互式分割、Matting、全景分割四大分割能力,助力算法在医疗、工业、遥感、娱乐等场景落地应用。

本项目只能在X64的CPU上编译和使用,只能在avx指令集上的CPU上使用,目前仅支持windows平台。

PaddleSegSharp 支持飞桨人像抠图的所有模型。PP-MattingV2-512、PP-Matting-512、PP-Matting-1024、PP-HumanMatting、MODNet-MobileNetV2。 默认使用MODNet-MobileNetV2模型。

1、新建winform项目

2、添加引用PaddleSegSharp ,使用NuGet 搜索添加PaddleSegSharp 

3、窗体上添加图片显示控件以及按钮

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PaddleSegSharp;namespace WindowsFormzhengjian
{public partial class Form1 : Form{#region 字段private string[] bmpFilters = new string[] { ".bmp", ".jpg", ".jpeg", ".tiff", ".tif", ".png" };private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";private PaddleSegMattingEngine engine;MattingParameter parameter;string modelPath = null;string path = Environment.CurrentDirectory + @"\out\";/// <summary>/// 创建画布的画板背景/// </summary>Bitmap backgroundImage = null;#endregionpublic Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){//初始化 引擎engine = new PaddleSegMattingEngine();//参数parameter = new MattingParameter();//parameter.outbgfile = true;//输出mask图//parameter.bgtransparent = true;//背景透明engine.Init(modelPath, parameter);if (!Directory.Exists(path)){Directory.CreateDirectory(path);}}/// <summary>/// 选择图片/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnSelectPic_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() == DialogResult.OK){//pictureBox1.Image = new Bitmap(ofd.FileName);using (FileStream ms = File.OpenRead(ofd.FileName)){//解决内存异常问题,以及this.pbx.BackgroundImage = System.Drawing.Image.FromStream(ms)导致的一般性gdi+ 问题using (Bitmap bt = new Bitmap(ms)){#region MyRegion//backgroundImage = new Bitmap(bt.Width, bt.Height);Graphics g = pbx.CreateGraphics();//Graphics g = Graphics.FromImage(backgroundImage);g.DrawLine(Pens.Black, startPoint, e.Location);g.Clear(Color.White);Pen myPen = new Pen(Color.Black, int.TryParse(ConfigurationManager.AppSettings["penwidth"], out int penwidth) ? penwidth : 3);//g.SmoothingMode = SmoothingMode.AntiAlias;//g.CompositingQuality = CompositingQuality.HighQuality;//g.CompositingMode = CompositingMode.SourceOver;//g.DrawImage(bt, bt.Width, bt.Height);PointF pointFstart = PointToPointF(startPoint);PointF pointFend = PointToPointF(e.Location);g.DrawBeziers(myPen, new PointF[] { pointFstart, pointFend });//pbx.BackgroundImage = backgroundImage;//g.Dispose();bt.Save("Fingerprint.jpg");this.pbx.BackgroundImage = System.Drawing.Image.FromStream(ms); #endregionbackgroundImage = new Bitmap(bt.Width, bt.Height, bt.PixelFormat);Graphics g = Graphics.FromImage(backgroundImage);g.SmoothingMode = SmoothingMode.AntiAlias;g.CompositingQuality = CompositingQuality.HighQuality;g.CompositingMode = CompositingMode.SourceOver;g.DrawImage(bt, 0, 0);backgroundImage.Save("Fingerprint11.jpg");//pictureBox1.Image = Image.FromHbitmap(backgroundImage.GetHbitmap());pictureBox1.Image = backgroundImage;g.Dispose();pictureBox1.Refresh();}}#region ok参数//MattingParameter parameter = new MattingParameter();初始化引擎//PaddleSegMattingEngine engine = new PaddleSegMattingEngine();//engine.Init(modelPath, parameter);设置背景颜色//engine.Setbackground(45, 145, 255);分割后的文件//string outfile = Guid.NewGuid().ToString() + ".png";//string outbgfile = Path.GetFileNameWithoutExtension(outfile) + "_bg.png";//engine.Seg(ofd.FileName, outfile, outbgfile); #endregion}this.Refresh();}/// <summary>/// 替换背景色/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnReplaceBackground_Click(object sender, EventArgs e){if (pictureBox1.Image != null){参数ok//MattingParameter parameter = new MattingParameter();初始化引擎//PaddleSegMattingEngine engine = new PaddleSegMattingEngine();//engine.Init(modelPath, parameter);设置背景颜色//Color bgcolor = Color.White;//ColorDialog colorDialog = new ColorDialog();//colorDialog.Color = bgcolor;//if (colorDialog.ShowDialog() != DialogResult.OK) return;//bgcolor = colorDialog.Color;//engine.Setbackground(bgcolor.R, bgcolor.G, bgcolor.B);engine.Setbackground(45, 145, 255);分割后的文件//string outfile = Guid.NewGuid().ToString() + ".png";//string outbgfile = Path.GetFileNameWithoutExtension(outfile) + "_bg.png";engine.Seg("小一寸.png", outfile, outbgfile);engine.Seg(new Bitmap("小一寸.png"), outfile, outbgfile);//engine.Seg(pictureBox1.Image, outfile, outbgfile);engine = new PaddleSegMattingEngine();engine.Init(modelPath, parameter);Color bgcolor = Color.White;ColorDialog colorDialog = new ColorDialog();colorDialog.Color = bgcolor;if (colorDialog.ShowDialog() != DialogResult.OK) return;bgcolor = colorDialog.Color;engine.Setbackground(bgcolor.R, bgcolor.G, bgcolor.B);//engine.SetbackgroundFile("");string outfile = path + Guid.NewGuid().ToString() + ".png";string outbgfile = path + Path.GetFileNameWithoutExtension(outfile) + "_bg.png";engine.Seg(pictureBox1.Image, outfile, outbgfile);//engine.Setbackground(45, 145, 255);分割后的文件//string outfile = "C:\\" + Guid.NewGuid().ToString() + ".bmp";//engine.Seg("C:\\Users\\Administrator\\Desktop\\1.jpg", outfile, "mask图路径");pictureBox2.Image = new Bitmap(outfile);}}/// <summary>/// 替换背景图/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnSetBackground_Click(object sender, EventArgs e){if (pictureBox1.Image != null){engine = new PaddleSegMattingEngine();engine.Init(modelPath, parameter);OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK)return;engine.SetbackgroundFile(ofd.FileName);string outfile = path + Guid.NewGuid().ToString() + ".png";string outbgfile = path + Path.GetFileNameWithoutExtension(outfile) + "_bg.png";//string outfile = path + "1.png";//string outbgfile = path + "1_bg.png";engine.Seg(pictureBox1.Image, outfile, outbgfile);pictureBox2.Image = new Bitmap(outfile);}}private void btnSave_Click(object sender, EventArgs e){if (pictureBox2.Image != null){SaveFileDialog fileDialog = new SaveFileDialog();fileDialog.Filter = "Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png|" +"JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF files (*.gif)|*.gif |BMP files (*.b" +"mp)|*.bmp|Tiff files (*.tif;*.tiff)|*.tif;*.tiff|Png files (*.png)| *.png |All f" +"iles (*.*)|*.*";if ((fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)){string path = fileDialog.FileName.ToString();pictureBox2.Image.Save(path);}}}private void btnOCR_Click(object sender, EventArgs e){//OpenFileDialog ofd = new OpenFileDialog();//ofd.Filter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";//if (ofd.ShowDialog() != DialogResult.OK)//    return;//var imagebyte = File.ReadAllBytes(ofd.FileName);//Bitmap bitmap = new Bitmap(new MemoryStream(imagebyte));//OCRModelConfig config = null;//OCRParameter oCRParameter = new OCRParameter();//OCRResult ocrResult = new OCRResult();//using (PaddleOCREngineengine = new PaddleOCREngine(config, oCRParameter))//{//    ocrResult = engine.DetectText(bitmap);//}//if (ocrResult != null)//{//    MessageBox.Show(ocrResult.Text, "识别结果");//}}}
}

这篇关于C# 证件照替换底色与设置背景图---PaddleSegSharp的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1014472

相关文章

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

grom设置全局日志实现执行并打印sql语句

《grom设置全局日志实现执行并打印sql语句》本文主要介绍了grom设置全局日志实现执行并打印sql语句,包括设置日志级别、实现自定义Logger接口以及如何使用GORM的默认logger,通过这些... 目录gorm中的自定义日志gorm中日志的其他操作日志级别Debug自定义 Loggergorm中的

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...

MySQL8.0设置redo缓存大小的实现

《MySQL8.0设置redo缓存大小的实现》本文主要在MySQL8.0.30及之后版本中使用innodb_redo_log_capacity参数在线更改redo缓存文件大小,下面就来介绍一下,具有一... mysql 8.0.30及之后版本可以使用innodb_redo_log_capacity参数来更改

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

Windows设置nginx启动端口的方法

《Windows设置nginx启动端口的方法》在服务器配置与开发过程中,nginx作为一款高效的HTTP和反向代理服务器,被广泛应用,而在Windows系统中,合理设置nginx的启动端口,是确保其正... 目录一、为什么要设置 nginx 启动端口二、设置步骤三、常见问题及解决一、为什么要设置 nginx