Skip to content

Instantly share code, notes, and snippets.

@johnciacia
Created November 7, 2010 19:56
Show Gist options
  • Save johnciacia/666369 to your computer and use it in GitHub Desktop.
Save johnciacia/666369 to your computer and use it in GitHub Desktop.
This is a very basic optical character recognition script written in PHP. This is untested and serves merely a proof of concept. As noted in the comments, adjusting the sample size can improve results, since with a large sample size on a small image there
<?php
/* create a test image */
$im = @imagecreate(100, 20) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 1, 5, 5, "Hello, World!", $text_color);
/***
* Assumptions:
* A monochrome image where characters are black
* A single character is connected
* Characters are disjointed by white space
*/
$width = imagesx($im);
$height = imagesy($im);
/***
* Notes:
* The smaller the sample size the more accurate it will be,
* however, it will take longer. Larger images can use a larger
* sample size wihtout compromizing much accuracy.
*/
$x_sample = 1;
$y_sample = 1;
$last = 0;
for($i = 0; $i < $width; $i++) {
$col = array();
for($j = 0; $j < $height; $j++) {
$col[$j] = imagecolorat($im, $i, $j);
}
if(($current = array_sum($col)) > 0) {
if($last == 0) {
$l = 0;
}
for($k = 0; $k < $height; $k++) {
if(($l % $x_sample) == 0) {
if(($k % $y_sample) == 0) {
$sample .= $col[$k];
}
}
}
$l++;
$last = $current;
} else {
$last = 0;
}
if(!empty($sample) && $last == 0) {
echo $sample . "\n";
$sample = "";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment