我想设计一个程序,可以帮助我在5种预定义的颜色中评估哪一种更类似于可变颜色,以及与可变颜色的百分比。问题是我不知道如何手动一步一步地做到这一点。所以想一个程序就更难了。
更多细节:颜色来自不同颜色的管子和凝胶的照片。我有5个不同颜色的管子,每个代表5个等级中的1个。我想拍摄其他样本的照片,然后在电脑上通过比较颜色来评估样本属于哪个级别,我也想知道一个近似的百分比。我想要一个这样做的程序:http://www.colortools.net/color_matcher.html
如果你能告诉我该采取什么步骤,即使它们需要我手动思考和执行。那会很有帮助的。
对于快速和肮脏,你可以做到
import java.awt.Color;
private Color dropPrecision(Color c,int threshold){
return new Color((c.getRed()/threshold),
(c.getGreen()/threshold),
(c.getBlue()/threshold));
}
public boolean inThreshold(Color _1,Color _2,int threshold){
return dropPrecision(_1,threshold)==dropPrecision(_2,threshold);
}
利用整数除法对颜色进行量化。
我在我的android up中使用了这个,它似乎令人满意,尽管不建议使用RGB空间:
public double colourDistance(int red1,int green1, int blue1, int red2, int green2, int blue2)
{
double rmean = ( red1 + red2 )/2;
int r = red1 - red2;
int g = green1 - green2;
int b = blue1 - blue2;
double weightR = 2 + rmean/256;
double weightG = 4.0;
double weightB = 2 + (255-rmean)/256;
return Math.sqrt(weightR*r*r + weightG*g*g + weightB*b*b);
}
然后我用下面的方法得到相似度的百分比:
double maxColDist = 764.8339663572415;
double d1 = colourDistance(red1,green1,blue1,red2,green2,blue2);
String s1 = (int) Math.round(((maxColDist-d1)/maxColDist)*100) + "% match";
它工作得很好。
如果你有两个颜色对象c1和c2,你可以比较c1和c2的每个RGB值。
int diffRed = Math.abs(c1.getRed() - c2.getRed());
int diffGreen = Math.abs(c1.getGreen() - c2.getGreen());
int diffBlue = Math.abs(c1.getBlue() - c2.getBlue());
你可以将这些值除以饱和度的差异(255),你就会得到两者之间的差异。
float pctDiffRed = (float)diffRed / 255;
float pctDiffGreen = (float)diffGreen / 255;
float pctDiffBlue = (float)diffBlue / 255;
之后你就可以找到平均色差的百分比。
(pctDiffRed + pctDiffGreen + pctDiffBlue) / 3 * 100
这就得到了c和c之间的百分比差。
Kotlin版本与你想匹配的百分比有多少。
方法调用,参数为percent
isMatchingColor(intColor1, intColor2, 95) // should match color if 95% similar
方法体
private fun isMatchingColor(intColor1: Int, intColor2: Int, percent: Int = 90): Boolean {
val threadSold = 255 - (255 / 100f * percent)
val diffAlpha = abs(Color.alpha(intColor1) - Color.alpha(intColor2))
val diffRed = abs(Color.red(intColor1) - Color.red(intColor2))
val diffGreen = abs(Color.green(intColor1) - Color.green(intColor2))
val diffBlue = abs(Color.blue(intColor1) - Color.blue(intColor2))
if (diffAlpha > threadSold) {
return false
}
if (diffRed > threadSold) {
return false
}
if (diffGreen > threadSold) {
return false
}
if (diffBlue > threadSold) {
return false
}
return true
}
请参阅维基百科关于色差的文章以获得正确的线索。
基本上,你想要在多维颜色空间中计算一个距离度量。
但是RGB并不是“感知上一致的”,所以Vadim建议的欧几里得RGB距离度量将与人类感知的颜色之间的距离不匹配。首先,L*a*b*是一个感知上均匀的颜色空间,delta度量是常用的。但有更精致的色彩空间和更精致的delta公式,更接近人类的感知。
你需要学习更多关于颜色空间和光源的知识来进行转换。但如果想要一个比欧几里得RGB度量更好的快速公式,只需这样做:
假设你的RGB值在sRGB颜色空间中
找到sRGB到L*a*b*的转换公式
将sRGB颜色转换为L*a*b*
计算两个L*a*b*值之间的delta
计算成本不高,只是一些非线性公式和一些乘法和加法。