note 97957 modified in function.imagecolorat by danbrown

From: Date: Tue, 18 May 2010 15:04:03 +0000
Subject: note 97957 modified in function.imagecolorat by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169353@lists.php.net to get a copy of this message
If an image is filled with a solid color (rgb 0,0,0) the average color will be 0,0,0, and the histogram will have the maximum values at r[0], g[0], and b[0]. Since we remove those values with the tolerance (+- 16), we are only left with colors outside of average. If the removed colors represent more than 90% of the image (we remove 6.25-12.5% of the histogram based on the average color), then we asume the image is blank. This detects solid filled images of any color, and with images of very little color variance (blank wall, blue sky with no clouds, nearly faded images, very low contrast and low color range images). The function is perfect for checking if a frame from a video is blank or not (in case you are generating thumbnails). <?php function isBlank($gd_resource, $tolerance, $percent) { list($w,$h) = array(imagesx($gd_resource), imagesy($gd_resource)); $count = 0; $hr = $hg = $hb = array(); for ($i=0; $i<$h; $i++) { for ($j=0; $j<$w; $j++) { $pos = imagecolorat($gd_resource, $j, $i); $f = imagecolorsforindex($gd_resource, $pos); $hr[$f['red']] = issetor($hr[$f['red']],0) + 1; $hg[$f['green']] = issetor($hg[$f['green']],0) + 1; $hb[$f['blue']] = issetor($hb[$f['blue']],0) + 1; $count ++; } } $rc = array_sum($hr); $gc = array_sum($hg); $bc = array_sum($hb); $r = $rc/$count; $g = $gc/$count; $b = $bc/$count; $ra = range(max(0,$r-$tolerance), min(255,$r+$tolerance)); $ga = range(max(0,$g-$tolerance), min(255,$g+$tolerance)); $ba = range(max(0,$b-$tolerance), min(255,$b+$tolerance)); foreach ($ra as $rx) { unset($hr[$rx]); } foreach ($ga as $gx) { unset($hg[$gx]); } foreach ($ba as $bx) { unset($hb[$bx]); } $red = floatval(array_sum($hr)+1) / floatval($rc); $green = floatval(array_sum($hg)+1) / floatval($gc); $blue = floatval(array_sum($hb)+1) / floatval($bc); if ($red<$percent && $green<$percent && $blue<$percent) { return true; } return false; } // Example; isBlank($gd_resource, 16, 0.1); ?> --was-- function isBlank($gd_resource, $tolerance, $percent) { list($w,$h) = array(imagesx($gd_resource), imagesy($gd_resource)); $count = 0; $hr = $hg = $hb = array(); for ($i=0; $i<$h; $i++) { for ($j=0; $j<$w; $j++) { $pos = imagecolorat($gd_resource, $j, $i); $f = imagecolorsforindex($gd_resource, $pos); $hr[$f['red']] = issetor($hr[$f['red']],0) + 1; $hg[$f['green']] = issetor($hg[$f['green']],0) + 1; $hb[$f['blue']] = issetor($hb[$f['blue']],0) + 1; $count ++; } } $rc = array_sum($hr); $gc = array_sum($hg); $bc = array_sum($hb); $r = $rc/$count; $g = $gc/$count; $b = $bc/$count; $ra = range(max(0,$r-$tolerance), min(255,$r+$tolerance)); $ga = range(max(0,$g-$tolerance), min(255,$g+$tolerance)); $ba = range(max(0,$b-$tolerance), min(255,$b+$tolerance)); foreach ($ra as $rx) { unset($hr[$rx]); } foreach ($ga as $gx) { unset($hg[$gx]); } foreach ($ba as $bx) { unset($hb[$bx]); } $red = floatval(array_sum($hr)+1) / floatval($rc); $green = floatval(array_sum($hg)+1) / floatval($gc); $blue = floatval(array_sum($hb)+1) / floatval($bc); if ($red<$percent && $green<$percent && $blue<$percent) { return true; } return false; } Example; isBlank($gd_resource, 16, 0.1); If an image is filled with a solid color (rgb 0,0,0) the average color will be 0,0,0, and the histogram will have the maximum values at r[0], g[0], and b[0]. Since we remove those values with the tolerance (+- 16), we are only left with colors outside of average. If the removed colors represent more than 90% of the image (we remove 6.25-12.5% of the histogram based on the average color), then we asume the image is blank. This detects solid filled images of any color, and with images of very little color variance (blank wall, blue sky with no clouds, nearly faded images, very low contrast and low color range images). The function is perfect for checking if a frame from a video is blank or not (in case you are generating thumbnails). http://php.net/manual/en/function.imagecolorat.php

« previous php.notes (#169353) next »