c#.net绿屏背景删除 [英] c# .NET green screen background remove

查看:74
本文介绍了c#.net绿屏背景删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发适用于Windows 8的台式PC的照片软件.我希望能够通过色度键消除照片中的绿色背景.

I am working on a photo software for desktop PC that works on Windows 8. I would like to be able to remove the green background from the photo by means of chroma keying.

我是图像处理的初学者,我发现了一些很酷的链接(例如 http://www.quasimondo.com/archives/000615.php ),但我无法使用C#代码进行销售.

I'm a beginner in image manipulation, i found some cool links ( like http://www.quasimondo.com/archives/000615.php ), but I can't transale it in c# code.

我正在使用网络摄像头(具有 aforge.net )来预览和拍张照片.我尝试使用滤色镜,但是绿色背景并不是真的均匀,所以这行不通.

I'm using a webcam (with aforge.net) to see a preview and take a picture. I tried color filters but the green background isn't really uniform, so this doesn't work.

如何在C#中正确执行此操作?

How to do that properly in C#?

推荐答案

它会起作用,即使背景不均匀,您也只需要足够大的适当策略即可捕获所有绿屏而无需更换还有什么.

It will work, even if the background isn't uniform, you'll just need the proper strategy that is generous enough to grab all of your greenscreen without replacing anything else.

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

Since at least some links on your linked page are dead, I tried my own approach:

  • 基础很简单:将图像像素的颜色与某个参考值进行比较,或者应用其他公式来确定是否应该透明/替换.

  • The basics are simple: Compare the image pixel's color with some reference value or apply some other formula to determine whether it should be transparent/replaced.

最基本的公式将涉及诸如确定绿色是否为最大价值"之类的简单操作.虽然这适用于非常基本的场景,但它可能会使您感到困惑(例如,白色或灰色也会被滤除).

The most basic formula would involve something as simple as "determine whether green is the biggest value". While this would work with very basic scenes, it can screw you up (e.g. white or gray will be filtered as well).

我使用一些简单的示例代码开玩笑了.当我使用Windows窗体时,它应该是可移植的,不会出现问题,并且我很确定您将能够解释该代码.请注意,这不一定是执行此操作最有效的方法.

I've toyed around a bit using some simple sample code. While I used Windows Forms, it should be portable without problems and I'm pretty sure you'll be able to interpret the code. Just note that this isn't necessarily the most performant way to do this.

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);
    }
}

我使用了来自Wikipedia的示例图片,并得到了以下结果:

I've used an example image from Wikipedia and got the following result:

请注意,您可能需要不同的阈值(以上代码中的 8 96 ),您甚至可能想要使用其他术语来确定是否应将某些像素被替换.您还可以在帧之间添加平滑化,混合(绿色差异较小)等,以减少硬边缘.

Just note that you might need different thresholds (8 and 96 in my code above), you might even want to use a different term to determine whether some pixel should be replaced. You can also add smoothening between frames, blending (where there's less green difference), etc. to reduce the hard edges as well.

这篇关于c#.net绿屏背景删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