How to find the Dominant color in image?

后端 未结 9 599
花落未央
花落未央 2020-12-07 13:34

i want to find the dominant color in image, how can i do it ?

it would be great if i can get this in HEX code (exm: #eeeeee)

9条回答
  •  余生分开走
    2020-12-07 14:14

    In regard to tkone answer, the $max is just a parameter showing density of the color at an image. I would change the code a bit to return the HEX color:

    
    
    
    
    Empty Document
    
    
    
    ";
                    // extract each value for r, g, b
    
                    $r = ($rgb >> 16) & 0xFF;
                    $g = ($rgb >> 8) & 0xFF;
                    $b = $rgb & 0xFF;
    
                    // get the Value from the RGB value
    
                    $V = round(($r + $g + $b) / 3);
                    //echo $V."
    "; // add the point to the histogram $histo[$V] += $V / $n; $histo_color[$V] = rgb2hex([$r,$g,$b]); } } // find the maximum in the histogram in order to display a normated graph $max = 0; for ($i=0; $i<255; $i++) { if ($histo[$i] > $max) { $max = $histo[$i]; } } echo "
    "; for ($i=0; $i<255; $i++) { $val += $histo[$i]; $h = ( $histo[$i]/$max )*$maxheight; echo ""; } echo "
    "; $key = array_search ($max, $histo); $col = $histo_color[$key]; ?>

    Also, it is worth mentioning that this is just the most 'repeated' color at the image that cannot absolutely considered 'dominant' color.

提交回复
热议问题