Page 1 of 1

How do I remove the background?

Posted: 2009-05-25T17:00:41-07:00
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

Re: How do I remove the background?

Posted: 2009-05-25T17:18:29-07:00
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

Re: How do I remove the background?

Posted: 2009-05-25T17:43:15-07:00
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?

Re: How do I remove the background?

Posted: 2009-05-25T18:13:06-07:00
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/

Re: How do I remove the background?

Posted: 2009-05-25T21:38:53-07:00
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.

Re: How do I remove the background?

Posted: 2009-05-26T00:02:21-07:00
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)/"