-1

I want use OCR. But the images can't read perfectly, so i'm converting image to delete noise background, Original Images.

then, i'm run this command :

convert -colorspace gray -modulate 120 -contrast-stretch 10%x80% -modulate 140 -gaussian-blur 1 -contrast-stretch 5%x50% +repage -negate -gaussian-blur 4 -negate -modulate 130 original.jpeg clean.jpeg

Images Result

The problem is, how to convert above command to php? Well, i'm very confusing using imagick in php.

mycode (this what i know) :

$image = new Imagick('captcha.png'); 
$image->modulateImage(450, 0, 500);
$image->writeImage("output.jpg"); 

Result from PHP Imagick : HERE

I know, it's diffrent configuration number, but result not to far.

Any suggestions how?

==== answare (thank you fmw42)

$image = new Imagick('captcha.png');
$image->thresholdimage(0.1 * \Imagick::getQuantum(), 134217727); 
$image->shaveImage(2, 1);
$image->writeImage("output.jpg"); 
testonID
  • 18
  • 6
  • 1
    What have you tried? Please post your attempt with Imagick coding so it can be corrected. Alternately, just put your command line in a PHP exec() and run it that way. For Imagick, see https://www.php.net/manual/en/book.imagick.php or Google for examples. – fmw42 Apr 05 '19 at 19:21
  • i do read php doc, but it's not look like same for command. my code : `$image = new Imagick('captcha.png'); $image->modulateImage(450, 0, 500); $image->writeImage("output.jpeg"); ` – testonID Apr 05 '19 at 19:24
  • Best to add your code to your original post. Sorry, I am not an Imagick expert. Note your modulateImage arguments are not the same as what is in your ImageMagick command line. Your Imagick code will convert to grayscale, since the saturation is 0, but it will also change the brightness and the color. Please clarify that or what you are trying to do with your Imagick code. The imagemagick code seems to be adjusting the brightness only. Please explain clearly what you are trying to achieve. If you post links to input images you want to clean, then perhaps we can suggest better techniques. – fmw42 Apr 05 '19 at 20:33
  • my bad, sorry. i has been edit first page with images example and result. – testonID Apr 05 '19 at 20:53
  • Just threshold your image. In ImageMagick `convert img.png -threshold 0% x.png`. Change the 0% to some other value as desired. In Imagick, see https://www.php.net/manual/en/imagick.thresholdimage.php. Values are in the quantum range of your ImageMagick version (0 to 65535 for Q16 default) rather than percent. – fmw42 Apr 05 '19 at 20:59
  • great.. it's works .. any ide how to delete border images ?? – testonID Apr 05 '19 at 21:48

1 Answers1

1

To remove the black border and threshold your image in ImageMagick, do

Input:

enter image description here

convert img.png -shave 1x1 -threshold 0 result.png


enter image description here

Because the 8 and 7 touch, I would be surprised if OCR worked.

For Imagick, see

https://www.php.net/manual/en/imagick.thresholdimage.php https://www.php.net/manual/en/imagick.shaveimage.php

fmw42
  • 46,825
  • 10
  • 62
  • 80