How to compare two colors for similarity/differenc

2019-01-01 06:19发布

I want to design a program that can help me assess between 5 pre-defined colors which one is more similar to a variable color, and with what percentage. The thing is that I don't know how to do that manually step by step. So it is even more difficult to think of a program.

More details: The colors are from photographs of tubes with gel that as different colors. I have 5 tubes with different colors were each is representative of 1 of 5 levels. I want to take photographs of other samples and on the computer assess to which level that sample belongs by comparing colors, and I want to know that with a percentage of approximation too. I would like a program that does something like this: http://www.colortools.net/color_matcher.html

If you can tell me what steps to take, even if they are things for me to think and do manually. It would be very helpful.

15条回答
泪湿衣
2楼-- · 2019-01-01 07:07

You'll need to convert any RGB colors into the Lab color space to be able to compare them in the way that humans see them. Otherwise you'll be getting RGB colors that 'match' in some very strange ways.

The wikipedia link on Color Differences gives you an intro into the various Lab color space difference algorithms that have been defined over the years. The simplest that just checks the Euclidian distance of two lab colours, works but has a few flaws.

Conveniently there's a Java implementation of the more sophisticated CIEDE2000 algorithm in the OpenIMAJ project. Provide it your two sets of Lab colours and it'll give you back single distance value.

查看更多
残风、尘缘若梦
3楼-- · 2019-01-01 07:11

Just an idea that first came to my mind (sorry if stupid). Three components of colors can be assumed 3D coordinates of points and then you could calculate distance between points.

F.E.

Point1 has R1 G1 B1
Point2 has R2 G2 B2

Distance between colors is

d=sqrt((r2-r1)^2+(g2-g1)^2+(b2-b1)^2)

Percentage is

p=d/sqrt((255)^2+(255)^2+(255)^2)
查看更多
何处买醉
4楼-- · 2019-01-01 07:13

For quick and dirty, you can do

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);
}

making use of integer division to quantize the colors.

查看更多
登录 后发表回答