# Mathematica 8 remove black background from image

How do I do that in Mathemtaica 8? I have found on this site many options like RemoveBackground[], but they don't work in Math.8.

Are there any options?

Here is the image; the new background should be white

• Please give an example of an image. What do you want to replace the black background with? – C. E. Oct 20 '14 at 20:54

Here is a quick-and dirty solution that takes the image as the first argument and an optional tolerance within which colors are assumed to be equal to the background:

removeBackground[im_, tol_: .3] :=
Module[{color,
r = If[ImageColorSpace[im] != "RGB", ColorConvert[im, "RGB"], im,
im]},
color = ImageData[r][[2, 2]];
SetAlphaChannel[r, Binarize[r, (Norm[# - color] > tol) &]]]

im = Import["http://i.stack.imgur.com/mgYjP.jpg"];

removeBackground[im]


The function works by adding an alpha channel to the image in which all colors that have been deemed background (via Binarize) are transparent. For simplicity, I consider the pixel at position 2,2 to be representative of the background. One could add an option to let the user specify the background color manually, too. But the above approach is probably going to work most of the time if the background is connected to the corners.