我想设计一个程序,可以帮助我在5种预定义的颜色中评估哪一种更类似于可变颜色,以及与可变颜色的百分比。问题是我不知道如何手动一步一步地做到这一点。所以想一个程序就更难了。

更多细节:颜色来自不同颜色的管子和凝胶的照片。我有5个不同颜色的管子,每个代表5个等级中的1个。我想拍摄其他样本的照片,然后在电脑上通过比较颜色来评估样本属于哪个级别,我也想知道一个近似的百分比。我想要一个这样做的程序:http://www.colortools.net/color_matcher.html

如果你能告诉我该采取什么步骤,即使它们需要我手动思考和执行。那会很有帮助的。


当前回答

通过人类感知来比较两种颜色的最佳方法之一是CIE76。这个差值叫做e。当小于1时,人眼无法识别差异。

有一个很棒的颜色工具类ColorUtils(代码如下),它包括CIE76比较方法。作者是苏黎世大学的丹尼尔·斯特雷贝尔。

从ColorUtils.class我使用的方法:

static double colorDifference(int r1, int g1, int b1, int r2, int g2, int b2)

r1,g1,b1 -第一种颜色的RGB值

r2,g2,b2 -您想比较的第二个颜色的RGB值

如果你使用Android,你可以得到这样的值:

r1 = Color.red(像素);

g1 = Color.green(像素);

b1 = Color.blue(像素);


ColorUtils.class作者:Daniel Strebel,苏黎世大学:

import android.graphics.Color;

public class ColorUtil {
public static int argb(int R, int G, int B) {
    return argb(Byte.MAX_VALUE, R, G, B);
}

public static int argb(int A, int R, int G, int B) {
    byte[] colorByteArr = {(byte) A, (byte) R, (byte) G, (byte) B};
    return byteArrToInt(colorByteArr);
}

public static int[] rgb(int argb) {
    return new int[]{(argb >> 16) & 0xFF, (argb >> 8) & 0xFF, argb & 0xFF};
}

public static int byteArrToInt(byte[] colorByteArr) {
    return (colorByteArr[0] << 24) + ((colorByteArr[1] & 0xFF) << 16)
            + ((colorByteArr[2] & 0xFF) << 8) + (colorByteArr[3] & 0xFF);
}

public static int[] rgb2lab(int R, int G, int B) {
    //http://www.brucelindbloom.com

    float r, g, b, X, Y, Z, fx, fy, fz, xr, yr, zr;
    float Ls, as, bs;
    float eps = 216.f / 24389.f;
    float k = 24389.f / 27.f;

    float Xr = 0.964221f;  // reference white D50
    float Yr = 1.0f;
    float Zr = 0.825211f;

    // RGB to XYZ
    r = R / 255.f; //R 0..1
    g = G / 255.f; //G 0..1
    b = B / 255.f; //B 0..1

    // assuming sRGB (D65)
    if (r <= 0.04045)
        r = r / 12;
    else
        r = (float) Math.pow((r + 0.055) / 1.055, 2.4);

    if (g <= 0.04045)
        g = g / 12;
    else
        g = (float) Math.pow((g + 0.055) / 1.055, 2.4);

    if (b <= 0.04045)
        b = b / 12;
    else
        b = (float) Math.pow((b + 0.055) / 1.055, 2.4);


    X = 0.436052025f * r + 0.385081593f * g + 0.143087414f * b;
    Y = 0.222491598f * r + 0.71688606f * g + 0.060621486f * b;
    Z = 0.013929122f * r + 0.097097002f * g + 0.71418547f * b;

    // XYZ to Lab
    xr = X / Xr;
    yr = Y / Yr;
    zr = Z / Zr;

    if (xr > eps)
        fx = (float) Math.pow(xr, 1 / 3.);
    else
        fx = (float) ((k * xr + 16.) / 116.);

    if (yr > eps)
        fy = (float) Math.pow(yr, 1 / 3.);
    else
        fy = (float) ((k * yr + 16.) / 116.);

    if (zr > eps)
        fz = (float) Math.pow(zr, 1 / 3.);
    else
        fz = (float) ((k * zr + 16.) / 116);

    Ls = (116 * fy) - 16;
    as = 500 * (fx - fy);
    bs = 200 * (fy - fz);

    int[] lab = new int[3];
    lab[0] = (int) (2.55 * Ls + .5);
    lab[1] = (int) (as + .5);
    lab[2] = (int) (bs + .5);
    return lab;
}

/**
 * Computes the difference between two RGB colors by converting them to the L*a*b scale and
 * comparing them using the CIE76 algorithm { http://en.wikipedia.org/wiki/Color_difference#CIE76}
 */
public static double getColorDifference(int a, int b) {
    int r1, g1, b1, r2, g2, b2;
    r1 = Color.red(a);
    g1 = Color.green(a);
    b1 = Color.blue(a);
    r2 = Color.red(b);
    g2 = Color.green(b);
    b2 = Color.blue(b);
    int[] lab1 = rgb2lab(r1, g1, b1);
    int[] lab2 = rgb2lab(r2, g2, b2);
    return Math.sqrt(Math.pow(lab2[0] - lab1[0], 2) + Math.pow(lab2[1] - lab1[1], 2) + Math.pow(lab2[2] - lab1[2], 2));
}
}

