Steve on Image Processing with MATLAB

Image processing concepts, algorithms, and MATLAB

Making Color Spectrum Plots – Part 2

It was a while ago now, but on4月27日I started explaining how I made this plot, which is from偶像3E(使用MATLAB的数字图像处理, 3rd ed.):

在今天的后续后,我将讨论如何计算频谱的颜色以显示在X轴下。我将使用并指的是几个偶像3Efunctions. These are available to you in马铃薯草Color Toolson theFile Exchange和上GitHub。The entire set of偶像3E功能也在上GitHub

Contents

可见波长

x轴限制应该是什么?换句话说,我们感兴趣的可见波长是什么?您将看到不同地方使用的不同限制。这里使用的限制380nm至780nm,是伯尔尼,R.S.(2000)中给出的限制。Billmeyer and Saltzman's Principals of Color Technology, 3rd ed., John Wiley & Sons, NJ.

lambda = 380:780;

Find XYZ values for the spectral colors

The first computational step is to find the XYZ values for each value of lambda. This computation can be found in the偶像3Efunctionlambda2xyz。但它真的很简单:只是插入CIE XYZ颜色匹配功能,由此返回偶像3EfunctionColorMatchingFunction.

T = colorMatchingFunctions; head(T)
ans = 8×4 table lambda x y z ______ __________ __________ __________ 360 0.0001299 3.917e-06 0.0006061 361 0.00014585 4.3936e-06 0.00068088 362 0.0001638 4.9296e-06 0.00076515 363 0.000184 5.5321e-06 0.00086001 364 0.00020669 6.2082e-06 0.00096659 365 0.0002321 6.965e-06 0.001086 366 0.00026073 7.8132e-06 0.0012206 367 0.00029308 8.7673e-06 0.0013727

找到特定Lambda的XYZ值,例如500,我们可以使用interp1.:

interp1.(T.lambda,[T.x,T.y,T.z],500)
ans = 0.0049 0.3230 0.2720

Or we can find the XYZ values for all of the wavelengths we are interested in.

xyz = interp1(t.lambda,[t.x,t.y,t.z],lambda(:));
plot(lambda(:),XYZ) title("XYZ values for spectral wavelengths") 传奇(“X”,“y”,“z”)

Try a simple XYZ to RGB conversion

让我们尝试使用图像处理工具箱函数简单地将这些XYZ值直接转换为RGBxyz2rgb

RGB = XYZ2RGB(XYZ);TrawColorbar(RGB)

(这function牵引彩色is below. It uses the偶像3EfunctioncolorSwatches。)

这对我来说看起来并不是一个非常好的光谱颜色。似乎不平衡,几个斑块似乎大多是一种颜色。这是怎么回事?如果我们绘制RGB值的线绘图,我们可以看到问题。(这Plotrgb.andShadegamutegion.functions are down below.)

close plotrgb(lambda(:),RGB) shadeGamutRegion

灰色阴影区域显示0到1之间的范围;这是可显示的颜色范围。在该范围之外的所有内容(负值或大于1的值)无法完全显示。这些超出范围的值被剪切到可显示范围,并导致效果不佳。

I'm going to show you the method used by the偶像3EfunctionspectrumColors。The method is a variation of the one described in: Andrew Young (2012).Rendering Spectra(https://ty.sdsu.edu/explain/optics/rendering.html.)。检索到2020年7月16日。

Work with linear RGB values

首先,让我们将XYZ值转换为“线性”RGB值。我们看到图像像素的典型RGB值是非线性地与光强度相关的,并且线性RGB值对于以下平均和缩放步骤更适合。图像处理工具箱功能xyz2rgb可以可选地转换为线性值。

RGB_LIN = XYZ2RGB(XYZ,“色彩”,'linear-rgb');Plotrgb(lambda(:),rgb_lin)title("Linear RGB values for spectral wavelengths")

Heuristic scaling of linear RGB values

我们希望修改这些曲线,使它们落在范围内,并产生合理准确,平稳地变化和有吸引力的光谱颜色表示。

The next thing we'll do is scale so that the maximum linear RGB value is 1.0. (Note: Young (2012) divides by a fixed value of 2.34.)

RGB_lin = RGB_lin / max(RGB_lin(:)); plotrgb(lambda(:),RGB_lin)

现在,一次一个组件,以及每个频谱颜色,用相同的y混合足够的中性灰度,使负组分值高达0。

Y = XYZ(:,2);fork = 1:3 C = RGB_lin(:,k); F = Y ./ (Y - C);%不需要扩展组件values that are already%非负面。f(c> = 0)= 1;RGB_LIN = Y + F. *(RGB_LIN  -  Y);endPlotrgb(lambda(:),rgb_lin)

Next, to get brighter spectral colors, including a good yellow, scale up the linear RGB values, allowing them to get higher than 1.0. Then, for each color, scale all components back down, if necessary, so that the maximum component value is 1.0. Note: [Young 2012] uses a scale factor of 1.85.

RGB_LIN = RGB_LIN * 2.5;s = max(rgb_lin,[],2);s = max(s,1);rgb_lin = rgb_lin ./ s;Plotrgb(lambda(:),rgb_lin)

Smooth the curves

平滑线性RGB曲线以消除不连续的第一衍生物。这有助于频谱看起来更平滑,减少急剧的过渡点。注意:此步骤不在[杨2012]中。

RGB_LIN = CONV2(RGB_LIN,ONE(21,1)/ 21,'same');Plotrgb(lambda(:),rgb_lin)

通过浮点循环消除小于1的小负数和数字略高于1。

rgb_lin = min(max(rgb_lin,0),1);

转换为最终结果的非线性RGB值

Convert to nonlinear sRGB values suitable for display on a computer monitor.

RGB = lin2rgb(RGB_lin); plotrgb(lambda(:),RGB)
TrawColorbar(RGB)

下次,我会讨论如何在图中绘制光谱色谱。

Utility functions

function牵引彩色(rgb_colors) f = figure; f.Position(4) = f.Position(4) / 5; colorSwatches(rgb_colors,0) daspect([40 1 1]) axisoffendfunctionPlotrgb(x,rgb_colors)% Pick the colors we want to use from the normal line color order.C =线(7);blue = c(1,:);Red = C(2,:);绿色= C(5,:);CLF Plot(x,rgb_colors(:,1),'颜色',red); holdonplot(x,rgb_colors(:,2),'颜色',green); plot(x,rgb_colors(:,3),'颜色',blue) holdoff网格on紧的yl = ylim;ylim([min(yl(1)-0.05,-0.05)max(yl(2)+0.05,1.05)])传奇("R","G","B") xlabel(“波长(NM)”)endfunctionShadegamutegion.xl = xlim; xx = [xl(1) xl(2) xl(2) xl(1) xl(1)]; yy = [0 0 1 1 0]; p = patch(xx,yy,[0.5 0.5 0.5],"FaceAlpha",0.1,......"EdgeAlpha",0.1,"HandleVisibility","off");end




发布MATLAB®R2020A

|
  • print
  • send email

Comments

要发表评论,请点击here要登录您的MathWorks帐户或创建新的。