How can I check for a pixel pattern in PHP?
I mean I wanna use as condition that pixel A has xxx value and the following pixel B has another value yyy.
This is what I wrote:
$img = imagecreatefrompng("myimage.png");
$w = imagesx($img);
$h = imagesy($img);
for($y=0;$y<$h;$y++) {
for($x=0;$x<$w;$x++) {
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
echo "#".$r.$g.$b.",";
$pixel = $r.$g.$b;
if ($pixel == "481023" and $pixel+1???
}
echo "<br />\r\n";
}
I'd like to ask also if I can speed up the whole thing by incrementing the $x value by 2 every for cycle. This because I have a pattern of 2 pixels, maybe I can use something like:
for($x=0;$x<$w;$x+2) {
//...
if ($pixel == "xxx") {//check the following pixel}
else if ($pixel == "yyy") {//check the previous pixel}
}
You might want to define a function like:
function getpixelat($img,$x,$y) {
$rgb = imagecolorat($img,$x,$y);
$r = dechex(($rgb >> 16) & 0xFF);
$g = dechex(($rgb >> 8) & 0xFF);
$b = dechex($rgb & 0xFF);
return $r.$g.$b;
}
Notice the dechex - you need this if you want it to look like an HTML colour code. Otherwise "white" would be 255255255 instead of ffffff and you'd also get ambiguous colours - is 202020 a dark gray (20,20,20) or "red with a slight hint of blue" (202,0,20)?
Once you have this, it should be a simple matter:
for( $y=0; $y<$h; $y++) {
for( $x=0; $x<$w; $x++) {
$pixel = getpixelat($img,$x,$y);
if( $pixel == "481023" && getpixelat($img,$x+1,$y) == "998877") {
// pattern! Do something here.
$x++; // increment X so we don't bother checking the next pixel again.
}
}
}
Related
I am trying to implement a sobel filter in PHP GD but something is wrong with my code:
$gd = imagecreatefrompng('base.png');
$width = imagesx($gd);
$height = imagesx($gd);
for($i=1; $i<$width-1;$i++){
for($j=1;$j<$height-1; $j++){
$pixelMatrix[0][0]= getColor($gd, $i-1,$j-1);
$pixelMatrix[0][1]= getColor($gd, $i-1,$j);
$pixelMatrix[0][2]= getColor($gd, $i-1,$j+1);
$pixelMatrix[1][0]= getColor($gd, $i,$j-1);
$pixelMatrix[1][2]= getColor($gd, $i,$j+1);
$pixelMatrix[2][0]= getColor($gd, $i+1,$j-1);
$pixelMatrix[2][1]= getColor($gd, $i+1,$j);
$pixelMatrix[2][2]= getColor($gd, $i+1,$j+1);
$edge=(int) convolution($pixelMatrix);
if($edge>255) $edge = 255;
imagesetpixel($gd, $i, $j, imagecolorallocate($gd,$edge,$edge,$edge));
}
}
function getColor($gd, $x, $y){
$rgb = #imagecolorat($gd, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
return round($r * 0.3 + $g * 0.59 + $b * 0.11); // gray
}
function convolution($pixelMatrix){
$gy=($pixelMatrix[0][0]*-1)+($pixelMatrix[0][1]*-2)+($pixelMatrix[0][2]*-1)+($pixelMatrix[2][0])+($pixelMatrix[2][1]*2)+($pixelMatrix[2][2]*1);
$gx=($pixelMatrix[0][0])+($pixelMatrix[0][2]*-1)+($pixelMatrix[1][0]*2)+($pixelMatrix[1][2]*-2)+($pixelMatrix[2][0])+($pixelMatrix[2][2]*-1);
return sqrt(pow($gy,2)+pow($gx,2));
}
// send PNG to browser
header("Content-type: image/png");
imagepng($gd);
Base image:
Correct result image:
My result:
For this image the edge contains an int from 0-990 so I limit it to 255. If i remove the cap all I get is noise. Im guessing the error is in translating the edge into RGB values (I dont understand that part) in imagesetpixel, no?
There are a few things wrong, as marked up in comments, but the bulk of your code is pretty correct.
Main things are:
Input image may be palettised, so make true colour
Cannot do Sobel in place - you need output image
You have a typo where obtaining the height
And that is mainly it, I think!
$gd = imagecreatefrompng('base.png');
imagepalettetotruecolor($gd); // IN CASE PALETTISED
$width = imagesx($gd);
$height = imagesy($gd); // NOT imagesx()
$result=imagecreatetruecolor($width,$height); // CREATE OUTPUT IMAGE
for($i=1; $i<$width-1;$i++){
for($j=1;$j<$height-1; $j++){
$pixelMatrix[0][0]= getColor($gd, $i-1,$j-1);
$pixelMatrix[0][1]= getColor($gd, $i-1,$j);
$pixelMatrix[0][2]= getColor($gd, $i-1,$j+1);
$pixelMatrix[1][0]= getColor($gd, $i,$j-1);
$pixelMatrix[1][2]= getColor($gd, $i,$j+1);
$pixelMatrix[2][0]= getColor($gd, $i+1,$j-1);
$pixelMatrix[2][1]= getColor($gd, $i+1,$j);
$pixelMatrix[2][2]= getColor($gd, $i+1,$j+1);
$edge=(int) convolution($pixelMatrix);
if($edge>255) $edge = 255;
imagesetpixel($result, $i, $j, imagecolorallocate($result,$edge,$edge,$edge));
}
}
imagepng($result,"result.png");
function getColor($gd, $x, $y){
$rgb = #imagecolorat($gd, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
return round($r * 0.3 + $g * 0.59 + $b * 0.11); // gray
}
function convolution($pixelMatrix){
$gy=($pixelMatrix[0][0]*-1)+($pixelMatrix[0][1]*-2)+($pixelMatrix[0][2]*-1)+($pixelMatrix[2][0])+($pixelMatrix[2][1]*2)+($pixelMatrix[2][2]*1);
$gx=($pixelMatrix[0][0])+($pixelMatrix[0][2]*-1)+($pixelMatrix[1][0]*2)+($pixelMatrix[1][2]*-2)+($pixelMatrix[2][0])+($pixelMatrix[2][2]*-1);
return sqrt(pow($gy,2)+pow($gx,2));
}
There is an excellent answer on how to change the HUE of an image using PHP-GD library. But I need to know how to change the SATURATION of an image using PHP-GD. Here is a copy of the code from the answer which successfully changes the HUE of the image.
function imagehue(&$image, $angle) {
if($angle % 360 == 0) return;
$width = imagesx($image);
$height = imagesy($image);
for($x = 0; $x < $width; $x++) {
for($y = 0; $y < $height; $y++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$alpha = ($rgb & 0x7F000000) >> 24;
list($h, $s, $l) = rgb2hsl($r, $g, $b);
$h += $angle / 360;
if($h > 1) $h--;
list($r, $g, $b) = hsl2rgb($h, $s, $l);
imagesetpixel($image, $x, $y, imagecolorallocatealpha($image, $r, $g, $b, $alpha));
}
}
}
If you need to take a look at the code of helper functions rgb2hsl and hsl2rgb please check the original answer. Since Hue is one of the parameters of HSL, I thought I could modify the function somehow to get a working solution for Saturation. Despite of limited php skills I had to try but it did not work and produced bizarre results. Here is the modification that I am trying.
MODIFIED CODE: UPDATED as suggested by #mark
function imageSaturation(&$image, $saturationPercentage) {
$width = imagesx($image);
$height = imagesy($image);
for($x = 0; $x < $width; $x++) {
for($y = 0; $y < $height; $y++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$alpha = ($rgb & 0x7F000000) >> 24;
list($h, $s, $l) = rgb2hsl($r, $g, $b);
$s = $s * (100 + $saturationPercentage ) /100;
if($s > 1) $s = 1;
list($r, $g, $b) = hsl2rgb($h, $s, $l);
imagesetpixel($image, $x, $y, imagecolorallocatealpha($image, $r, $g, $b, $alpha));
}
}
}
header('Content-type: image/png');
$image = imagecreatefrompng('rgb.png');
imageSaturation($image, -80);//bring down current image saturation to 80%
imagepng($image);
EFFORTS UPDATE :
It has been pointed out to me by #Dai that these lines help construct a color code of an individual RGB pixel. So I guess this part can remained unchanged ?
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$alpha = ($rgb & 0x7F000000) >> 24;
In the next line we are simply converting the RGB values to HSL with rgb2hsl($r, $g, $b); and assigning it to list($h, $s, $l). The point I am stuck at the moment are these lines.
$s += $saturationPercentage / 100;
if($s > 1) $s--;
I understand the syntax, but not sure how I handle these or if they are even required. If not an answer helpful hints / suggestions would be great. I am trying the code on this image to bring down the saturation from 100% to 80%, but I am getting this image as result.
I think you need something like
$s=$s * (100+$saturationPercentage)/100
else you are just adding a constant to each value rather than a percentage of the existing value.
Also, where you decrement 1 if your new saturation exceeds 1, you probaly would be better just setting it to 1.0, i.e. fully saturated like this:
if($s>1)$s=1
otherwise, say the resulting saturation is 1.3 (i.e. very, very saturated) you will make that into 0.3, i.e. very undersaturated, instead of 1.0 (fully saturated).
So, if $s is 0.7, and you add 10%, you will get
$s = 0.7 * (100 + 10)/100
$s = 0.7 * 1.1
$s = 0.77
I need to find a particular pixel of an image. Let's say "89 21 24".
I'm using two nested cycles to walk the image:
for( $y=$inity; $y<$h; $y++) {
for( $x=$initx; $x<$w; $x++) {
$pixel = getpixelat($img,$x,$y);
if ($pixel == "892124" and getpixelat($img,$x+1,$y) == "1212224") {
$px = $x; $py = $y;
break 2;
}
}
I'd like to have a faster algorithm by increasing $y and $x not one by one, instead for example 4 by 4. (Sixteen times faster?)
Of course I need to know exactly the pixels that build 4x4 square.
I would do it like:
if ($pixel == "" and nextpixel...) {
$px = $x - 1; $py = $y -...;
}
//etc.
Is there a smarter way to achieve that?
EDIT:
Here's the getpixelat function:
function getpixelat($img,$x,$y) {
$rgb = imagecolorat($img,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
return $r.$g.$b;
}
If you know that the color you are looking for always appears in patches of at least 2x2, or 3x3, or any fixed size, then yes, you can speed up the lookup by incrementing $x and $y by more than one at each iteration.
For instance, if you know that your patches are always of size at least KxL, you can do the following:
for( $y=$inity; $y<$h; $y += L) {
for( $x=$initx; $x<$w; $x += K) {
$pixel = getpixelat($img,$x,$y);
if ($pixel == "892124" and getpixelat($img,$x+1,$y) == "1212224") {
$px = $x; $py = $y;
break 2;
}
}
}
1.) avoide nested loops:
2.) save old value of pixle (calling an function is not that fast)
3.) dont convert colors to string/dont compare strings! (what is 1212224? it is not a Hex-Color )
$px=-1;
$py=-1;
$lineW = ($w-$initx);
$lineH = ($h-$inity);
$count = $lineW * $lineH;
$color1 =hexdec ("892124") ;
$color2 =hexdec ("1212224") ;
$x=$initx;
$y=$inity;
$new = imagecolorat($img,$x,$y);
while(true) {
$x++;
if ($x>=$w) {
$x=$initx+1;
$y++;
if ($y>=$h) break;
$new = imagecolorat($img,$x-1,$y);
}
$old = $new;
$new = imagecolorat($img,$x,$y);
if ($old == $color1 && $new == $color2)
{
$px = $x; $py = $y;
break;
}
}
(code is not tested :-) )
I want to load an entire image (PNG) into a 2-dimensional array where a black pixel is true and a white pixel is false.
What's the most efficient way of doing this?
Should I convert the image into bitmap and attempt to read that in, or is there a more efficient method?
This should do:
$image = imagecreatefrompng("input.png");
$width = imagesx($image);
$height = imagesy($image);
$colors = array();
for ($y = 0; $y < $height; $y++)
{
for ($x = 0; $x < $width; $x++)
{
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$black = ($r == 0 && $g == 0 && $b == 0);
$colors[$x][$y] = $black;
}
}
A probably more efficient way would be using Imagick::exportImagePixels().
I'd like to loop through each row and column in an image and replace certain pixels with different colors. I am open to a solution using GD or ImageMagick. Can anyone give me an example of how to do this? I've Googled several different ways and haven't found a solid example.
You can achieve this with GD by something like:
You will be handling colors as hex values
function replaceColor($img, $from, $to) {
$r = hexdec(substr($to, 0, 2));
$g = hexdec(substr($to, 2, 2));
$b = hexdec(substr($to, 4, 2));
// allocate $to color.
$to = imagecolorallocate($img, $r, $g, $b);
// pixel by pixel grid.
for ($y = 0; $y < imagesy($img); $y++) {
for ($x = 0; $x < imagesx($img); $x++) {
// find hex at x,y
$at = imagecolorat($img, $x, $y);
$r = 0xFF & ($at >> 0x10);
$g = 0xFF & ($at >> 0x8);
$b = 0xFF & ($at);
$hex = dechex($r).dechex($g).dechex($b);
// set $from to $to if hex matches.
if ($hex == $from) {
imagesetpixel($img, $x, $y, $to);
}
}
}
}