Smooth edges after background removal

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
crc863
Posts: 12
Joined: 2013-11-21T10:06:21-07:00
Authentication code: 6789

Smooth edges after background removal

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Smooth edges after background removal

Post 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
snibgo's IM pages: im.snibgo.com
crc863
Posts: 12
Joined: 2013-11-21T10:06:21-07:00
Authentication code: 6789

Re: Smooth edges after background removal

Post by crc863 »

Thanks so much that worked great!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Smooth edges after background removal

Post 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)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply