note 96601 added to function.imageconvolution

From: Date: Sun, 07 Mar 2010 18:44:09 +0000
Subject: note 96601 added to function.imageconvolution
Groups: php.notes 
Request: Send a blank email to php-notes+get-166760@lists.php.net to get a copy of this message
Normalization is necessary to keep the image balanced (else any filter may quickly turn the image almost completely black or white). Here is a short, easy-to-use, class to handle normalization automatically and make for easier input of the 3x3 matrix: The code respects the "array of three arrays" syntax for use with the imageconvolution() function and automatically calculates the necesarry divisor for normalization. <?php class ConvolutionFilter { public $matrix; public $div; public function computeDiv() { $this->div = array_sum ($this->matrix[0]) + array_sum ($this->matrix[1]) + array_sum ($this->matrix[2]); } function __construct() { $matrix = func_get_args(); $this->matrix = array( array($matrix[0], $matrix[1], $matrix[2]), array($matrix[3], $matrix[4], $matrix[5]), array($matrix[6], $matrix[7], $matrix[8]) ); $this->computeDiv(); } } ?> Example usage: <?php $gaussianFilter = new ConvolutionFilter( 1.0, 2.0, 1.0, 2.0, 3.0, 2.0, 1.0, 2.0, 1.0 ); imageconvolution($image, $gaussianFilter->matrix, $gaussianFilter->div, 0); ?> Some common filters: <?php $identityFilter = new ConvolutionFilter( 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 ); $sharpenFilter = new ConvolutionFilter( 0.0, -1.0, 0.0, -1.0, 5.0, -1.0, 0.0, -1.0, 0.0 ); $edgeFilter = new ConvolutionFilter( 0.0, 1.0, 0.0, 1.0, -4.0, 1.0, 0.0, 1.0, 0.0 ); $findEdgesFilter = new ConvolutionFilter( -1.0, -1.0, -1.0, -2.0, 8.0, -1.0, -1.0, -1.0, -1.0 ); ?> Remember you can use imagefilter() for such basic needs but the above class will make it easier for you when you want to create your own filters. ---- Server IP: 89.31.146.176 Probable Submitter: 88.178.238.198 ---- Manual Page -- http://www.php.net/manual/en/function.imageconvolution.php Edit -- https://master.php.net/note/edit/96601 Del: integrated -- https://master.php.net/note/delete/96601/integrated Del: useless -- https://master.php.net/note/delete/96601/useless Del: bad code -- https://master.php.net/note/delete/96601/bad+code Del: spam -- https://master.php.net/note/delete/96601/spam Del: non-english -- https://master.php.net/note/delete/96601/non-english Del: in docs -- https://master.php.net/note/delete/96601/in+docs Del: other reasons-- https://master.php.net/note/delete/96601 Reject -- https://master.php.net/note/reject/96601 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166760) next »