Last update: 31.03.2008 | © 2025 Julian von Mendel | Datenschutz
<?php
/* (C) 2008 by Julian von Mendel (prog@derjulian.net)
* License: LGPL3
* $LastChangedBy: jvm $
* $LastChangedDate: 2008-04-03 14:04:24 +0000 (Thu, 03 Apr 2008) $
* $Revision: 5 $
*
* Todo: generalize
*/
require_once("image.php");
class image_sobel extends imagealgorithmmask {
/* sobel mask */
public static $gx = array(
array(-1, 0, 1),
array(-2, 0, 2),
array(-1, 0, 1));
public static $gy = array(
array(1, 2, 1),
array(0, 0, 0),
array(-1, -2, -1));
protected $colors = true;
protected $alpha = array();
protected $maxbrightness = false;
public function setmaxbrightness($yes) {
$this->maxbrightness = $yes;
return $this;
}
public function setcolors($yes) {
$this->colors = $yes;
return $this;
}
public function getalpha() {
return $this->alpha;
}
public function algorithm($x, $y) {
if (($y == 0 || $y == $this->image->height() - 1) ||
($x == 0 || $x == $this->image->width() - 1))
return 0;
$sumx = 0;
$sumy = 0;
for($i = -1; $i <= 1; $i++) { for($j = -1; $j <= 1; $j++) {
if (self::$gx[$i + 1][$j + 1] == 0 &&
self::$gy[$i + 1][$j + 1] == 0)
continue;
$intensity = $this->image->getpixel($x + $i, $y + $j, false);
$intensity = reset($intensity);
$sumx += $intensity * self::$gx[$i + 1][$j + 1];
$sumy += $intensity * self::$gy[$i + 1][$j + 1];
}}
$sum = sqrt(pow($sumx, 2) + pow($sumy, 2));
$alpha = atan2($sumy, $sumx) * 180 / M_PI;
$alpha = round($alpha / 45) * 45;
if ($alpha < 0)
$alpha += 180;
$alpha %= 180;
if ($y == 0)
$this->alpha[$x] = array();
$this->alpha[$x][$y] = $alpha;
if ($sum < $this->threshold)
return 0;
if ($this->maxbrightness)
$sum = 255;
if (!$this->colors)
return $sum;
switch ($alpha) {
case 0:
return array("red" => $sum, "green" => $sum, "blue" => 0);
case 45: /* red */
return array("red" => $sum, "green" => 0, "blue" => 0);
case 90: /* blue */
return array("red" => 0, "green" => 0, "blue" => $sum);
case 135: /* green */
return array("red" => 0, "green" => $sum, "blue" => 0);
default:
throw new AlgorithmException("Impossible.");
}
}
}
© 2009 Julian von Mendel (http://derjulian.net) | Datum: 16.01.2025