效果
说明
背景去除是图像处理中的一个经典难题。从早期的颜色键控、GrabCut,到如今基于深度学习的分割模型,技术的演进让抠图这件事变得越来越智能。而在众多背景去除方案中,BRIA AI 开源的 RMBG-2.0 凭借其极高的精度和良好的工程化能力,成为当前这一领域备受关注的选择。
本文将介绍如何在 C# 中使用 ONNX Runtime 部署 RMBG-2.0 模型,实现高精度的图像背景去除,输出带透明通道的 PNG 图片。全文包含完整的代码实现,适合希望将 AI 抠图能力集成到 .NET 应用中的开发者参考。
RMBG-2.0 是什么
RMBG-2.0(Remove Background 2.0)是 BRIA AI 推出的新一代图像背景移除模型,基于 BiRefNet(Bilateral Reference Network,双边参考网络)架构设计。相比前代 RMBG v1.4,准确率从 73.94% 大幅提升至 90.14%,超越了不少商业付费工具
BiRefNet 架构的核心思想
传统图像分割模型往往把任务简化为“给每个像素打标签”——是前景就是 1,是背景就是 0。这种单向分类思维在处理发丝、纱巾、玻璃反光等边界模糊区域时,天然存在歧义。
BiRefNet 换了一种思路:它不直接预测分割图,而是构建一个双向参考系统——前景参考分支聚焦于主体内部结构,背景参考分支同步分析周围环境,两个分支在特征空间进行交叉调制,共同完成高保真分割。
具体来说,BiRefNet 由两个核心模块构成:定位模块(Localization Module, LM) 负责理解全局语义、生成语义图;恢复模块(Restoration Module, RM) 负责边缘细节的精修,通过边缘感知注意力机制实现对发丝级细节的精细处理。
这种设计使得 RMBG-2.0 在保持计算效率的同时,能够精准处理复杂边缘。
项目
模型信息
Model Properties
-------------------------
---------------------------------------------------------------
Inputs
-------------------------
name:pixel_values
tensor:Float[1, 3, -1, -1]
---------------------------------------------------------------
Outputs
-------------------------
name:alphas
tensor:Float[1, 1, -1, -1]
---------------------------------------------------------------
代码
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;
namespace Onnx_Demo
{
public partial class Form1 : Form
{
public Form1()
{
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; // 原始图像(BGR)
Mat result_image_with_alpha; // 最终带有透明背景的图像
SessionOptions options;
InferenceSession onnx_session;
Tensor<float> input_tensor;
List<NamedOnnxValue> input_container;
IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
int inpHeight, inpWidth;
// RMBG-2.0 预处理参数
private readonlyfloat[] mean = new float[] { 0.485f, 0.456f, 0.406f };
private readonlyfloat[] std = new float[] { 0.229f, 0.224f, 0.225f };
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null;
image_path = ofd.FileName;
pictureBox1.Image = new Bitmap(image_path);
textBox1.Text = "";
image = new Mat(image_path);
pictureBox2.Image = null;
}
private void button2_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
button2.Enabled = false;
pictureBox2.Image = null;
textBox1.Text = "";
Application.DoEvents();
// 读取原始图像(BGR)
image = new Mat(image_path);
int originalWidth = image.Cols;
int originalHeight = image.Rows;
// ------------------ 预处理 ------------------
// 1. 转换为RGB
Mat rgb = new Mat();
Cv2.CvtColor(image, rgb, ColorConversionCodes.BGR2RGB);
// 2. Resize到模型输入尺寸(1024x1024)
Mat resized = new Mat();
Cv2.Resize(rgb, resized, new OpenCvSharp.Size(inpWidth, inpHeight));
// 3. 转换为浮点并归一化到 [0,1]
resized.ConvertTo(resized, MatType.CV_32FC3, 1.0 / 255.0);
// 4. 分离通道并应用标准化 (value - mean) / std
Mat[] channels = Cv2.Split(resized); // R, G, B
float[] inputData = new float[3 * inpHeight * inpWidth];
int channelSize = inpHeight * inpWidth;
for (int c = 0; c < 3; c++)
{
float[] channelData = new float[channelSize];
System.Runtime.InteropServices.Marshal.Copy(channels[c].Data, channelData, 0, channelSize);
// 标准化每个通道
for (int i = 0; i < channelSize; i++)
{
channelData[i] = (channelData[i] - mean[c]) / std[c];
}
Array.Copy(channelData, 0, inputData, c * channelSize, channelSize);
}
// 创建输入张量 (1, 3, H, W)
input_tensor = new DenseTensor<float>(inputData, new[] { 1, 3, inpHeight, inpWidth });
// 将输入放入容器(使用正确的输入名称 pixel_values)
input_container.Clear();
input_container.Add(NamedOnnxValue.CreateFromTensor("pixel_values", input_tensor));
// ------------------ 推理 ------------------
dt1 = DateTime.Now;
result_infer = onnx_session.Run(input_container);
dt2 = DateTime.Now;
// 按输出名称 alphas 获取结果
var output = result_infer.First(x => x.Name == "alphas").AsTensor<float>();
int[] outShape = output.Dimensions.ToArray();
int outH = outShape[2];
int outW = outShape[3];
float[] predFloat = output.ToArray().Select(x => (float)x).ToArray();
// 创建单通道 Mat(Alpha 蒙版,值域 [0,1])
Mat alphaMat = new Mat(outH, outW, MatType.CV_32FC1, predFloat);
// ------------------ 后处理 ------------------
// 1. 双线性插值到原始尺寸
Mat maskResized = new Mat();
Cv2.Resize(alphaMat, maskResized, new OpenCvSharp.Size(originalWidth, originalHeight), interpolation: InterpolationFlags.Linear);
// 2. 确保值在 [0,1] 范围内(sigmoid输出理论如此,防止溢出)
Cv2.Threshold(maskResized, maskResized, 1.0, 1.0, ThresholdTypes.Trunc);
Cv2.Threshold(maskResized, maskResized, 0.0, 0.0, ThresholdTypes.Tozero);
// 3. 转换为8位单通道(alpha 0~255)
Mat alphaMask = new Mat();
maskResized.ConvertTo(alphaMask, MatType.CV_8UC1, 255.0);
// ------------------ 合成透明背景图像 ------------------
// 原始图像(BGR)转为 BGRA
Mat rgba = new Mat();
Cv2.CvtColor(image, rgba, ColorConversionCodes.BGR2BGRA);
// 替换 alpha 通道
Mat[] bgraChannels = Cv2.Split(rgba);
bgraChannels[3] = alphaMask;
Cv2.Merge(bgraChannels, rgba);
result_image_with_alpha = rgba.Clone();
// 显示最终图像(PictureBox 支持透明通道显示)
pictureBox2.Image = new Bitmap(rgba.ToMemoryStream());
textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
button2.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
startupPath = System.Windows.Forms.Application.StartupPath;
model_path = "model/model.onnx"; // 请确保模型文件存在于此路径
// 创建会话,使用 CPU(可根据需要改为 CUDA)
options = new SessionOptions();
options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
options.AppendExecutionProvider_CPU(0);
onnx_session = new InferenceSession(model_path, options);
input_container = new List<NamedOnnxValue>();
// 模型固定输入尺寸
inpHeight = 1024;
inpWidth = 1024;
// 测试图片路径(可选)
image_path = "test_img/1.jpg";
if (System.IO.File.Exists(image_path))
{
pictureBox1.Image = new Bitmap(image_path);
image = new Mat(image_path);
}
}
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
Common.ShowNormalImg(pictureBox1.Image);
}
private void pictureBox2_DoubleClick(object sender, EventArgs e)
{
Common.ShowNormalImg(pictureBox2.Image);
}
SaveFileDialog sdf = new SaveFileDialog();
private void button3_Click(object sender, EventArgs e)
{
if (result_image_with_alpha == null || result_image_with_alpha.Empty())
{
MessageBox.Show("请先进行推理!");
return;
}
sdf.Title = "保存透明背景图片";
sdf.Filter = "PNG图片 (*.png)|*.png|JPEG图片 (*.jpg)|*.jpg|BMP图片 (*.bmp)|*.bmp";
sdf.FilterIndex = 1; // 默认 PNG,保留透明度
if (sdf.ShowDialog() == DialogResult.OK)
{
string ext = System.IO.Path.GetExtension(sdf.FileName).ToLower();
ImageFormat format = ImageFormat.Png;
if (ext == ".jpg" || ext == ".jpeg")
format = ImageFormat.Jpeg;
elseif (ext == ".bmp")
format = ImageFormat.Bmp;
using (var stream = result_image_with_alpha.ToMemoryStream())
using (var bitmap = new Bitmap(stream))
{
bitmap.Save(sdf.FileName, format);
}
MessageBox.Show("保存成功,位置:" + sdf.FileName);
}
}
}
}
模型下载
通过网盘分享的文件:RMBG-2.0 模型 链接: pan.baidu.com/s/12pA3YBoD… 提取码: tqj7