Steve on Image Processing with MATLAB

Image processing concepts, algorithms, and MATLAB

How to Compute Perceptual Color Difference

I以前写道about the newColorChecker., which can detect X-Rite test charts in the R2020b release. Another area of new color-related functionality is computing perceptual color differences. There is the new function斯蒂根,基于L * A * B *颜色空间和CIE76标准计算颜色差异。还有imcolordifffunction, which can compute color differences based either on the CIE94 or the CIEDE2000 standard.

I recently mentioned to someone on the Image Processing Toolbox team that I was planning to write a blog post about these functions, and he posed the following question: which two colors are the furthest apart, perceptually speaking? I decided to give that a try using $\Delta_E$. I'll formulate the problem this way: which two sRGB colors have the largest $\Delta_E$ between them?

In a related post back in 2015, I demonstrated how to plot the sRGB gamut surface in L*a*b* space, like this:

看了这个图表,你可能能够猜出答案。但是,让我们通过它。我要做的第一件事就是对SRGB空间进行采样。

[r,g,b] = ndgrid(linspace(0,1,100));rgb = [r(:),g(:),b(:)];

Next, convert the sRGB colors to Lab. (I'm going to stop typing the "*" characters.)

lab = rgb2lab(rgb);

Now let's find the colors on the convex hull of the Lab values. The pair of colors most distant from each other must both be on the convex hull.

k = convhull(lab); hull_colors = lab(unique(k(:,1)),:);

If this were a two-dimensional problem, then I know of an algorithm that can find the most distant pair of points in $O(N)$, where $N$ is the number of points on the convex hull. I don't know of a similar algorithm in three dimensions, so I'm just going to brute force it by computing the distance between every color pair.

So, how many colors are we talking about?

尺寸(Hull_colors)
ans = 3980 3

There are 3908 colors, each of which is represented by a 3-element vector in Lab space. The color difference $\Delta_E$ is the Euclidean distance between two colors in Lab space. I want to compute the distance between every color in that set and every other color. Here is a nifty and compact way to do that using the implicit expansion behavior of MATLAB arithmetic operators.

C1 = RESHAPE(HULL_COLORS,[],1,3);C2 = REPAPE(HULL_COLORS,1,[],3);Cdiff = C1  -  C2;de = sqrt(sum (daid ^ 2,3));尺寸(de)
ans = 3980 3980

You can see thatDEis an 3980x3980 matrix. It contains the pair-wise distances between all the convex hull colors. (See alsopdist2, a function in the Statistics and Machine Learning Toolbox that can compute pair-wise vector distances with much more flexibility, as well ashypot,一个Matlab函数,以避免数值溢出和下溢问题的方式计算两个元素矢量的大小。)

让我们找到该矩阵中最大距离的位置。

[m,n] = find(de == max(de(:)))
m = 3883 92 n = 92 3883

这些颜色(在实验室空间)是什么?

max_colors_lab = hull_colors(m,:)
MAX_COLORS_LAB = 32.2970 79.1875 -107.8602 87.7347 -86.1827 83.1793

和SRGB空间中的两种颜色:

max_colors_rgb = lab2rgb(max_colors_lab)
max_colors_rgb = 0.0000 -0.0000 1.0000 0.0000 1.0000 0.0000

它们只是SRGB绿色和蓝色原初。

colorSwatches(max_colors_rgb) axisequal离开

(colorSwatchesis aDipum3e.您可以使用的功能matlab彩色工具在这方面File Exchangeand onGitHub。)

These two colors have a $\Delta_E$ of:

de(m(1),n(1))
ans = 258.6827.

Deltae函数提供更方便的方式来计算$ \ delta_e $:

Deltae([01 0],[0 0 1])
ans = 258.6827.

有人,我会写下提供的其他色差计算imcolordiff.




发布与MATLAB®R2020B

|
  • print
  • send email

注释

To leave a comment, please click这里to sign in to your MathWorks Account or create a new one.