Page 1 of 1

Smooth edges after background removal

Posted: 2014-04-03T14:26:16-07:00
by crc863
Hi everyone. I got great help before and I'm in need of a little bit more. This is the current script I call via PHP to remove a white background image and convert the remaining image to a single color.

Code: Select all

exec('convert '.WP_PLUGIN_DIR.'/fancy-product-designer/'.$fimage.' -quality 100 '.WP_PLUGIN_DIR.'/fancy-product-designer/'.$fimageNoExt.'c.png');
exec('convert '.WP_PLUGIN_DIR.'/fancy-product-designer/'.$fimageNoExt.'c.png -fuzz 23% -transparent white '.WP_PLUGIN_DIR.'/fancy-product-designer/'.$fimageNoExt.'t.png');
exec('convert '.WP_PLUGIN_DIR.'/fancy-product-designer/'.$fimageNoExt.'t.png \
\( -clone 0 -fuzz 99% -fill gray18 -opaque red -write '.WP_PLUGIN_DIR.'/fancy-product-designer/'.$fimageNoExt.'g.png \) \
null: 2>&1', $a);
It works but the removal of the background image leaves jagged edges. Is there a way I can smooth out the edges? Here is an example image below.

original
Image
converted
Image

Re: Smooth edges after background removal

Posted: 2014-04-03T15:28:37-07:00
by snibgo
"-transparent" is a binary operator: each pixel is either changed or it isn't. You could follow this with a blur. Or you could keep the original anti-aliasing, while removing the horrible jpeg noise. Windows BAT syntax:

Code: Select all

convert ^
  original_1963817856_vietnam-tourism-logo-design.jpeg ^
  -negate -grayscale brightness -level 10,50%% ^
  ( +clone -fill Black -colorize 100 ) ^
  +swap -compose CopyOpacity -composite ^
  t.png

Re: Smooth edges after background removal

Posted: 2014-04-03T16:27:39-07:00
by crc863
Thanks so much that worked great!

Re: Smooth edges after background removal

Posted: 2014-04-14T19:59:41-07:00
by anthony
Going to a deeper level.

A anti-aliased shape on a transparent background requires two pieces of knowledge for every pixel.
1/ the true foreground color of the pixel without the backgrounds influence
2/ how transparent the pixel should be.

You can determine 2 if you know the foreground color and the background color, and the two are different.

Well in this image the background is white. that means all the anti-aliasing pixels of the image will be lighter in color than they should be.

Also as the image has no 'border' and a very smooth coloring scheme, you can get an good idea of what the actual forground color is, simply by doing a morphological "Erode" operation, or perhaps a "Intensity_Erode". That is if a pixel is lighter than a neighbour, make it the same color as the neighbour.

Thus you will now have image of the pixels foreground colors, comparing that against the original image to see how much the pixel 'lightened' against a pure white background should let you see how transparent each pixel should be, masked so only pixels next to pure white is made transparent.

That should get you a pretty good transparency mask, which when combined with the foreground colors should get you a good anti-aliased version of the image, on a transparent background.

I am certain it can be worked out (but only for smoothly colored, borderless images on white backgrounds)