1. applicable scenarios: list the corresponding images by color. 2. main program and API explanation: [php] & lt ;? PhpclassMajorColor {// reference color protected $ _ colorsnull; // The tolerance80 tolerance80; // The ignored color protected...
1. applicable scenarios: list the corresponding images by color.
II. main program and API explanation:
[Php]
Class MajorColor
{
// Reference color
Protected $ _ colors = null;
// Tolerance
Protected $ _ tolerance = 80;
// Ignored color
Protected $ _ ignoreColors = array ();
// Supported Image types
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 [1];
} Else {
Return $ val >=$ confVal-$ this-> _ tolerance & $ val <= $ confVal + $ this-> _ 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 <$ width; $ I ++ ){
For ($ m = 0; $ m <$ height; $ m ++ ){
$ Color_index = imagecolorat ($ im, $ I, $ m );
$ Color_tran = imagecolorsforindex ($ im, $ color_index );
$ Alpha = $ color_tran ['alpha'];
Unset ($ color_tran ['alpha']);
If (100 <$ alpha | in_array ($ color_tran, $ this-> _ 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];
}
}
1. void setColors (array $ colors)
Set the optional color, that is, all colors under "all colors" (white, gray, black ...)
2. void setTolerance (int $ tolerance)
Set the tolerance. for example, if the green RGB value is (0,255, 0) and the tolerance is set to 40, then, all colors in the range of-40 <= R <40 & 215 <= G <= 295 &-40 <= B <= 40 will be treated as green.
This method is used to roughly differentiate colors.
3. void setIgnoreColors (array $ colors)
Set unnecessary colors. For example, the background of most images is white, but we obviously do not want the result to be white. in this case, you can use this method to make it white.
4. array getOrderedColors ($ pic)
Obtain the number of matches of various colors (set with setColors) based on $ pic, and sort by matching volume from high to low
The parameter $ pic is the path of the image to be detected.
5. mix getMajorColor ($ pic)
Call getOrderedColors internally to return the key of the color with the highest matching quantity.
III. determine the format and scope of $ colors
1. if the color differences in $ colors are obvious, we only need to pass in the color value. internally, we will differentiate the colors based on the tolerance set by setTolerance.
[Php]
$ 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 ),
);
2. the setTolerance setting method can only distinguish between different colors. if you need more precise control, you need to set the R, G, and B ranges of a certain color to www.2cto.com.
[Php]
$ 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 )),
);
A series of fine-tuning is required until various colors can be clearly distinguished.
Author: xiaodao1986