Click here to Skip to main content
15,908,842 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
how to remove the background of an image?
Posted

To answer your question in any meaningful way: we need to know exactly what you mean by "background."

For example: consider four different scenarios:

0. you have both an image with the background only, and an image with the content you wish to extract on that exact same background. That leads to another type of solution, which is probably much more achievable in simpler code.

However, if the second image, with the content you wish to extract, has had that content anti-aliased into the background: you are in some deep waters (see #3 below).

1. one image in which background is defined as one unique color: every pixel of that color is to be eliminated. simplest case. pretty easy to do.

A slightly more complex variation of this might be one source image for which you can say that every pixel that has any red color value in RBG color space is background to be removed.

And, with images in HSL, CMYK, or LAB color spaces: there could be variations on the above which may or may not be useful, depending very much on the specific source image.

2. one image with a sharply defined border color, unique to the border only, that defines a closed shape: your goal is to remove everything outside that sharply defined color border. more complex: you've got to discriminate, for every pixel, what is outside the border color.

Now imagine a further escalation of this scenario by having an image in which there are multiple closed shapes: and you remove everything outside the unique border color of all of them. even more complex.

And now, imagine another permutation of this: your goal is to remove the contents inside a closed shape in an image with some specific border color which is unique to the border.

3. one image, of, say, a human head, which is on a multi-color background, and which has been anti-aliased so that it appears to the viewer more clearly defined without "jaggies." much more complex; in fact, very complex.

Beyond these cases, you get into the very complex computer science area of shape recognition, and image extraction, where you can go very, very deep, yea even unto the Ph.D. level.

In the meantime, unless you are trying to do the simplest of these tasks ... #0 with no anti-aliasing in the second image, or, #1 ... I suggest you get GIMP or PhotoShop, and learn how to use their excellent tools for such tasks.
 
Share this answer
 
Comments
Santhosh Subramanian 20-Feb-12 5:22am    
in a photo except mine i have to subtract all the other things.............
Member 12214314 13-Feb-17 2:17am    
wish you could shear code.
Member 12934774 18-Mar-17 13:04pm    
That was great but can you share code on a tool used to do this like in quick selection tool or any could help save a life here
 
Share this answer
 
Dear Friend,

This is the link for you:-

http://www.oliveragustin.com/background-subtraction-in-c/[^]

But this will work for only one image in background.

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900