Jump to content

Recommended Posts

Recently i try to detect one text box with this code for uploded imege no 1....and sucesfully .

<?php

$im = imagecreatefromgif('1.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;
}
?>

but here i have another challange to detect  4 text boxes in image no 2 i tried that code but note working.........post-154438-0-05824600-1389383970_thumb.gifpost-154438-0-30813900-1389384076_thumb.png

 

i tried hard on this sinse past 2 days but no luck............plz help me out ........

 

 


<?php
$im = imagecreatefrompng('2.png');
$iw = imagesx($im);
$ih = imagesy($im);
imagefilter($im,IMG_FILTER_BRIGHTNESS,20);
//get the correct result on the original PNG image by adjusting the image brightness (line 5 in code attached)
$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;
}
}
}

/***************************************
* 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;
}
}
}

/************************************************
* Create image of the text area *
*************************************************/
$im2 = imagecreate($right-$left+1, $bottom-$top+1);
$blk = imagecolorallocate($im2,0,0,0);
imagecopy($im2, $im, 0, 0, $left, $top, $right-$left, $bottom-$top);
imagerectangle($im2,0,0,$right-$left, $bottom-$top, $blk);
// output image
header("Content-type: image/gif");
imagegif($im2);
imagedestroy($im);
imagedestroy($im2);

/*******************************************************
* 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;
}
?>

Not as easy but, in theory, you could apply a similar technique.

 

The searching code above stops when it finds a long row of white pixels. This time you would have to continue until all four top left  (x,y) positions were found and stored in an array.

 

You would then do the same for the bottom right positions and pair them off with the top left positions.

 

However in practice you have a problem with the image you posted. If you look at my attached image (which is part of a magnified version of yours) you will see there is no row of white pixels at the bottom of the text box - the text touches the border.

 

One option may be to find the top lefts (where there are white pixels) and assume they are all of a fixed size.

post-3105-0-72145400-1389388824_thumb.png

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.