开发思路:自动调整曝光
1.根据图片中最多的色彩HSL值中的亮度(l:0~1)判断,超过0.6为过曝,不足0.4为欠曝
2.计算(亮度-0.5)的绝对值,计算曝光调整范围并修正图片
3.欠曝补偿范围0~100
4.过曝下降范围0~5
5.调整图片曝光值php
一 自定义图片处理类html
<?php /** * 自定义图片处理类 * @author Martin */ class MyImagick { public $image; public function __construct($file) { $this->image = new Imagick($file); } /** * 获取图像HSL信息 * @param type $num * @return type */ public function getImageHSL($num = 1) { $avater = clone $this->image; //获取 $num个主要色调 $avater->quantizeImage($num, Imagick::COLORSPACE_RGB, 0, false, false); $avater->uniqueImageColors(); $hslarr = array(); $it = $avater->getPixelIterator(); $it->resetIterator(); while ($row = $it->getNextIteratorRow()) { foreach ($row as $pixel) { $hslarr[] = $pixel->getHSL(); //获取hsl } } return $hslarr; } /** * 自动调整曝光 * 根据图片中最多的色彩HSL值中的亮度(l:0~1)判断 * 超过$exposure_high为过曝,不足$exposure_low为欠曝 * 计算(亮度-0.5)的绝对值,计算曝光调整范围并修正图片 * 欠曝补偿范围0~100 * 过曝下降范围0~5 * @author Martin.Ma 2016.9.2 */ public function autoExposure() { $exposure_low = 0.4;//这两个值内为正常曝光范围,可本身调整 $exposure_high = 0.6; $hsl = $this->getImageHSL(); $l = $hsl[0]['luminosity']; //亮度 if ($l > $exposure_high) { $constant = abs($l - 0.5) / 0.5 * 5; return $this->image->evaluateImage(Imagick::EVALUATE_POW, $constant); } if ($l < $exposure_low) { $constant = abs($l - 0.5) / 0.5 * 100; return $this->image->evaluateImage(Imagick::EVALUATE_LOG, $constant); } return true; } }
二 使用方法算法
$image = new MyImagick('h1.jpg'); //自动曝光算法 $image->autoExposure(); header('Content-type: image/jpeg'); echo $image->image;
三 结果实例(左侧为处理结果)windows
备注:
1.首先,你要安装Imagick这个库,关于如何安装,文后会附一个转载连接。
http://www.open-open.com/lib/...
2.开发环境:windows7 WAMP3.0.4 64位 PHP7.0.4this