将照片背景设置为透明 [英] Set a Photo Background Transparent

查看:82
本文介绍了将照片背景设置为透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,在该项目中,我必须即时清除照片的背景.
我们将在需要照片的人的背面放置一个特定颜色(即黄色)的对象,并在调用摄像头拍摄照片"事件时,代码将自动清除黄色背景并将图片合并到另一个背景上图片.

代码如下:

<code>
Image i = Crop(pictureBox1.Image, rectWidth, rectHeight, rectX, rectY);
Bitmap img1 = new Bitmap(i);
Bitmap img2 = new Bitmap(pictureBox2.Image);

Color transparencyColor = img1.GetPixel(0, 0);
img1.MakeTransparent(img1.GetPixel(1, 1)); 
using (Graphics g = Graphics.FromImage(img2))
{
     using (Image skinImage = (Image)img1)
     {
          // draw image         
          g.DrawImage(skinImage, (img2.Width - img1.Width)/2,(img2.Height- img1.Height)/2,img1.Width, img1.Height);
     }
}
pictureBox2.Image = (Image)img2;
</code>



函数MakeTransparent并不是我尝试过的唯一方法,我已经尝试过发现几乎所有与使背景透明相关的内容,包括在每个像素上运行一个循环并根据需要更改其颜色.

代码正在做使背景色透明和合并的工作,但是我面临的主要问题是照片的曲线仍然显示黄点和实线,这非常糟糕.我不确定是否可以在此问题区域放置图像.

谁能告诉我如何完全清除背景颜色,以及如何使背景完全透明,平滑的边缘和深的曲线也是透明的. :

g.DrawImage(skinImage, (img2.Width - img1.Width)/2,(img2.Height- img1.Height)/2,img1.Width, img1.Height);



它看起来也像是调整了图像的大小(或您正在执行的任何操作.我可以对图像进行成像,这意味着将某些颜色压在一起并变成了不是透明颜色的新"颜色.透明,同时保持宽度和高度不变,然后看看会发生什么.

含义...首先,请尝试确切的示例;-):
http://msdn.microsoft.com/en-us/library/4zzst10b.aspx [ ^ ]

祝你好运!


这种方法行不通.如果您在图片查看器中打开黄色图像并放大放大,则会看到并非每个黄色像素的颜色都与其他每个黄色像素相同.您的代码假定每个黄色像素都是相同的.您必须有一个黄色像素+/-百分比才能确定黄色像素应该是什么的阈值.您正在将一系列颜色视为黄色.

有了这个阈值,您就可以逐像素扫描图像,并用真实的背景色像素(例如洋红色)替换它们,然后将其设置为透明键.

在文章针对傻瓜的图像处理"(不,serisouly!)中搜索有关如何在C#中执行类似操作的示例.


您尝试执行的操作通常称为chromakeying( 绿屏"或蓝屏").由于抗锯齿,圆角的边缘始终是一个问题.大多数用于执行此操作的程序都具有各种羽化"选项以降低边缘效果,即使是昂贵的程序也无法始终使其正常工作.

基本上,我要说的是,不要试图通过自动流程使其完美而使自己发疯……几乎是不可能的.寻找羽化"边缘或抗锯齿"功能以使您靠近.但是:必须在替换背景后完成,而不是在此之前.如果在新背景到位之前完成操作,它将无法如您所愿地工作.

使整个事情变得容易的提示:使用抠像背景而不是黄色.使背景尽可能远离主题.均匀(明亮)地照亮背景.在对象上使用边缘光(背光可以消除边缘阴影-如果可以对边缘光使用互补色,则更好).


I am working on a project, in which i have to clear the background of photo on the fly.
we will put an object of specific color (i.e. yellow) on the back of the person who''s photo is required and when the camera Take Photo event is called, the code will automaticaly clear the yellow background and merge the picture on another background image.

The code is given as under:

<code>
Image i = Crop(pictureBox1.Image, rectWidth, rectHeight, rectX, rectY);
Bitmap img1 = new Bitmap(i);
Bitmap img2 = new Bitmap(pictureBox2.Image);

Color transparencyColor = img1.GetPixel(0, 0);
img1.MakeTransparent(img1.GetPixel(1, 1)); 
using (Graphics g = Graphics.FromImage(img2))
{
     using (Image skinImage = (Image)img1)
     {
          // draw image         
          g.DrawImage(skinImage, (img2.Width - img1.Width)/2,(img2.Height- img1.Height)/2,img1.Width, img1.Height);
     }
}
pictureBox2.Image = (Image)img2;
</code>



The function MakeTransparent is not the only way i have tried, I have experimented almost everything i found related to make background transparent including running a loop on every pixel and changing its colour if required.

The code is doing the job to make background color transparent and merge is also ok but the main problem which i am facing is that the curves of the photo are still showing yellow dots and solid lines which is very bad. I am not sure if i can place an image in this question area.

Can any one tell me how to completely clear the background colour and make background fully transparent with smooth edges and deep curves are also transparent.

解决方案

The way you call the DrawImage:

g.DrawImage(skinImage, (img2.Width - img1.Width)/2,(img2.Height- img1.Height)/2,img1.Width, img1.Height);



It also looks like image is resized (or whatever you''re doing with it. I can image this means that some colors are pressed together and become a "new" color that is not the transparent color. I would suggest first just make it transparent while keeping width and height unchanged and see what happens then.

Meaning... first just try the exact example ;-) :
http://msdn.microsoft.com/en-us/library/4zzst10b.aspx[^]

Good luck!


This method isn''t going to work. If you open your yellow image in a picture viewer and zoom in close, you''ll see that not every Yellow pixel is identical in color to every other Yellow pixel. Your code assumes that every Yellow pixel is identical. You have to have a Yellow pixel +/- a percentage to establish a threshold of what a Yellow pixel should be. You''re making a range of colors be treated as Yellow.

One you have that threshold, you can scan the image, pixel-by-pixel, and replace them with your true background color pixel, say Magenta, then set that as the transparent key.

Search the articles for "Image processing for dummies" (no, serisouly!) for examples on how to do something like this in C#.


What you are trying to do is generally called chromakeying ("green screen" or "blue screen"). Rounded edges are always a problem because of antialiasing. Most programs used to do this have various sorts of "feathering" options to tone down the edge effects, and even the expensive ones can''t always get it to work nicely.

Basically, what I''m saying is don''t make yourself crazy trying to get it perfect with an automatic process...nearly impossible. Look for "feathering" edges or "antialiasing" functions to get you close. But: this has to be done after the background has been replaced, not before. It won''t work as you want if done before the new background is in place.

Hints to make the whole thing easier: use a chromakey background, not yellow. Have the background as far back from the subject as you can manage. Evenly (and brightly) light the background. Use a rim light on the subject (a backlight to get rid of edge shadowing--even better if you can use a complementary color for the rimlight).


这篇关于将照片背景设置为透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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