Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //*********************************
- // FILENAME : class.gambar.php
- // PURPOSE : generate histogram equalization from given image
- // CREATED BY : Cahya DSN
- // CREATED DATE : 2012/12/06
- // UPDATE DATE : 2012/12/07
- // DOWNLOAD : http://adzan.16mb.com/source/histogram.zip
- //*********************************
- class gambar{
- private $filename;
- private $img;
- private $reds = array();
- private $blues = array();
- private $greens = array();
- private $values = array();
- private $freqr = array();
- private $freqb = array();
- private $freqg = array();
- private $width,$height,$totalpixels;
- private $fr,$fg,$fb,$fv;
- public function __construct($filename)
- {
- $this->filename=$filename;
- $info = getimagesize($filename);
- $this->width = $info[0];
- $this->height = $info[1];
- $this->totalpixels = $this->width * $this->height;
- $this->img = imagecreatefromjpeg($filename);
- for($i=0;$i<256;$i++){
- $this->reds[$i]=0;
- $this->blues[$i] =0;
- $this->greens[$i]=0;
- $this->values[$i]=0;
- $this->freqr[$i]=0;
- $this->freqb[$i]=0;
- $this->freqg[$i]=0;
- }
- }
- //Build image histogram
- public function build_histogram()
- {
- for ($i = 0; $i < $this->height; $i++) {
- for ($j = 0; $j < $this->width; $j++) {
- $rgb = imagecolorat($this->img, $j, $i);
- $r = ($rgb >> 16) & 0xFF;
- $g = ($rgb >> 8) & 0xFF;
- $b = $rgb & 0xFF;
- $v = round(($r + $g + $b) / 3);
- // Add counts to our histogram arrays for each color.
- $this->reds[$r]++;
- $this->greens[$g]++;
- $this->blues[$b]++;
- $this->values[$v]++;
- }
- }
- // Sort them by keys into order
- ksort($this->reds);
- ksort($this->greens);
- ksort($this->blues);
- ksort($this->values);
- }
- // Build frequency charts for all colors (cummulative distribution function)
- public function build_cdf()
- {
- $this->fr=0;
- $this->fg=0;
- $this->fb=0;
- $this->fv=0;
- // Loop through all values and sum counts from 0 to current column $i
- for ($i = 0; $i <= 255; $i++) {
- $sumR = 0;
- $sumG = 0;
- $sumB = 0;
- $sumV = 0;
- for ($j = 0; $j <= $i; $j++) {
- // Sums for Red, Green, Blue
- $sumR += $this->reds[$j];
- $sumG += $this->greens[$j];
- $sumB += $this->blues[$j];
- $sumV += $this->values[$j];
- }
- // Stash sums into frequency charts for each color
- $this->freqr[$i] = $sumR;
- $this->freqg[$i] = $sumG;
- $this->freqb[$i] = $sumB;
- $this->freqv[$i] = $sumV;
- $this->fr=($this->fr==0 && $this->freqr[$i]!=0)?$this->freqr[$i]:$this->fr;
- $this->fg=($this->fg==0 && $this->freqg[$i]!=0)?$this->freqg[$i]:$this->fg;
- $this->fb=($this->fb==0 && $this->freqb[$i]!=0)?$this->freqb[$i]:$this->fb;
- $this->fv=($this->fv==0 && $this->freqv[$i]!=0)?$this->freqv[$i]:$this->fv;
- }
- }
- public function grayscale()
- {
- $newimg = @imagecreatetruecolor($this->width, $this->height);
- $color = imagecolorallocate($newimg, 255, 255, 255);
- for ($i = 0; $i < $this->height; $i++) {
- for ($j = 0; $j < $this->width; $j++) {
- $rgb = imagecolorat($this->img, $j, $i);
- $r = ($rgb >> 16) & 0xFF;
- $g = ($rgb >> 8) & 0xFF;
- $b = $rgb & 0xFF;
- $v = (int)(($r+$g+$b)/3);
- $color = imagecolorallocate($newimg, $v, $v, $v);
- imagesetpixel($newimg, $j, $i, $color);
- }
- }
- header('Content-Type: image/jpg');
- imagejpeg($newimg, NULL, 100);
- imagedestroy($newimg);
- }
- public function red()
- {
- $newimg = @imagecreatetruecolor($this->width, $this->height);
- $color = imagecolorallocate($newimg, 255, 255, 255);
- for ($i = 0; $i < $this->height; $i++) {
- for ($j = 0; $j < $this->width; $j++) {
- $rgb = imagecolorat($this->img, $j, $i);
- $r = ($rgb >> 16) & 0xFF;
- $color = imagecolorallocate($newimg, $r, 0, 0);
- imagesetpixel($newimg, $j, $i, $color);
- }
- }
- header('Content-Type: image/jpg');
- imagejpeg($newimg, NULL, 100);
- imagedestroy($newimg);
- }
- public function green()
- {
- $newimg = @imagecreatetruecolor($this->width, $this->height);
- $color = imagecolorallocate($newimg, 255, 255, 255);
- for ($i = 0; $i < $this->height; $i++) {
- for ($j = 0; $j < $this->width; $j++) {
- $rgb = imagecolorat($this->img, $j, $i);
- $g = ($rgb >> 8) & 0xFF;
- $color = imagecolorallocate($newimg, 0,$g, 0);
- imagesetpixel($newimg, $j, $i, $color);
- }
- }
- header('Content-Type: image/jpg');
- imagejpeg($newimg, NULL, 100);
- imagedestroy($newimg);
- }
- public function blue()
- {
- $newimg = @imagecreatetruecolor($this->width, $this->height);
- $color = imagecolorallocate($newimg, 255, 255, 255);
- for ($i = 0; $i < $this->height; $i++) {
- for ($j = 0; $j < $this->width; $j++) {
- $rgb = imagecolorat($this->img, $j, $i);
- $b = $rgb & 0xFF;
- $color = imagecolorallocate($newimg, 0, 0, $b);
- imagesetpixel($newimg, $j, $i, $color);
- }
- }
- header('Content-Type: image/jpg');
- imagejpeg($newimg, NULL, 100);
- imagedestroy($newimg);
- }
- public function output()
- {
- $newimg = @imagecreatetruecolor($this->width, $this->height);
- $color = imagecolorallocate($newimg, 255, 255, 255);
- for ($i = 0; $i < $this->height; $i++) {
- for ($j = 0; $j < $this->width; $j++) {
- $rgb = imagecolorat($this->img, $j, $i);
- $r = ($rgb >> 16) & 0xFF;
- $g = ($rgb >> 8) & 0xFF;
- $b = $rgb & 0xFF;
- $v = (int)(($r+$g+$b)/3);
- $adjR = (int)((float)abs($this->freqr[$r]-$this->fr) * (float)(255.0 / (float)($this->totalpixels-$this->fr)));
- $adjG = (int)((float)abs($this->freqg[$g]-$this->fg) * (float)(255.0 / (float)($this->totalpixels-$this->fg)));
- $adjB = (int)((float)abs($this->freqb[$b]-$this->fb) * (float)(255.0 / (float)($this->totalpixels-$this->fb)));
- $color = imagecolorallocate($newimg, $adjR, $adjG, $adjB);
- imagesetpixel($newimg, $j, $i, $color);
- }
- }
- header('Content-Type: image/jpg');
- imagejpeg($newimg, NULL, 100);
- imagedestroy($newimg);
- }
- }
- /*********** example *************
- $histo=new gambar("contoh.jpg");
- $histo->build_histogram();
- $histo->build_cdf();
- $histo->output();
- *******************************/
- //all used files and examples can be download @ http://adzan.16mb.com/source/histogram.zip
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement