当前位置: 首页 > news >正文

C# PortraitModeFilter (人物图片)背景模糊

效果

项目

代码

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;
using UMapx.Core;
using UMapx.Imaging;namespace PortraitModeFilter_模糊背景
{public partial class frmMain : Form{public frmMain(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";string startupPath;DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;string model_path;Mat image;Bitmap src;Bitmap mask;int modelSize = 512;SessionOptions options;InferenceSession onnx_session;Tensor<byte> input_tensor;List<NamedOnnxValue> input_ontainer;IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;DisposableNamedOnnxValue[] results_onnxvalue;Tensor<long> result_tensors;long[] result_array;double strength = 1;// 0 到 1OpenFileDialog ofd;private void button1_Click(object sender, EventArgs e){if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;image_path = ofd.FileName;src = new Bitmap(image_path);pictureBox1.Image = src;textBox1.Text = "";image = new Mat(image_path);pictureBox2.Image = null;trackBar1.Enabled = false;}private void Form2_Load(object sender, EventArgs e){trackBar1.Enabled = false;startupPath = Application.StartupPath;ofd = new OpenFileDialog();ofd.Filter = fileFilter;model_path = startupPath + "\\deeplabv3_mnv2_pascal_train_aug.onnx";//创建输出会话,用于输出模型读取信息options = new SessionOptions();options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;// 设置为CPU上运行options.AppendExecutionProvider_CPU(0);// 创建推理模型类,读取本地模型文件onnx_session = new InferenceSession(model_path, options);// 创建输入容器input_ontainer = new List<NamedOnnxValue>();// 创建输入容器input_ontainer = new List<NamedOnnxValue>();}private void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}textBox1.Text = "生成中……";pictureBox2.Image = null;Application.DoEvents();//缩放图片大小int oldwidth = image.Cols;int oldheight = image.Rows;int maxEdge = Math.Max(image.Rows, image.Cols);float ratio = 1.0f * modelSize / maxEdge;int newHeight = (int)(image.Rows * ratio);int newWidth = (int)(image.Cols * ratio);Mat resize_image = image.Resize(new OpenCvSharp.Size(newWidth, newHeight));input_tensor = new DenseTensor<byte>(new[] { 1, newHeight, newWidth, 3 });// 输入Tensorfor (int y = 0; y < resize_image.Height; y++){for (int x = 0; x < resize_image.Width; x++){input_tensor[0, y, x, 2] = resize_image.At<Vec3b>(y, x)[0];input_tensor[0, y, x, 1] = resize_image.At<Vec3b>(y, x)[1];input_tensor[0, y, x, 0] = resize_image.At<Vec3b>(y, x)[2];}}//将 input_tensor 放入一个输入参数的容器,并指定名称input_ontainer.Add(NamedOnnxValue.CreateFromTensor(onnx_session.InputNames[0].ToString(), input_tensor));dt1 = DateTime.Now;//运行 Inference 并获取结果result_infer = onnx_session.Run(input_ontainer);dt2 = DateTime.Now;//将输出结果转为DisposableNamedOnnxValue数组results_onnxvalue = result_infer.ToArray();//读取第一个节点输出并转为Tensor数据result_tensors = results_onnxvalue[0].AsTensor<Int64>();result_array = result_tensors.ToArray();//得到掩码图mask = SegmentationMap(result_array, newWidth, newHeight);mask = new Bitmap(mask, new System.Drawing.Size(oldwidth, oldheight));trackBar1.Enabled = true;//模糊背景pictureBox2.Image = Filter(src, mask);textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";}/// <summary>/// Converts an RGB tensor array to a color image./// </summary>/// <param name="tensor">RGBA tensor array</param>/// <param name="width">Bitmap width</param>/// <param name="height">Bitmap height</param>public unsafe Bitmap SegmentationMap(long[] tensor, int width, int height){Bitmap Data = new Bitmap(width, height);BitmapData bmData = Data.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);int stride = bmData.Stride;byte* p = (byte*)bmData.Scan0.ToPointer();int pos = 0;for (int j = 0; j < height; j++){int k, jstride = j * stride;for (int i = 0; i < width; i++, pos++){k = jstride + i * 3;var z = (tensor[pos] == 15) ? (byte)255 : (byte)0;// rgbp[k + 2] = z;p[k + 1] = z;p[k + 0] = z;}}Data.UnlockBits(bmData);return Data;}private void button3_Click(object sender, EventArgs e){if (pictureBox2.Image == null){return;}Bitmap output = new Bitmap(pictureBox2.Image);var sdf = new SaveFileDialog();sdf.Title = "保存";sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";if (sdf.ShowDialog() == DialogResult.OK){switch (sdf.FilterIndex){case 1:{output.Save(sdf.FileName, ImageFormat.Jpeg);break;}case 2:{output.Save(sdf.FileName, ImageFormat.Png);break;}case 3:{output.Save(sdf.FileName, ImageFormat.Bmp);break;}case 4:{output.Save(sdf.FileName, ImageFormat.Emf);break;}case 5:{output.Save(sdf.FileName, ImageFormat.Exif);break;}case 6:{output.Save(sdf.FileName, ImageFormat.Gif);break;}case 7:{output.Save(sdf.FileName, ImageFormat.Icon);break;}case 8:{output.Save(sdf.FileName, ImageFormat.Tiff);break;}case 9:{output.Save(sdf.FileName, ImageFormat.Wmf);break;}}MessageBox.Show("保存成功,位置:" + sdf.FileName);}}private void trackBar1_Scroll(object sender, EventArgs e){strength = trackBar1.Value / 100.0;label1.Text = $"Strenght: {strength}";pictureBox2.Image = Filter(src, mask);}BoxBlur _boxBlur = new BoxBlur();AlphaChannelFilter _alphaChannelFilter = new AlphaChannelFilter();Merge _merge = new Merge(0, 0, 255);Bitmap Filter(Bitmap image, Bitmap mask){int radius = (int)(strength * 2 * ((Math.Max(image.Height, image.Width) / 100) + 1));// deep person labvar alphaMask = (Bitmap)src.Clone();var portrait = (Bitmap)src.Clone();var segmentantionMask = (Bitmap)mask.Clone();// gaussian blur approximation_boxBlur.Size = new SizeInt(radius, radius);_boxBlur.Apply(portrait);_boxBlur.Apply(segmentantionMask);_boxBlur.Size = new SizeInt(radius / 2, radius / 2);_boxBlur.Apply(portrait);_boxBlur.Apply(segmentantionMask);// merging images_alphaChannelFilter.Apply(alphaMask, segmentantionMask);_merge.Apply(portrait, alphaMask);alphaMask.Dispose();segmentantionMask.Dispose();return portrait;}}
}

下载

可执行程序exe下载

源码下载

http://www.lryc.cn/news/192599.html

相关文章:

  • centos7下安装elasticsearch7.8.1并配置远程连接
  • MongoDB的作用和安装方法
  • spring boot 使用SSE向前端推送数据
  • C++智能指针(三)——unique_ptr初探
  • Composition Api 与 Options Api 有什么区别?
  • 紫光同创FPGA实现UDP协议栈网络视频传输,基于YT8511和RTL8211,提供4套PDS工程源码和技术支持
  • 深度学习简述
  • 【从零开始学习Redis | 第二篇】Redis中的数据类型和相关命令
  • 数据结构 - 3(链表12000字详解)
  • Jmeter性能测试插件jpgc的安装
  • 关于safari浏览器浏览html video标签无法正常播放的问题
  • 【C++代码】最大二叉树,合并二叉树,二叉搜索树中的搜索,验证二叉搜索树--代码随想录
  • 母婴用品会员商城小程序的作用是什么
  • c++初阶--内存管理
  • Burstormer论文阅读笔记
  • Apifox 学习笔记 - 前置操作之:动态更新请求体中的时间戳
  • 2023年9月 青少年软件编程等级考试Scratch二级真题
  • 12.验证码以及付费代理
  • 使用Plotly可视化
  • 【C语言】结构体、位段、枚举、联合(共用体)
  • “Python+”集成技术高光谱遥感数据处理与机器学习深度应用
  • Excel 转为 PDF,PNG,HTML等文件
  • docker中使用GPU+rocksdb
  • 好用的跨平台同步笔记工具,手机和电脑可同步的笔记工具
  • 【Python 千题 —— 基础篇】浮点数转换为整数
  • 金融科技论文D部分
  • Apache Tomcat下载安装配置使用超详细
  • 基于Seata的分布式事务方案
  • 指令跳转:原来if...else就是goto
  • 【数据库系统概论】第四章数据库安全性