当前位置: 首页 > 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下载

源码下载

相关文章:

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; us…...

centos7下安装elasticsearch7.8.1并配置远程连接

1、下载安装包 sudo wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.8.1-linux-x86_64.tar.gz 2、解压 sudo tar -zxvf elasticsearch-7.8.1-linux-x86_64.tar.gz 3、添加用户并设置密码 sudo useradd es sudo passwd es # 设置密码 Lida15…...

MongoDB的作用和安装方法

MongoDB是一种非关系型数据库&#xff0c;其作用是存储和管理非结构化数据&#xff0c;例如文档、图像和视频等多媒体数据。它有以下几个特点&#xff1a; 数据存储的格式是类似JSON的文档格式&#xff0c;易于理解、存储和查询。可扩展性强&#xff0c;可以在多个服务器上分布…...

spring boot 使用SSE向前端推送数据

SSE&#xff08;Server-Sent Events&#xff09;是一种基于HTTP的实时通信协议&#xff0c;它允许服务器向客户端发送持久性的数据流。与WebSocket不同的是&#xff0c;SSE是单向通信&#xff0c;只能由服务器向客户端发送数据。Spring Boot通过Spring WebFlux模块提供了对SSE的…...

C++智能指针(三)——unique_ptr初探

与共享指针shared_ptr用于共享对象的目的不同&#xff0c;unique_ptr是用于独享对象。 文章目录 1. unqiue_ptr的目的2. 使用 unique_ptr2.1 初始化 unique_ptr2.2 访问数据2.3 作为类的成员2.4 处理数组 3. 转移所有权3.1 简单语法3.2 函数间转移所有权3.2.1 转移至函数体内3.…...

Composition Api 与 Options Api 有什么区别?

Vue 3.0采用的Composition API与Vue 2.x使用的Options API在编写Vue组件时有一些区别。 区别&#xff1a; 组织代码的方式不同&#xff1a; Options API&#xff1a;按照选项进行组织&#xff0c;将数据、计算属性、方法等声明在一个对象中。Composition API&#xff1a;按照逻…...

紫光同创FPGA实现UDP协议栈网络视频传输,基于YT8511和RTL8211,提供4套PDS工程源码和技术支持

目录 1、前言免责声明 2、相关方案推荐我这里已有的以太网方案紫光同创FPGA精简版UDP方案紫光同创FPGA带ping功能UDP方案 3、设计思路框架OV7725摄像头配置及采集OV5640摄像头配置及采集UDP发送控制视频数据组包数据缓冲FIFOUDP协议栈详解RGMII转GMII动态ARPUDP协议IP地址、端口…...

深度学习简述

⭐️⭐️⭐️⭐️⭐️欢迎来到我的博客⭐️⭐️⭐️⭐️⭐️ 🐴作者:秋无之地 🐴简介:CSDN爬虫、后端、大数据领域创作者。目前从事python爬虫、后端和大数据等相关工作,主要擅长领域有:爬虫、后端、大数据开发、数据分析等。 🐴欢迎小伙伴们点赞👍🏻、收藏⭐️、…...

【从零开始学习Redis | 第二篇】Redis中的数据类型和相关命令

前言&#xff1a; Redis是一种快速、高效的开源内存数据库&#xff0c;被广泛用于构建各种类型的应用程序。其被设计成支持多种数据类型&#xff0c;这使得Redis在处理各种场景的数据存储和操作中非常灵活。Redis的数据类型提供了对不同数据结构的直接支持&#xff0c;包括字符…...

数据结构 - 3(链表12000字详解)

一&#xff1a;LinkedList的使用 1.1 ArrayList的缺陷 上篇文章我们已经基本熟悉了ArrayList的使用&#xff0c;并且进行了简单模拟实现。由于其底层是一段连续空间&#xff0c;当在ArrayList任意位置插入或者删除元素时&#xff0c;就需要将后序元素整体往前或者往后搬移&am…...

Jmeter性能测试插件jpgc的安装

一、获取插件包 1.访问官网获取 官网地址&#xff1a; 2.百度网盘下载 链接&#xff1a;百度网盘 请输入提取码 提取码&#xff1a;blmn 二、安装路径 将下载到的plugins-manager.jar插件存放到%JMETER_HOME%/lib/ext目录下 ​ 三、安装插件 1.重启Jmeter 如果已启动了…...

关于safari浏览器浏览html video标签无法正常播放的问题

问题&#xff1a; 前端demo使用一个video标签包含一个非静态资源的mp4文件。在chrome浏览器下可以正常展示&#xff0c;但是safari却不可以。 原因&#xff1a; 1. mp4文件必须用ffmpeg合成的&#xff0c;其他压缩的mp4文件是不可能展示的。请确定mp4文件并用正常的ffmpeg进…...

【C++代码】最大二叉树,合并二叉树,二叉搜索树中的搜索,验证二叉搜索树--代码随想录

题目&#xff1a;最大二叉树 给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建: 创建一个根节点&#xff0c;其值为 nums 中的最大值。递归地在最大值 左边 的 子数组前缀上 构建左子树。递归地在最大值 右边 的 子数组后缀上 构建右子树。 …...

母婴用品会员商城小程序的作用是什么

随着政策放松&#xff0c;母婴行业相比以前迎来了更高的发展空间&#xff0c;由于可以与多个行业连接&#xff0c;因此市场规模也是连年上升&#xff0c;母婴用品是行业重要的分支&#xff0c;近些年从业商家连年增加&#xff0c;但在实际经营中&#xff0c;商家所遇经营痛点也…...

c++初阶--内存管理

