Advertisement
cdsatrian

histogram equalization

Dec 7th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.58 KB | None | 0 0
  1. <?php
  2.   //*********************************
  3.   // FILENAME     : class.gambar.php
  4.   // PURPOSE      : generate histogram equalization from given image
  5.   // CREATED BY   : Cahya DSN
  6.   // CREATED DATE : 2012/12/06
  7.   // UPDATE DATE  : 2012/12/07
  8.   // DOWNLOAD     : http://adzan.16mb.com/source/histogram.zip
  9.   //*********************************
  10.  
  11. class gambar{
  12.   private $filename;
  13.   private $img;
  14.   private $reds = array();
  15.   private $blues = array();
  16.   private $greens = array();
  17.   private $values = array();
  18.   private $freqr = array();
  19.   private $freqb = array();
  20.   private $freqg = array();
  21.   private $width,$height,$totalpixels;
  22.   private $fr,$fg,$fb,$fv;
  23.  
  24.   public function __construct($filename)
  25.   {  
  26.     $this->filename=$filename;
  27.     $info = getimagesize($filename);
  28.     $this->width = $info[0];
  29.     $this->height = $info[1];
  30.     $this->totalpixels = $this->width * $this->height;
  31.     $this->img = imagecreatefromjpeg($filename);
  32.     for($i=0;$i<256;$i++){
  33.       $this->reds[$i]=0;
  34.       $this->blues[$i] =0;
  35.       $this->greens[$i]=0;
  36.       $this->values[$i]=0;
  37.       $this->freqr[$i]=0;
  38.       $this->freqb[$i]=0;
  39.       $this->freqg[$i]=0;
  40.     }    
  41.   }
  42.   //Build image histogram
  43.   public function build_histogram()
  44.   {
  45.     for ($i = 0; $i < $this->height; $i++) {
  46.       for ($j = 0; $j < $this->width; $j++) {
  47.         $rgb = imagecolorat($this->img, $j, $i);
  48.         $r = ($rgb >> 16) & 0xFF;
  49.         $g = ($rgb >> 8) & 0xFF;
  50.         $b = $rgb & 0xFF;
  51.         $v = round(($r + $g + $b) / 3);
  52.         // Add counts to our histogram arrays for each color.
  53.         $this->reds[$r]++;
  54.         $this->greens[$g]++;
  55.         $this->blues[$b]++;
  56.         $this->values[$v]++;
  57.       }
  58.     }
  59.     // Sort them by keys into order
  60.     ksort($this->reds);
  61.     ksort($this->greens);
  62.     ksort($this->blues);
  63.     ksort($this->values);
  64.   }
  65.   // Build frequency charts for all colors (cummulative distribution function)
  66.   public function build_cdf()
  67.   {
  68.     $this->fr=0;
  69.     $this->fg=0;
  70.     $this->fb=0;
  71.     $this->fv=0;
  72.     // Loop through all values and sum counts from 0 to current column $i
  73.     for ($i = 0; $i <= 255; $i++) {
  74.       $sumR = 0;
  75.       $sumG = 0;
  76.       $sumB = 0;
  77.       $sumV = 0;
  78.       for ($j = 0; $j <= $i; $j++) {
  79.         // Sums for Red, Green, Blue
  80.         $sumR += $this->reds[$j];
  81.         $sumG += $this->greens[$j];
  82.         $sumB += $this->blues[$j];
  83.         $sumV += $this->values[$j];
  84.       }
  85.       // Stash sums into frequency charts for each color
  86.       $this->freqr[$i] = $sumR;
  87.       $this->freqg[$i] = $sumG;
  88.       $this->freqb[$i] = $sumB;
  89.       $this->freqv[$i] = $sumV;
  90.       $this->fr=($this->fr==0 && $this->freqr[$i]!=0)?$this->freqr[$i]:$this->fr;
  91.       $this->fg=($this->fg==0 && $this->freqg[$i]!=0)?$this->freqg[$i]:$this->fg;
  92.       $this->fb=($this->fb==0 && $this->freqb[$i]!=0)?$this->freqb[$i]:$this->fb;
  93.       $this->fv=($this->fv==0 && $this->freqv[$i]!=0)?$this->freqv[$i]:$this->fv;
  94.     }
  95.   }
  96.  
  97.   public function grayscale()
  98.   {
  99.     $newimg = @imagecreatetruecolor($this->width, $this->height);
  100.     $color = imagecolorallocate($newimg, 255, 255, 255);
  101.     for ($i = 0; $i < $this->height; $i++) {
  102.       for ($j = 0; $j < $this->width; $j++) {
  103.         $rgb = imagecolorat($this->img, $j, $i);
  104.         $r = ($rgb >> 16) & 0xFF;
  105.         $g = ($rgb >> 8) & 0xFF;
  106.         $b = $rgb & 0xFF;
  107.         $v = (int)(($r+$g+$b)/3);
  108.         $color = imagecolorallocate($newimg, $v, $v, $v);
  109.         imagesetpixel($newimg, $j, $i, $color);
  110.       }
  111.     }
  112.     header('Content-Type: image/jpg');
  113.     imagejpeg($newimg, NULL, 100);
  114.     imagedestroy($newimg);    
  115.   }
  116.  
  117.   public function red()
  118.   {
  119.     $newimg = @imagecreatetruecolor($this->width, $this->height);
  120.     $color = imagecolorallocate($newimg, 255, 255, 255);
  121.     for ($i = 0; $i < $this->height; $i++) {
  122.       for ($j = 0; $j < $this->width; $j++) {
  123.         $rgb = imagecolorat($this->img, $j, $i);
  124.         $r = ($rgb >> 16) & 0xFF;
  125.         $color = imagecolorallocate($newimg, $r, 0, 0);
  126.         imagesetpixel($newimg, $j, $i, $color);
  127.       }
  128.     }
  129.     header('Content-Type: image/jpg');
  130.     imagejpeg($newimg, NULL, 100);
  131.     imagedestroy($newimg);
  132.   }
  133.  
  134.   public function green()
  135.   {
  136.     $newimg = @imagecreatetruecolor($this->width, $this->height);
  137.     $color = imagecolorallocate($newimg, 255, 255, 255);
  138.     for ($i = 0; $i < $this->height; $i++) {
  139.       for ($j = 0; $j < $this->width; $j++) {
  140.         $rgb = imagecolorat($this->img, $j, $i);
  141.         $g = ($rgb >> 8) & 0xFF;
  142.         $color = imagecolorallocate($newimg, 0,$g, 0);
  143.         imagesetpixel($newimg, $j, $i, $color);
  144.       }
  145.     }
  146.     header('Content-Type: image/jpg');
  147.     imagejpeg($newimg, NULL, 100);
  148.     imagedestroy($newimg);
  149.   }
  150.  
  151.   public function blue()
  152.   {
  153.     $newimg = @imagecreatetruecolor($this->width, $this->height);
  154.     $color = imagecolorallocate($newimg, 255, 255, 255);
  155.     for ($i = 0; $i < $this->height; $i++) {
  156.       for ($j = 0; $j < $this->width; $j++) {
  157.         $rgb = imagecolorat($this->img, $j, $i);
  158.         $b = $rgb & 0xFF;
  159.         $color = imagecolorallocate($newimg, 0, 0, $b);
  160.         imagesetpixel($newimg, $j, $i, $color);
  161.       }
  162.     }
  163.     header('Content-Type: image/jpg');
  164.     imagejpeg($newimg, NULL, 100);
  165.     imagedestroy($newimg);    
  166.   }
  167.  
  168.   public function output()
  169.   {
  170.     $newimg = @imagecreatetruecolor($this->width, $this->height);
  171.     $color = imagecolorallocate($newimg, 255, 255, 255);
  172.     for ($i = 0; $i < $this->height; $i++) {
  173.       for ($j = 0; $j < $this->width; $j++) {
  174.         $rgb = imagecolorat($this->img, $j, $i);
  175.         $r = ($rgb >> 16) & 0xFF;
  176.         $g = ($rgb >> 8) & 0xFF;
  177.         $b = $rgb & 0xFF;
  178.         $v = (int)(($r+$g+$b)/3);
  179.         $adjR = (int)((float)abs($this->freqr[$r]-$this->fr) * (float)(255.0 / (float)($this->totalpixels-$this->fr)));
  180.         $adjG = (int)((float)abs($this->freqg[$g]-$this->fg) * (float)(255.0 / (float)($this->totalpixels-$this->fg)));
  181.         $adjB = (int)((float)abs($this->freqb[$b]-$this->fb) * (float)(255.0 / (float)($this->totalpixels-$this->fb)));
  182.         $color = imagecolorallocate($newimg, $adjR, $adjG, $adjB);
  183.         imagesetpixel($newimg, $j, $i, $color);
  184.       }
  185.     }
  186.     header('Content-Type: image/jpg');
  187.     imagejpeg($newimg, NULL, 100);
  188.     imagedestroy($newimg);    
  189.   }
  190.  
  191. }
  192. /*********** example *************
  193. $histo=new gambar("contoh.jpg");
  194. $histo->build_histogram();
  195. $histo->build_cdf();
  196. $histo->output();
  197. *******************************/
  198. //all used files and examples can be download @ http://adzan.16mb.com/source/histogram.zip
  199. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement