SHARE
TWEET

Untitled

a guest Dec 13th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     # DETECT RANDOMNESS IN "NOISE" OF AN IMAGE
  3.  
  4.  
  5.     $fname = $argv[1];
  6.     $im = imagecreatefrompng($fname);
  7.     list($sx, $sy) = getimagesize($fname);
  8.    
  9.     # -----------------------------------------------------------
  10.     # Divide the image into blocks and count a colors in each one
  11.     # For each color calculate average count in one block
  12.     # -----------------------------------------------------------
  13.     $xblocks = 8;
  14.     $yblocks = 8;
  15.     $xsize = intval($sx/$xblocks);
  16.     $ysize = intval($sy/$yblocks);
  17.     $count = $avg_count = array();
  18.     for ($yb = 0; $yb < $yblocks; $yb++) {
  19.     for ($xb = 0; $xb < $xblocks; $xb++) {
  20.         for ($y = $yb*$ysize; $y < ($yb+1)*$ysize; $y++) {
  21.         for ($x = $xb*$xsize; $x < ($xb+1)*$xsize; $x++) {
  22.             $c = imagecolorat($im, $x, $y);
  23.             @$count[$yb][$xb][$c]++;
  24.         }}
  25.         foreach ($count[$yb][$xb] as $color => $color_count) {
  26.             @$avg_count[$color] += $color_count/($xblocks*$yblocks);
  27.         }
  28.     }}
  29.  
  30.     # -----------------------------------------------------------
  31.     # Calculate a dispersion (deviation) from average count
  32.     # for each color as sum of each block's squared difference
  33.     # -----------------------------------------------------------
  34.     $d = array();
  35.     $dmax = 0;
  36.     for ($yb = 0; $yb < $yblocks; $yb++) {
  37.     for ($xb = 0; $xb < $xblocks; $xb++) {
  38.         foreach ($count[$yb][$xb] as $color => $color_count) {
  39.             @$d[$color] += pow($color_count - $avg_count[$color], 2);
  40.             if ($d[$color] > $dmax) $dmax = $d[$color];
  41.         }
  42.     }}
  43.    
  44.     # -----------------------------------------------------------
  45.     # Calculate average dispersion, just for information
  46.     # -----------------------------------------------------------
  47.     $avg_d = 0;
  48.     foreach ($d as $disp) {
  49.         $avg_d += $disp;
  50.     }
  51.     $avg_d /= count($d);
  52.     echo "MAX disp: ".round($dmax,2)."; AVG: ".round($avg_d,2)."\n";
  53.    
  54.     # -----------------------------------------------------------
  55.     # Find the largest "gap" in array, use it as edge
  56.     # -----------------------------------------------------------
  57.     asort($d);
  58.     $gap = 0;
  59.     $gap_disp = 0;
  60.     $prev_disp = -1;
  61.     foreach ($d as $color=>$disp) {
  62.         if ($prev_disp > 0) {
  63.             if ($disp - $prev_disp > $gap) {
  64.                 $gap = $disp - $prev_disp;
  65.                 $gap_disp = $prev_disp + ($disp - $prev_disp)/2;
  66.             }
  67.         }
  68.         $prev_disp = $disp;
  69.     }
  70.     echo "GAP: ".round($gap_disp,2)." ± ".round($gap/2,2)."\n";
  71.  
  72.     # -----------------------------------------------------------
  73.     # Blacken pixels with disp < $limit
  74.     # -----------------------------------------------------------
  75.     $limit = $gap_disp; //we can use intval($dmax/3);
  76.     for ($y = 0; $y < $sy; $y++) {
  77.     for ($x = 0; $x < $sx; $x++) {
  78.         $c = imagecolorat($im, $x, $y);
  79.         if ($d[$c] < $limit) {
  80.             imagesetpixel($im, $x, $y, 0);
  81.         }
  82.     }}
  83.    
  84.     imagepng($im, "solve.png");
  85.     echo "DONE.\n";
  86.    
  87. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top