Created
July 30, 2013 09:28
-
-
Save andriesss/6111551 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$img = new Imagick(); | |
$img->readImage('car.jpg'); | |
$draw = new ImagickDraw(); | |
/* | |
* Contrast enhancement | |
* | |
* Contrast of each image is enhanced through histogram equalization technique. This method usually increases the | |
* global contrast of many images, especially when the usable data of the image is represented by close contrast values. | |
* | |
* http://en.wikipedia.org/wiki/Histogram_equalization | |
*/ | |
$img->equalizeImage(); | |
/* | |
* Median filtering | |
* | |
* Replaces the gray value of a pixel by the median of the | |
* gray values of its neighbors. We have used 3 × 3 masks to get eight neighbors of a pixel and their | |
* corresponding gray values. This operation removes salt-and-peeper noise from the image. | |
*/ | |
$img->medianFilterImage(3); | |
// Convert to 8-bit gray scale | |
$img->quantizeimage(256, Imagick::COLORSPACE_GRAY, 8, true, true); | |
// 3x3 gx Sobel mask for vertical edge detection | |
$gy = array( | |
array(-1, 0, 1), | |
array(-2, 0, 2), | |
array(-1, 0, 1), | |
); | |
// edge detection using Sobel mask | |
$it = $img->getPixelIterator(); | |
foreach ($it as $y => $pixels) { | |
/** @var $pixel \ImagickPixel */ | |
foreach ($pixels as $x => $pixel) { | |
$rawColour = $pixel->getcolor(); | |
$sumY = 0; | |
for ($i = -1; $i <= 1; $i++) { | |
for ($j = -1; $j <= 1; $j++) { | |
$rawColour = $img->getimagepixelcolor($x + $i, $y + $j)->getcolor(); | |
$r = $rawColour['r']; | |
$g = $rawColour['g']; | |
$b = $rawColour['b']; | |
$rgb = ($r + $g + $b); | |
$sumY += ($rgb * $gy[$i+1][$j+1]) / 4; | |
} | |
} | |
// combine gradient approximations to give the gradient magnitude | |
$gray = abs(sqrt($sumY * $sumY)); | |
if ($gray > 255) { | |
$gray = 255; | |
} | |
$pix = new ImagickPixel("rgb($gray, $gray, $gray)"); | |
$draw->setFillColor($pix); | |
$draw->point($x, $y); | |
} | |
$it->syncIterator(); | |
echo "computed column {$y}\n"; | |
} | |
$img->drawImage($draw); | |
$img->writeimage('temp.jpg'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fwiw, this is only an experiment, I do not advice to use this code in production, PHP is not the right tool for this :)