RGB到最接近的预定义颜色 [英] RGB to closest predefined color

查看:187
本文介绍了RGB到最接近的预定义颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:



由于我提供了此功能的答案

  function grabclosestcolor($ r,$ g,$ b){
$ colors = array(array(124,12,12),array(7,7,11),array (110,224,219),阵列(123,123,123),阵列(124,177,74),阵列(130,86,53),阵列(77,77,77),阵列(164,124,68),阵列(204,196,132) ,阵列(163,123,67),阵列(26,122,26),阵列(195,195,50),阵列(193,193,193),阵列(255,248,73),阵列(243,243,243)
$ differentialarray = array();
foreach($ colors为$ value){
$ difference = sqrt(pow($ r- $ value [0],2)+ pow($ g- $ value [1],2) pow($ b- $ value [2],2));
array_push($ differencearray,$ difference);
$ smallest = min($ differencearray);
$ key = array_search($ smallest,$ differencearray);
return $ colors [$ key];
}
}



我的目标是。我抓住一个图片,并循环通过每个像素,抓住它的x,y和rgb。



而不是只抓住rgb,我有一个预定义的数组,寻找从我抓到预定义数组的颜色最接近的匹配。
这里的目标是只使用预定义数组中的颜色。
这里是我的颜色数组。

  $ colors = array(array(124,12,12),array (7,7,11),阵列(110,224,219),阵列(123,123,123),阵列(124,177,74),阵列(130,86,53),阵列(77,77,77),阵列(164,124,68)阵列(204,196,132),阵列(164,148,147),阵列(163,123,67),阵列(26,122,26),阵列(195,195,50),阵列(193,193,193),阵列(243,243,243) 

这里是我现有的代码遍历所有。

  $ int = imagesx($ im) -  1; 
$ int2 = imagesy($ im) - 1;
$ start2 = 0;
do {
$ start = 0;
do {
$ rgb = imagecolorat($ im,$ start,$ start2);
$ r =($ rgb>> 16)& 0xFF;
$ g =($ rgb>> 8)& 0xFF;
$ b = $ rgb& 0xFF;
$ value = rgb2hex($ r,$ g,$ b):$ start:$ start2;
array_push($ colorsofimage,$ value);
} while($ int> $ start ++);
} while($ int2> $ start2 ++);

rgb2hex是用户定义的函数,但是我想要完成的是用函数以获取最接近的颜色。



$ colorsofimage包含每个像素的数组信息:hex:x:y
我想要的是rgb2hex(NEWFUNCTION ($ r,$ g,$ b));
所以新的十六进制是预定义数组中的1。



我希望你明白,因为我没有线索如何做,不知道颜色的算法。

解决方案

你必须计算到每种颜色的距离, / p>

有几种方法可以做到这一点。一个简单的方法是计算距离:

  sqrt((r-r1)^ 2 +(g-g1 )^ 2 +(b-b1)^ 2)

值来计算距离,例如当转换RGB-> YUV时使用的值:

  Y = 0.299 * R + 0.587 * G + 0.114 * B 

在这种情况下,您将使用

  sqrt(((r-r1)* .299)^ 2 +((g- g1)* .587)^ 2 +((b- b1)*。 114)^ 2)

当然,因为你不需要确切的距离, ,你可以,也许应该跳过平方根,做最后一个计算:

 ((r  -  r1)* .299 )^ 2 +((g-g1)* .587)^ 2 +((b-b1)* .114)^ 2 


Edit:

With the answer given I made this function

function grabclosestcolor($r, $g, $b){
    $colors = array(array(124,12,12),array(7,7,11),array(110,224,219),array(123,123,123),array(124,177,74),array(130,86,53),array(77,77,77),array(164,124,68),array(204,196,132),array(164,148,147),array(163,123,67),array(26,122,26), array(195,195,50),array(193,193,193),array(255,248,73),array(243,243,243));
    $differencearray = array();
    foreach ($colors as $value) {
        $difference = sqrt(pow($r-$value[0],2)+pow($g-$value[1],2)+pow($b-$value[2],2));
        array_push($differencearray, $difference);
        $smallest = min($differencearray);
        $key = array_search($smallest, $differencearray);
        return $colors[$key];
        }
    }


My goal is this. I grab a picture and loop through each pixel and grab its x,y, and rgb.

Instead of just grabbing the rgb, I have a predefined array and I'm looking for the closest match from the color I grabbed to the predefined array. The goal here is to only use colors from the predefined array. Here is my array of colors.

$colors = array(array(124,12,12),array(7,7,11),array(110,224,219),array(123,123,123),array(124,177,74),array(130,86,53),array(77,77,77),array(164,124,68),array(204,196,132),array(164,148,147),array(163,123,67),array(26,122,26), array(195,195,50),array(193,193,193),array(255,248,73),array(243,243,243));

and here is my existing code that loops through it all.

$int = imagesx($im) - 1;
$int2 = imagesy($im) - 1;
$start2 = 0;
do{
    $start = 0;
    do{
        $rgb = imagecolorat($im, $start, $start2);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        $value = rgb2hex($r,$g,$b).":$start:$start2";
        array_push($colorsofimage, $value);
    } while($int > $start++);
} while($int2 > $start2++);

rgb2hex is a User Defined Function, but what I want to accomplish is to change that function with the function to grab the closest color.

$colorsofimage contains an array of each pixels info with hex:x:y what i want it to be is rgb2hex(NEWFUNCTION($r,$g,$b)); So that the new hex is the 1 out of the predefined array.

I hope you understood, because I have no clue how to do it because I don't know the algorithm of a color.

解决方案

You have to calculate the distance to each color, and pick the smallest.

There are a few ways to do this. A simple method would be to calculate the distance would be:

sqrt((r-r1)^2+(g-g1)^2+(b-b1)^2)

A better method might be to incorporate the weighted values to calculate a distance, for instance the values used when converting RGB->YUV:

Y = 0.299 * R + 0.587 * G + 0.114 * B

in that case you would use

sqrt(((r - r1) * .299)^2 + ((g - g1) * .587)^2 + ((b - b1) * .114)^2)

Of course, since you don't need the exact distances, just a comparison, you can and probably should just skip the square root, making the last calculation:

((r - r1) * .299)^2 + ((g - g1) * .587)^2 + ((b - b1) * .114)^2

这篇关于RGB到最接近的预定义颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