Median filters

Noise reduction

Original Image Modified Image Source code
<?php

require_once('../gd2imaging.php');

$image = new Image('noise.PNG');
// use default 3x3 matrix
$image->useMedian();
$image->show();

Pastelization

Original Image Modified Image Source code
<?php

require_once('../gd2imaging.php');

$image = new Image('noise.PNG');
// use 7x7 median matrix
$image->useMedian(77);
$image->show();

Hough transform

Skew detection

Original Image Modified Image Source code
<?php

require_once('../gd2imaging.php');

$image = new Image('test4.PNG');
$image->getSkewAngle(
    
8// detect up to 8 different angles
    
256// use 256x256 matrix
    
true // draw Hough lines (debug mode)
);
$image->show();

Auto deskewing

Original Image Modified Image Source code
<?php

require_once('../gd2imaging.php');

$image = new Image('test4.PNG');
$image->deskew();
// rotate by 90 degrees after deskewing
// because text should be placed
// horizontally, not vertically
// (deskew doesn't detect text direction)
$image->rotate(90);
$image->show();

Other operations

Background color detection

Original Image Modified Image Source code
<?php

require_once('../gd2imaging.php');

$image = new Image('blue.png');
// rotate by 45 degrees
// and autodetect background color
// in this case, it will detect
// blue color as the background
// color
$image->rotate(45);
$image->show();
// you can read the bg color using
// this function:
$color $this->getBackgroundColor();

Image histogram

Original Image Modified Image Source code
<?php

require_once('../gd2imaging.php');

$image = new Image('plain.jpg');
$histogram $image->getHistogram();
$histogram->show();

OCR - Optical Character Recognition

Basic OCR features

Original Image Result (plain text) Source code
Found: THIS IS A SIMPLE TEST
<?php
require_once('../gd2imaging.php');

// load an image with a reference alphabet characters
$charsImage = new Image('alphabet.png');
// set size of the reference characters
$charSize = new Dimensions(2625);
// create the quantizator
$quantizator = new Quantizator();

foreach(
range('A''Z') as $index => $char) {
    
// fetch an image of the given character
    
$charImage $charsImage->getSubImage(new Point($index 260), $charSize);
    
// add vector to the glyphs collection
    
$quantizator->addGlyph($charImage->getVector(), $char);
}

// load an image with text to read
$image = new Image('text.png');

$text '';
$lastObject null;
// try to find all objects(letters) in the image.
foreach($image->findObjects() as $object) {
    
// find spaces...
    
if($lastObject && $object->getPosition()->$lastObject->getPosition()->$object->getDimensions()->width 1.5) {
        
$text .= ' ';
    }
    
$lastObject $object;
    
$search $object->resize($charSize)->getVector();
    
$result $quantizator->findNearestEuklid($search);
    
$text .= $result[0];
}

echo 
'Found: <strong>'.$text.'</strong><br>';

Basic captcha reading features

Original Image (captcha) Result (plain text) Source code
Captcha: K5ANAO
<?php

require_once('../gd2imaging.php');

// load an image with a reference alphabet characters
$charsImage = new Image('alphabet.png');
// set size of the reference characters
$charSize = new Dimensions(2625);
// create the quantizator
$quantizator = new Quantizator();

foreach(
array_merge(range('A''Z'), range(19)) as $index => $char) {
    
// fetch an image of the given character
    
$charImage $charsImage->getSubImage(new Point($index 260), $charSize);
    
// add vector to the glyphs collection
    
$quantizator->addGlyph($charImage->getVector(), $char);
}

// load captcha text to read
$image = new Image('captcha.gif');

// invert colors, remove captcha noise
$image->toNegative()->useMedian();

$text '';
$lastObject null;

// try to find all letters and numbers in the captcha.
foreach($image->findObjects() as $object) {
    
$lastObject $object;
    
$search $object->resize($charSize)->getVector();
    
$result $quantizator->findNearestEuklid($search);
    
$text .= $result[0];
}

echo 
'Captcha: <strong>'.$text.'</strong><br>';

Color space manipulation

Hue rotation

Original Image Modified Image Source code
<?php

require_once('../gd2imaging.php');

$image = new Image('bird.png');
$image->setHue(180); // 180 degrees
$image->show();

Vibrant colors (Saturation + Luminance)

Original Image Modified Image Source code
<?php

require_once('../gd2imaging.php');

$image = new Image('hawaii.jpg');
// Luminance set to 140%
$image->setLuminance(1.4);
// Color saturation set to 220%
$image->setSaturation(2.2);
$image->show();;