Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can detect the background of an image programmatically?

I need to develop a software that selects a face from a photo where the background is a plain color (green, like in the movies).

Then we want to compose that selection with another background image, this part is easy with many libraries. But I don't know how can I do the selection? Can you give some links or libraries to investigate? I can do this project with any language of my choose, so examples or links in any language are welcome.

like image 669
Castro Avatar asked Sep 07 '10 20:09

Castro


2 Answers

Ok, what you are trying to do is called chroma key. Like you say, it used a lot in the movies with a blue/green screen. On windows its actually pretty easy to do because its built into windows as part of GDI+ (or on C#, I think its just called the Graphics class).

I dont have any sample code handy, but the process is pretty straight forward:

With GDI+, you create a bitmap object of your foreground image (the one with the green background). Then create an ImageAttributes object. Use ImageAttribute's object's SetColorKey() method to specify a color or range of colors to use as the background color. Lastly, draw that bitmap object over the target bitmap, and GDI+ will draw it as if the background color is transparent.

There's more to it in that in code, but concept-wise thats all there is to it.

like image 76
GrandmasterB Avatar answered Nov 11 '22 18:11

GrandmasterB


This is probably an area where it is easier to work in some other space than RGB - such as HSV.

I would also look at the OpenCV library.

like image 1
Martin Beckett Avatar answered Nov 11 '22 18:11

Martin Beckett