Advertisement
overloop

image autocorrelation

Jun 2nd, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.01 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  3. <head>
  4.     <title>title</title>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6.     <meta name="description" content="" />
  7.     <meta name="keywords" content="" />
  8.     <meta name="robots" content="index,follow" />
  9.     <!-- <link rel="stylesheet" type="text/css" href="styles.css" /> -->
  10.     <script language="javascript" type="text/javascript" src="flot/jquery.js"></script>
  11.     <script language="javascript" type="text/javascript" src="flot/jquery.flot.js"></script>
  12.    
  13. <style type="text/css">
  14. body {
  15.     /*background: url('pattern.png');*/
  16. }
  17.  
  18. #rs {
  19.     color: #ff0000;
  20. }
  21. #gs {
  22.     color: #00ff00;
  23. }
  24. #bs {
  25.     color: #0000ff;
  26. }
  27. #flot {
  28.     width: 1024px;
  29.     height: 768px;
  30. }
  31. </style>
  32. <script type="text/javascript">
  33.  
  34. window.onload = function() {
  35.    
  36.     <?php
  37.         main();
  38.        
  39.         if (count($rs)>1) {
  40.             $ra = autocorel($rs);
  41.             $ga = autocorel($gs);
  42.             $ba = autocorel($bs);
  43.         }
  44.        
  45.         echo flot_data_string('d1',$ra) . "\n";
  46.         echo flot_data_string('d2',$ga) . "\n";
  47.         echo flot_data_string('d3',$ba) . "\n";
  48.     ?>
  49.     $.plot("#flot", [ d1, d2, d3 ]);
  50. };
  51.  
  52. </script>
  53. </head>
  54.  
  55. <?php
  56. function echo_post_var($var,$def) {
  57.     if (isset($_POST[$var]))
  58.         echo $_POST[$var];
  59.     else
  60.         echo $def;
  61. }
  62.  
  63.  
  64. ?>
  65.  
  66. <body>
  67. <form method="post" action="index.php" enctype="application/x-www-form-urlencoded">
  68. x1<input type="text" name="x1" value="<?php echo_post_var('x1',0); ?>" size="3"/>
  69. y2<input type="text" name="y1" value="<?php echo_post_var('y1',0); ?>" size="3"/>
  70. x2<input type="text" name="x2" value="<?php echo_post_var('x2',100); ?>" size="3"/>
  71. y2<input type="text" name="y2" value="<?php echo_post_var('y2',0); ?>" size="3"/>
  72. <input type="submit" value="go" />
  73. </form>
  74. <div id="flot"></div>
  75.  
  76. <?php
  77.  
  78. $rs = array();
  79. $gs = array();
  80. $bs = array();
  81.  
  82. function main() {
  83.     global $rs,$gs,$bs;
  84.     $coords = array('x1','x2','y1','y2');
  85.     $coords_are_set = TRUE;
  86.     foreach ($coords as $coord) {
  87.         if (!isset($_POST[$coord])) {
  88.             $coords_are_set = FALSE;
  89.             echo "coord $coord are not set<br>";
  90.         } else {
  91.             $$coord = $_POST[$coord];
  92.         }
  93.     }
  94.     if ($coords_are_set == FALSE) {
  95.         //exit("some coords are not set");
  96.         return;
  97.     }
  98.     $image = imagecreatefromjpeg("image.jpg");
  99.     $xl = $x2 - $x1;
  100.     $yl = $y2 - $y1;
  101.  
  102.     if ($xl == 0 || $yl == 0) {
  103.         $xk = 0;
  104.         $yk = 0;
  105.         if ( $xl == 0 ) {
  106.             $l = $yl;
  107.             $yk = 1;
  108.         } else {
  109.             $l = $xl;
  110.             $xk = 1;
  111.         }
  112.         for ($i=0;$i<$l;$i++) {
  113.             $rgb = imagecolorat($image, $x1+$xk*$i, $y1+$yk*$i);
  114.             $r = ($rgb >> 16) & 0xFF;
  115.             $g = ($rgb >> 8) & 0xFF;
  116.             $b = $rgb & 0xFF;
  117.             $rs[] = $r;
  118.             $gs[] = $g;
  119.             $bs[] = $b;
  120.         }
  121.     }
  122. }
  123.  
  124. function array_to_string($ar) {
  125.     $s = '';
  126.     foreach($ar as $v) {
  127.         $s = $s . $v . ' ';
  128.     }
  129.     return $s;
  130. }
  131.  
  132. function flot_data_string($name,$ar) {
  133.     $s = "var $name = [";
  134.     $l = count($ar);
  135.     for ($i=0;$i<$l-1;$i++) {
  136.         $x = $i+1;
  137.         $y = $ar[$i];
  138.         $s = $s . "[$x,$y],";
  139.     }
  140.     $x = $l;
  141.     $y = $ar[$l-1];
  142.     $s = $s . "[$x,$y]];";
  143.     return $s;
  144. }
  145.  
  146. function autocorel($ar) {
  147.     $l = count($ar);
  148.     $sum = array_sum($ar);
  149.     $avg = $sum / $l;
  150.     $disp = 0;
  151.     $ar_norm = array();
  152.     foreach ($ar as $v) {
  153.         $disp = $disp + pow($v - $avg,2.0);
  154.     }
  155.     $disp = $disp / $l;
  156.     $sko = sqrt($disp);
  157.     foreach ($ar as $v) {
  158.         $ar_norm[] = ($v - $avg) / $sko;
  159.     }
  160.     //echo "sko: $sko; avg: $avg";
  161.     //return array_to_string($ar_norm);
  162.    
  163.     $ar_corel = array();
  164.     $corel = 0;
  165.     for ($i=1;$i<$l*2/3;$i++) {
  166.         for ($j=0;$j<$l-$i;$j++) {
  167.             $corel = $corel + $ar_norm[$j]*$ar_norm[$j+$i];
  168.         }
  169.         $corel = $corel;
  170.         $ar_corel[] = $corel;
  171.     }
  172.     return $ar_corel; //flot_data_string('blah',$ar_corel); //array_to_string($ar_corel);
  173. }
  174.  
  175. //$rs_text = array_to_string($rs);
  176. //$gs_text = array_to_string($gs);
  177. //$bs_text = array_to_string($bs);
  178.  
  179. //echo '<div id="rs">' . $rs_text . '</div><div id="gs">' . $gs_text . '</div><div id="bs">' . $bs_text . '</div>';
  180. //echo $ac;
  181.  
  182. ?>
  183.  
  184. </body>
  185. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement