Jump to content

how to detect text box in an image?


ShivaGupta
Go to solution Solved by Barand,

Recommended Posts

  • Solution

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

Link to comment
Share on other sites

ok sir Thanks a lot ....

and again here i want to save new picture with detected pixeles left top (20, 0)
right bottom (462, 47)

 

 

i tried myself  but i am new in php so no luck plz help again.........Thank you

Edited by ShivaGupta
Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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

Edited by GetFreaky
Link to comment
Share on other sites

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.