For example, using ExampleData[{"TestImage", "Girl2"}]:

what's a general way to make the background transparent? I've tried various combinations of EdgeDetect and Threshold and ImageAdd but can't figure it out. Thank you.
|
For example, using
what's a general way to make the background transparent? I've tried various combinations of |
|||||||||||||
|
|
============>> 2-liner <<============ Let's import the image and crop boundary artifacts:
We have quite uniform background. So with Mathematica functions such as
Below is just a deeper insight on how it all owrks on pixel value level. ============>> Deeper Insight <<============ Now lets get the
Because background is located above approximately 0 to 60, i'll pick number 30 (1st tuning parameter) as a test pixel and plot
Based on this we can say that roughly background is everything below threshold 0.1 (2nd tuning parameter) and replace all those pixels with white {1,1,1} pixel:
Based on that create mask and to avoid pixelation at the edge
And finally mask the original image by adding the thresholded version of it:
In the same way you could add an alpha channel to background making it transparent. To play with the whole procedure make an app to tune in the 3 parameters described above. Note the little piece of background in the left bottom corner is removed now by tuning background pixel parameter.
Additional parameters to add could be |
|||||||||||||
|
Edit It works quite well mostly
The problems are due to the background not being connected or not being the biggest morphological component. Edit Using RM's suggestion
Not perfect, but better. (blondes still can't make it unharmed)
|
|||||||||||||
|