How do I remove the background?

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
353297

How do I remove the background?

Post by 353297 »

I want to remove the background and border of this image :

Image

and am using this code :

Code: Select all

exec("convert c.jpg -quality 100 c.jpg");
exec("convert c.jpg -fuzz 25000 -fill black -draw 'color 5,5 floodfill' -quality 100 c2.jpg");
exec("convert c2.jpg -negate -quality 100 c3.jpg");
exec("convert c3.jpg -shave 10x10 -quality 100 done.jpg");
where c.jpg is above image.

Using xampp with gd and imagemagick on windowsxp.

The problem is c3 image is same as c2 but with a different background. C3 image preview :

Image

The final image should look like :

Image

Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I remove the background?

Post by fmw42 »

not easy with a pattern background. see http://www.imagemagick.org/Usage/channels/#mask_bgnd

but try varying the fuzz factors in the following:

convert captchapl6.jpg -fuzz 15% -fill black -opaque "rgb(255,230,165)" \
-fuzz 40% -fill white +opaque black captchal6_result.jpg

Image
353297

Re: How do I remove the background?

Post by 353297 »

Thanks for your reply.

This is the code i tried

Code: Select all

exec("convert c.jpg -quality 100 c.jpg");
exec("convert c.jpg -fuzz 15% -fill black -opaque 'rgb(255,230,165)' -fuzz 40% -fill white +opaque black c2.jpg");
exec("convert c2.jpg -negate -quality 100 c3.jpg");
exec("convert c3.jpg -shave 10x10 -quality 100 done.jpg");
but it didn't work. The result is an image with a line in the center.

Did i try the correct code ? I'm a complete newbie as far as imagemagick is concerned.

Also, could this be due to xampp? Should i try this on a unix hosting?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I remove the background?

Post by fmw42 »

what do you get if you do just:

exec("convert c.jpg -fuzz 15% -fill black -opaque 'rgb(255,230,165)' -fuzz 40% -fill white +opaque black c2.jpg");

from the original c.jpg without the -quality step and without the other post processing? Note you don't need all the -quality 100 steps except at the end, and only if you want something other than the default of 85. And I am not sure why you need to negate in order to shave. I don't think that is necessary unless you want the invert the resulting black and white?

If you get the above working then we can streamline your other steps into one command.

The -fuzz 15% -fill black -opaque 'rgb...' step changes the text color (and others within the fuzz factor) to black. The -fuzz 40% -fill white +opaque black changes all non-black colors within the fuzz factor to white.

What version of IM are you using? Perhaps your version is too old and/or has a bug. I am using the current version 6.5.2-8 Q16 on Mac OSX Tiger.

<?php
system("convert -version");
?>


or

<?php
$IM_version=shell_exec("convert -version");
echo $IM_version
?>

I presume that your system is setup to know where IM is located as you have not provided the full path to IM, such as /usr/local/bin/convert or whatever it is on your presumably Windows system?

If you are on Windows, see

http://www.imagemagick.org/Usage/windows/

I am not a Windows user, and don't know if in PHP on Windows if you need to escape (double) the % as in %%

Also for PHP scripting see http://www.imagemagick.org/Usage/api/#php

For IM newbies, good reading at http://www.imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How do I remove the background?

Post by anthony »

Avoid if at all possible saving the image to JPEG. It is lossy and each save generates new colors as part of its compression algorithm. If you really need JPEG, use it only as a final step.

the problem with using a -fuzz -opaque method is it is a all or nothing method. This can be good if you are actually planing to later send the image though a OCR to try and decode the catchpa. But in a typical situation you lose some of the border information.

However it seems to be your best option. particularly by using a combination of -opaque and +opaque to select the color range to enhance the numbers from the background. It would however is specific to that specific image, and not easy to automate for similar catchpa's generated using various different color schemes.

Hmmm changing color space and extracting the the greyscale seems to produce a good separation.

Code: Select all

convert captchapl6.jpg -colorspace HSL -channel B -separate +channel \
               -sigmoidal-contrast 10x75% -negate  lum.png
Image

After all as we need to read it so their must be some difference in the areas of the image. The contrast enhancement in the above could also be replaced by some form of histogram segmentation.

Equalizing the saturation to enhance the brodaries also seem to work...

Code: Select all

convert captchapl6.jpg -colorspace HSL -channel G -separate +channel \
             -equalize -negate  sat.png
Image

This by the way also enhances the color effects that the JPG introduced in the image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How do I remove the background?

Post by Bonzo »

I hope you are not trying to get around a CAPTCHA system ?

With windows you need to use " not ' so /"rgb(255,230,165)/"
Post Reply