1
$\begingroup$

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

enter image description here

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

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]

alpha

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.

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.