gpt4 book ai didi

c# .NET 绿屏背景移除

转载 作者:太空狗 更新时间:2023-10-30 00:24:53 27 4
gpt4 key购买 nike

我正在开发适用于 Windows 8 的桌面 PC 照片软件。我希望能够通过色度键控从照片中删除绿色背景。

我是图像处理的初学者,我发现了一些很酷的链接(如 http://www.quasimondo.com/archives/000615.php ),但我无法在 C# 代码中转售它。

我正在使用网络摄像头(使用 aforge.net )查看预览并拍照。我尝试了滤色器,但绿色背景不是很均匀,所以这不起作用。

如何在 C# 中正确地做到这一点?

最佳答案

它会起作用,即使背景不均匀,您只需要适当的策略,该策略足够大以在不替换任何其他东西的情况下获取所有绿屏。

由于您的链接页面上至少有一些链接已失效,我尝试了自己的方法:

  • 基础知识很简单:将图像像素的颜色与某个引用值进行比较或应用一些其他公式来确定它是否应该透明/替换。

  • 最基本的公式会涉及像“确定绿色是否是最大值”这样简单的事情。虽然这适用于非常基本的场景,但它可能会把你搞砸(例如,白色或灰色也会被过滤)。

我已经尝试使用一些简单的示例代码。虽然我使用的是 Windows 窗体,但它应该可以毫无问题地移植,而且我很确定您将能够解释代码。请注意,这不一定是执行此操作的最佳方式。

Bitmap input = new Bitmap(@"G:\Greenbox.jpg");

Bitmap output = new Bitmap(input.Width, input.Height);

// Iterate over all piels from top to bottom...
for (int y = 0; y < output.Height; y++)
{
// ...and from left to right
for (int x = 0; x < output.Width; x++)
{
// Determine the pixel color
Color camColor = input.GetPixel(x, y);

// Every component (red, green, and blue) can have a value from 0 to 255, so determine the extremes
byte max = Math.Max(Math.Max(camColor.R, camColor.G), camColor.B);
byte min = Math.Min(Math.Min(camColor.R, camColor.G), camColor.B);

// Should the pixel be masked/replaced?
bool replace =
camColor.G != min // green is not the smallest value
&& (camColor.G == max // green is the biggest value
|| max - camColor.G < 8) // or at least almost the biggest value
&& (max - min) > 96; // minimum difference between smallest/biggest value (avoid grays)

if (replace)
camColor = Color.Magenta;

// Set the output pixel
output.SetPixel(x, y, camColor);
}
}

我用过 example image from Wikipedia得到如下结果:

Masked result (magenta would be replaced by your background)

请注意,您可能需要不同的阈值(我上面的代码中的 896),您甚至可能想使用不同的术语来确定某个像素是否应该被更换。您还可以在帧之间添加平滑、混合(绿色差异较小的地方)等,以减少硬边。

关于c# .NET 绿屏背景移除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20901093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com