Jump to content

how to detect text box in an image?


ShivaGupta

Recommended Posts

Not much experience in this area but I have used "imagemagick" with PHP and I did find a possible lead for you:

 

Searching Google for "php imagemagick ocr"

 

Found:

 

http://stackoverflow.com/questions/11978986/ocr-getting-text-from-image-using-tesseract-3-0-and-imagemagick-6-6-5

  On 1/4/2014 at 1:04 PM, ShivaGupta said:

is their any example?

Not that I know of, you may have to roll up your sleeves and dive in.

 

It should just require a couple of nested for() loops and use of imagecolorat to test each pixel

I tried it with the png image you posted. It fount the top left OK but when working up from the bottom of the image it couldn't find a long chain of white pixels until it reached row 7 (should have found one at row 47). The only thing I can think of is there may be some anti-aliasing and not all pixels in the text background are a perfect white.

 

I saved the image as a a gif file to reduce the color resolution and it worked fine giving

left top (20, 0)
right bottom (462, 47)

gif image attached

 

Here's the code

<?php

$im = imagecreatefromgif('shiva1.gif');
$iw = imagesx($im);
$ih = imagesy($im);
$wht = imagecolorresolve($im,0xFF,0xFF,0xFF);

$top = $left = $bottom = $right = -1;

/***************************************
* search from top left                 *
****************************************/
for ($y=0; $y<$ih; $y++) {
    for ($x=0; $x<$iw; $x++) {
        if (countRight($im, $x, $y, $iw, $wht) >= 100) {
            $top = $y;
            $left = $x;
            break 2;
        }
    }
}
echo "left top ($left, $top)<br>";

/***************************************
* search from bottom right             *
****************************************/
for ($y=$ih-1; $y>0; $y--) {
    for ($x=$iw-1; $x>0; $x--) {
        if (countLeft($im, $x, $y, $wht) >= 100) {
            $bottom = $y;
            $right = $x;
            break 2;
        }
    }
}
echo "right bottom ($right, $bottom)";


/*******************************************************
* functions to count horizontal white pixels           *
********************************************************/
function countRight($im, $x, $y, $iw, $wht)
{
    $k = 0;
    while ($x < $iw) {
        if (imagecolorat($im, $x, $y)!=$wht) {
            return $k;
        }
        ++$k;
        ++$x;
    }
    return $k;
}

function countLeft($im, $x, $y, $wht)
{
    $k = 0;
    while ($x > 0) {
        if (imagecolorat($im, $x, $y)!=$wht) {
            return $k;
        }
        ++$k;
        --$x;
    }
    return $k;
}
?>

post-3105-0-82371000-1388876159_thumb.gif

Delete the 2 lines that echo the left-top and right-bottom coordinates then add this code

/************************************************
* Create image of the text area                 *
*************************************************/
$im2 = imagecreate($right-$left, $bottom-$top);
imagecopy($im2, $im, 0, 0, $left, $top, $right-$left, $bottom-$top);
// output image
header("Content-type: image/gif");
imagegif($im2);
imagedestroy($im);
imagedestroy($im2);
  On 12/31/2013 at 8:16 AM, Barand said:

Start at the top left and look for an occurrence of many contiguous horizontal white pixels.

Then work back from bottom right looking for same.

 

 

 Does PHP allow to iterate pixel by pixel... like this for example

for($y=0;$y<=$height;$y++)
{
  
  for($x=0;$x<=$width;$x++)
  {
    // pixel manipulation here
  }

}

Whenever I worked with images with php, it was always built-in functions, not sure how flexible the libraries actually are. From what I remember they had a layer of abstraction and you can invoke functions to do certain things to a group of pixels, don't know about pixel by pixel

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.