目录 c/c 内存分布c内存管理方式new/delete操作内置类型new和delete操作自定义类型 operator new与operator delete函数new和delete的实现原理内置类型自定义类型 malloc/free和new/delete的区别内存泄露什么是内存泄漏&#xff0c;内存泄露的危害如何避免内存泄漏 在c语言中我…...

Burstormer论文阅读笔记

这是CVPR2023的一篇连拍图像修复和增强的论文&#xff0c;一作是阿联酋的默罕默德 本 扎耶得人工智能大学&#xff0c;二作是旷视科技。这些作者和CVPR2022的一篇BIPNet&#xff0c;同样是做连拍图像修复和增强的&#xff0c;是同一批。也就是说同一个方向&#xff0c;22年中了…...

Apifox 学习笔记 - 前置操作之:动态更新请求体中的时间戳

Apifox 学习笔记 - 前置操作之&#xff1a;动态更新请求体中的时间戳 1. 在前置操作中添加一个&#xff1a;自定义脚本或公共脚本2. 定义我们所需的环境变量。3. 在请求参数中使用【时间戳】4. 检验参考资料 1. 在前置操作中添加一个&#xff1a;自定义脚本或公共脚本 2. 定义我…...

2023年9月 青少年软件编程等级考试Scratch二级真题

202309 青少年软件编程等级考试Scratch二级真题&#xff08;电子学会等级考试&#xff09; 试卷总分数&#xff1a;100分 试卷及格分&#xff1a;60 分 考试时长&#xff1a;60 分钟 第 1 题 点击绿旗&#xff0c;运行程序后&#xff0c;舞台上的图形是?( ) A&#xff1a;画…...

12.验证码以及付费代理

文章目录 一、验证码的处理1、验证码概述1、2 什么是图片验证码&#xff1f;1、2 验证码的作用1、3 图片验证码使用场景1、4 图片验证码的处理方案 2、图片在网页页面中的形式2、1 如何进行图片形式的转化 3、打码平台 二、代理的使用2、1 付费代理2、1、1 找付费代理服务站点2…...

使用Plotly可视化

显示项目受欢迎程度 改进图表 设置颜色&#xff0c;字体...

应用升级/灾备测试时使用guarantee 闪回点迅速回退

1.场景 应用要升级,当升级失败时,数据库回退到升级前. 要测试系统,测试完成后,数据库要回退到测试前。 相对于RMAN恢复需要很长时间&#xff0c; 数据库闪回只需要几分钟。 2.技术实现 数据库设置 2个db_recovery参数 创建guarantee闪回点&#xff0c;不需要开启数据库闪回。…...

家政维修平台实战20:权限设计

目录 1 获取工人信息2 搭建工人入口3 权限判断总结 目前我们已经搭建好了基础的用户体系&#xff0c;主要是分成几个表&#xff0c;用户表我们是记录用户的基础信息&#xff0c;包括手机、昵称、头像。而工人和员工各有各的表。那么就有一个问题&#xff0c;不同的角色&#xf…...

Python实现prophet 理论及参数优化

文章目录 Prophet理论及模型参数介绍Python代码完整实现prophet 添加外部数据进行模型优化 之前初步学习prophet的时候&#xff0c;写过一篇简单实现&#xff0c;后期随着对该模型的深入研究&#xff0c;本次记录涉及到prophet 的公式以及参数调优&#xff0c;从公式可以更直观…...

srs linux

下载编译运行 git clone https:///ossrs/srs.git ./configure --h265on make 编译完成后即可启动SRS # 启动 ./objs/srs -c conf/srs.conf # 查看日志 tail -n 30 -f ./objs/srs.log 开放端口 默认RTMP接收推流端口是1935&#xff0c;SRS管理页面端口是8080&#xff0c;可…...

《基于Apache Flink的流处理》笔记

思维导图 1-3 章 4-7章 8-11 章 参考资料 源码&#xff1a; https://github.com/streaming-with-flink 博客 https://flink.apache.org/bloghttps://www.ververica.com/blog 聚会及会议 https://flink-forward.orghttps://www.meetup.com/topics/apache-flink https://n…...

网络编程(UDP编程)

思维导图 UDP基础编程&#xff08;单播&#xff09; 1.流程图 服务器&#xff1a;短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...

蓝桥杯3498 01串的熵

问题描述 对于一个长度为 23333333的 01 串, 如果其信息熵为 11625907.5798&#xff0c; 且 0 出现次数比 1 少, 那么这个 01 串中 0 出现了多少次? #include<iostream> #include<cmath> using namespace std;int n 23333333;int main() {//枚举 0 出现的次数//因…...

蓝桥杯 冶炼金属

原题目链接 &#x1f527; 冶炼金属转换率推测题解 &#x1f4dc; 原题描述 小蓝有一个神奇的炉子用于将普通金属 O O O 冶炼成为一种特殊金属 X X X。这个炉子有一个属性叫转换率 V V V&#xff0c;是一个正整数&#xff0c;表示每 V V V 个普通金属 O O O 可以冶炼出 …...

CSS | transition 和 transform的用处和区别

省流总结&#xff1a; transform用于变换/变形&#xff0c;transition是动画控制器 transform 用来对元素进行变形&#xff0c;常见的操作如下&#xff0c;它是立即生效的样式变形属性。 旋转 rotate(角度deg)、平移 translateX(像素px)、缩放 scale(倍数)、倾斜 skewX(角度…...

uniapp 字符包含的相关方法

在uniapp中&#xff0c;如果你想检查一个字符串是否包含另一个子字符串&#xff0c;你可以使用JavaScript中的includes()方法或者indexOf()方法。这两种方法都可以达到目的&#xff0c;但它们在处理方式和返回值上有所不同。 使用includes()方法 includes()方法用于判断一个字…...