ShivaGupta Posted January 10, 2014 Share Posted January 10, 2014 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......... i tried hard on this sinse past 2 days but no luck............plz help me out ........ Quote Link to comment Share on other sites More sharing options...
ShivaGupta Posted January 10, 2014 Author Share Posted January 10, 2014 <?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; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted January 10, 2014 Share Posted January 10, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.