其他回答

请参阅维基百科关于色差的文章以获得正确的线索。 基本上,你想要在多维颜色空间中计算一个距离度量。

但是RGB并不是“感知上一致的”,所以Vadim建议的欧几里得RGB距离度量将与人类感知的颜色之间的距离不匹配。首先,L*a*b*是一个感知上均匀的颜色空间,delta度量是常用的。但有更精致的色彩空间和更精致的delta公式,更接近人类的感知。

你需要学习更多关于颜色空间和光源的知识来进行转换。但如果想要一个比欧几里得RGB度量更好的快速公式,只需这样做:

假设你的RGB值在sRGB颜色空间中 找到sRGB到L*a*b*的转换公式 将sRGB颜色转换为L*a*b* 计算两个L*a*b*值之间的delta

计算成本不高,只是一些非线性公式和一些乘法和加法。

只是另一个答案,尽管它与Supr的答案相似-只是不同的颜色空间。

问题是:人类感知颜色的差异并不均匀,而RGB颜色空间忽略了这一点。因此,如果你使用RGB颜色空间,只是计算两种颜色之间的欧几里得距离,你可能会得到一个在数学上绝对正确的差异,但与人类告诉你的不一致。

This may not be a problem - the difference is not that large I think, but if you want to solve this "better" you should convert your RGB colors into a color space that was specifically designed to avoid the above problem. There are several ones, improvements from earlier models (since this is based on human perception we need to measure the "correct" values based on experimental data). There's the Lab colorspace which I think would be the best although a bit complicated to convert it to. Simpler would be the CIE XYZ one.

这里有一个网站列出了在不同颜色空间之间转换的公式,所以你可以尝试一下。

您需要将任何RGB颜色转换为Lab颜色空间,以便能够以人类看到它们的方式进行比较。否则你会得到RGB颜色“匹配”在一些非常奇怪的方式。

关于颜色差异的维基百科链接向您介绍了多年来定义的各种Lab颜色空间差异算法。最简单的方法是检查两种实验室颜色的欧几里得距离,可以工作,但有一些缺陷。

在OpenIMAJ项目中有一个更复杂的CIEDE2000算法的Java实现。提供你的两组Lab颜色,它会给你一个距离值。

一个只使用RGB的简单方法是

cR=R1-R2 
cG=G1-G2 
cB=B1-B2 
uR=R1+R2 
distance=cR*cR*(2+uR/256) + cG*cG*4 + cB*cB*(2+(255-uR)/256)

我已经使用这个工具有一段时间了,它可以很好地用于大多数目的。

我猜你最后想分析一幅完整的图像,对吧?所以你可以检查单位颜色矩阵的最小/最大差值。

大多数处理图形的数学操作都使用矩阵,因为使用矩阵的可能算法通常比经典的逐点距离和比较计算更快。(例如,对于使用DirectX, OpenGL,…的操作)

所以我认为你应该从这里开始:

http://en.wikipedia.org/wiki/Identity_matrix

http://en.wikipedia.org/wiki/Matrix_difference_equation

…正如Beska在上面评论的那样:

这可能不会带来最好的“可见”差异……

这也意味着,如果你在处理图像,你的算法取决于你对“相似”的定义。