Reputation: 547
If one is creating an application in which one would like to make the "background" of an image transparent or white, is there a way to do this?
For example :
In the image, http://upload.wikimedia.org/wikipedia/commons/b/b9/Bronze_Statuette_of_a_Veiled_and_Masked_Dancer_1.jpg , I would like to transform the image programmatically, such that only the statue remains, and the background (i.e. the rest of the image) is all white, or transparent.
Also, the user might point out to the part of the image that should "remain", and the rest should be all white or transparent.
How can this be done? Also, if there is a suitable java library or piece of code, it would be helpful.
Regards
Upvotes: 4
Views: 6192
Reputation: 4093
For that particular image there are several methods that could solve part of the problem. Maybe if you combine several methods and offer the user interactive choices you could develop a good software in a reasonably short period of time. I'd use it!
You and Misha already discussed the first two items:
Improving usability
Assuming you can segment the image into chunks that are reasonably distinct, prompt the user to click on chunks that belong to the same object of interest. Each chunk could have its own set of tweaking parameters for edge strength, acceptance range for color, etc. Having chunk-specific parameters can help make the software usable even if there are shadows, varying lighting, etc., that present problems for segmentation. Something like this can be done in GIMP and Photoshop by combining selections, but it's less usable than it could be.
For the chunks that have been identified, implement a "snap to edge" feature that helps the user moves the discovered edge curve onto the true edge curve. If the user grabs a chunk contour and drags it in one direction, the contour could snap to the next strong edge in that direction.
Offer a batch processing option. If a user has a series of photos taken under the same conditions, then the user-selected chunks for the first image could help guide the software in setting parameters for successive images. This isn't intended to solve the general segmentation problem, but might save the user a bit of time and effort for certain groups of images.
It's a fun problem. Good luck!
Upvotes: 3
Reputation: 21906
What you're asking for is programmatic segmentation of foreground and background. As this is an active research area, you're unlikely to find any ready-mode source code that works out of the box (especially in Java).
If you have the time, look up image segmentation and browse through relevant papers on Google Scholar. You will see that in the general case, it's not an easy problem for a computer to solve. In specific cases, you can try to take advantage of certain conditions. In the image you've specified, the background is really blurry, so the edges of the figure really stand out well. Edge detection with the Canny operator gives you this:
It's not perfect, but it's a start. Using the edge information, you can locate major external contours and extract the dancer figure.
Upvotes: 4