Home > Backend Development > PHP Tutorial > 【PHP】判别图片主色调

【PHP】判别图片主色调

WBOY
Release: 2016-06-13 10:36:51
Original
848 people have browsed it

【PHP】识别图片主色调

一、适用情景:如http://www.teapic.com/list.htm,根据颜色列出相应图片。



二、主程序及API解释:

<?phpclass MajorColor{	//参考颜色	protected $_colors = null;		//容差	protected $_tolerance = 80;		//忽略的颜色	protected $_ignoreColors = array();		//支持的图片类型	protected $_funcs = array('image/png' => 'imagecreatefrompng', 'image/jpeg' => 'imagecreatefromjpeg', 'image/gif' => 'imagecreatefromgif');		public function __construct(array $colors = null) {		if(null !== $colors) {			$this->_colors = $colors;		}	}		public function setColors(array $colors) {		$this->_colors = $colors;	}		public function setTolerance($tolerance) {		$this->_tolerance = $tolerance;	}		public function setIgnoreColors($colors) {		$this->_ignoreColors = $colors;	}		public function _isValidColor($confVal, $val) {		if(is_array($confVal)) {			return $val >= $confVal[0] && $val = $confVal - $this->_tolerance && $val _tolerance;		}	}		public function getOrderedColors($pic) {		$size = getimagesize($pic);		if(!$size) {			return false;		}				$width = $size[0];		$height = $size[1];		$mime = $size['mime'];		$func = isset($this->_funcs[$mime]) ? $this->_funcs[$mime] : null;		if(!$func) {			return false;		}				$im = $func($pic);		if(!$im) {			return false;		}		$total = $width * $height;		$nums = array();		for($i = 0; $i _ignoreColors)) {					continue;				}				foreach ($this->_colors as $colorid => $color) {					if($this->_isValidColor($color['red'], $color_tran['red'])						&& $this->_isValidColor($color['green'], $color_tran['green'])						&& $this->_isValidColor($color['blue'], $color_tran['blue'])					) {						$nums[$colorid] = isset($nums[$colorid]) ? $nums[$colorid] + 1 : 1;					}				}			}		}				imagedestroy($im);		arsort($nums);		return $nums;	}		public function getMajorColor($pic) {		$nums = $this->getOrderedColors($pic);		$keys = array_keys($nums);		return $keys[0];	}}
Copy after login

1.void setColors(array $colors)

设置可选颜色,即上图中“全部颜色”下的所有颜色(白、灰、黑...)


2.void setTolerance(int $tolerance)

设置容差,比如绿色的RGB值为(0,255,0),如果设置容差为40,那么-40

此方法用于大致区别各颜色。


3.void setIgnoreColors(array $colors)

设置不需考虑的颜色。如大多图片的背景是白色,而我们显然不希望结果是白色,此时可调用此方法简略白色。


4.array getOrderedColors($pic)

根据$pic获取各种颜色(用setColors设置的颜色)的匹配数量,按匹配量由高到低排列

参数$pic是待检测图片的路径


5.mix getMajorColor($pic)

内部调用getOrderedColors,返回匹配量最高的颜色的key


三、$colors的格式及范围确定

1.如果$colors中的各种颜色差别很明显,我们只需简单的传入颜色值,内部会根据setTolerance设置的容差来区别各颜色。

$colors = array(	1 => array('red' => 0xff, 'green' => 0xff, 'blue' => 0xff),	2 => array('red' => 0xc0, 'green' => 0xc0, 'blue' => 0xc0),	2 => array('red' => 0x00, 'green' => 0x00, 'blue' => 0x00),);
Copy after login


2.setTolerance设置容差的方法只能大致区分各种颜色,如果需要更精确的控制,则需要分别设置某一颜色的R、G、B范围

$colors = array(	1 => array('red' => array(189, 230), 'green' => array(189, 230), 'blue' => array(189, 230)),	2 => array('red' => array(0, 37), 'green' => array(0, 37), 'blue' => array(0, 37)),	3 => array('red' => array(128, 255), 'green' => array(0, 76), 'blue' => array(0, 100)),);
Copy after login
需要进行一系列的微调,直至能明显区分各种颜色。


四、demo下载

http://download.csdn.net/detail/xiaodao1986/4479821

积分不够了,赚点积分。真没积分的朋友可以联系我QQ:88433062

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template