C# 证件照替换底色、设置背景图 您所在的位置:网站首页 证件照背景图怎么样更换 C# 证件照替换底色、设置背景图

C# 证件照替换底色、设置背景图

2024-03-08 07:12| 来源: 网络整理| 查看: 265

目录

软件说明

软件目录

软件界面

测试效果

代码

下载 

软件说明

基于以下PaddleSegSharp开源项目,做了再次封装

PaddleSegSharp地址:https://gitee.com/raoyutian/PaddleSegSharp

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

自带C++相关动态库;

支持非纯色背景的图片; 

软件目录

软件界面

测试效果

代码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using PaddleSegSharp; using System.IO;

namespace PaddleSegSharpDemo {     public partial class frmMain : Form     {         public frmMain()         {             InitializeComponent();         }

        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\";

        private void btnSelect_Click(object sender, EventArgs e)         {             OpenFileDialog ofd = new OpenFileDialog();             ofd.Filter = fileFilter;             if (ofd.ShowDialog() == DialogResult.OK)             {                 pictureBox1.Image = new Bitmap(ofd.FileName);             }         }

        private void frmMain_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);             }

        }

        ///         /// 替换背景色         ///         ///         ///         private void button1_Click(object sender, EventArgs e)         {

            if (pictureBox1.Image != null)             {                 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";

                //string outfile = path  + "1.png";                 //string outbgfile = path + "1_bg.png";

                engine.Seg(pictureBox1.Image, outfile, outbgfile);

                pictureBox2.Image = new Bitmap(outfile);             }         }

        ///         /// 设置背景图         ///         ///         ///         private void button2_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 button3_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);                 }             }

        }     } }  

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using PaddleSegSharp; using System.IO; namespace PaddleSegSharpDemo { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } 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\"; private void btnSelect_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = fileFilter; if (ofd.ShowDialog() == DialogResult.OK) { pictureBox1.Image = new Bitmap(ofd.FileName); } } private void frmMain_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); } } /// /// 替换背景色 /// /// /// private void button1_Click(object sender, EventArgs e) { if (pictureBox1.Image != null) { 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"; //string outfile = path + "1.png"; //string outbgfile = path + "1_bg.png"; engine.Seg(pictureBox1.Image, outfile, outbgfile); pictureBox2.Image = new Bitmap(outfile); } } /// /// 设置背景图 /// /// /// private void button2_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 button3_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); } } } } } 下载 

软件下载

源码下载



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有