Matlab 生成颜色三角形
我想生成一个背景颜色为rgb颜色的三角形。这用于显示颜色值 我能找出如何计算这样一个三角形的位置Matlab 生成颜色三角形,matlab,plot,Matlab,Plot,我想生成一个背景颜色为rgb颜色的三角形。这用于显示颜色值 我能找出如何计算这样一个三角形的位置 function [x,y] = RGBtoCartCoordXY(R, G, B) RGB = R + G + B; r = R / RGB; g = G / RGB; b = B / RGB; XYr = [-0.5; -0.5]; XYb = [+0.5; -0.5]; XYg = [+0.0; +0.5]; xy = (r * XYr + b * XYb + g * XYg) / ( r +
function [x,y] = RGBtoCartCoordXY(R, G, B)
RGB = R + G + B;
r = R / RGB; g = G / RGB; b = B / RGB;
XYr = [-0.5; -0.5]; XYb = [+0.5; -0.5]; XYg = [+0.0; +0.5];
xy = (r * XYr + b * XYb + g * XYg) / ( r + g + b );
x = xy(1); y = xy(2);
但是,我在绘图中用点绘制这样一个三角形的方法不是很成功,而且需要很长时间才能获得有用的点数(此处>100000)
似乎这些点有阴影,我不希望背景中有空白。显然,我希望这个图尽可能快,因为我想在上面画一条简单的曲线——这是图中的实际信息。
编辑:
该图基于以下代码:
for r = linspace(0,1,points)
for g = linspace(0,1,points)
for b = linspace(0,1,points)
index = index + 1;
RBGtriple(index, :) = [r g b];
end
end
end
for k = 1:size(RBGtriple,1)
R = RBGtriple(k,1);
G = RBGtriple(k,2);
B = RBGtriple(k,3);
[x y] = RGBtoCartCoordXY(R,G,B);
xaxis(k) = x;
data(k) = y;
end
hold on;
for k = 1:size(RBGtriple,1)
hplot = plot (xaxis(k), data(k));
set(hplot, ...
'Marker', 'square', ...
'LineStyle','none' , ...
'MarkerSize' ,3 , ...
'LineWidth' , 0.1, ...
'MarkerEdgeColor',RBGtriple(k, :) , ...
'MarkerFaceColor',RBGtriple(k, :));
end
hold off;
imgsize = 500;
for i=1:length(xaxis),
m(1+floor((xaxis+0.5)*imgsize),1+floor((data+0.5)*imgsize),:)=RGBtriple(i,:);
end
image(m);
如果希望平面中的所有点都有一个颜色值,为什么不取每个点来计算颜色值,而是取一个颜色值并计算它应该出现的点?显然,没有任何颜色输入值产生[-.49,-.49]的
xy
,因此[-.5,-.5]处的红色标记在其区域中是单独的
此外,您正在连续绘制各个点,这当然不是很快。(在发布的前三分之二的代码中,循环中有矩阵增长,但我想最慢的部分是最后一部分)。尝试先创建一个要显示的值矩阵,然后再显示它
请尝试以下代码:
for r = linspace(0,1,points)
for g = linspace(0,1,points)
for b = linspace(0,1,points)
index = index + 1;
RBGtriple(index, :) = [r g b];
end
end
end
for k = 1:size(RBGtriple,1)
R = RBGtriple(k,1);
G = RBGtriple(k,2);
B = RBGtriple(k,3);
[x y] = RGBtoCartCoordXY(R,G,B);
xaxis(k) = x;
data(k) = y;
end
hold on;
for k = 1:size(RBGtriple,1)
hplot = plot (xaxis(k), data(k));
set(hplot, ...
'Marker', 'square', ...
'LineStyle','none' , ...
'MarkerSize' ,3 , ...
'LineWidth' , 0.1, ...
'MarkerEdgeColor',RBGtriple(k, :) , ...
'MarkerFaceColor',RBGtriple(k, :));
end
hold off;
imgsize = 500;
for i=1:length(xaxis),
m(1+floor((xaxis+0.5)*imgsize),1+floor((data+0.5)*imgsize),:)=RGBtriple(i,:);
end
image(m);
对我来说,这(或从您的代码改编的类似代码)生成了一个具有类似颜色分布的三角形,但变为90°,并具有默认的黑色背景。如果您希望平面中的所有点都具有颜色值,为什么不取每个点来计算颜色值,但是,取而代之的是获取一个颜色值并计算它应该出现的点?显然,没有任何颜色输入值产生[-.49,-.49]的
xy
,因此[-.5,-.5]处的红色标记在其区域中是单独的
此外,您正在连续绘制各个点,这当然不是很快。(在发布的前三分之二的代码中,循环中有矩阵增长,但我想最慢的部分是最后一部分)。尝试先创建一个要显示的值矩阵,然后再显示它
请尝试以下代码:
for r = linspace(0,1,points)
for g = linspace(0,1,points)
for b = linspace(0,1,points)
index = index + 1;
RBGtriple(index, :) = [r g b];
end
end
end
for k = 1:size(RBGtriple,1)
R = RBGtriple(k,1);
G = RBGtriple(k,2);
B = RBGtriple(k,3);
[x y] = RGBtoCartCoordXY(R,G,B);
xaxis(k) = x;
data(k) = y;
end
hold on;
for k = 1:size(RBGtriple,1)
hplot = plot (xaxis(k), data(k));
set(hplot, ...
'Marker', 'square', ...
'LineStyle','none' , ...
'MarkerSize' ,3 , ...
'LineWidth' , 0.1, ...
'MarkerEdgeColor',RBGtriple(k, :) , ...
'MarkerFaceColor',RBGtriple(k, :));
end
hold off;
imgsize = 500;
for i=1:length(xaxis),
m(1+floor((xaxis+0.5)*imgsize),1+floor((data+0.5)*imgsize),:)=RGBtriple(i,:);
end
image(m);
对我来说,这个(或从你那里改编的类似代码)生成了一个具有类似颜色分布的三角形,但变为90°,并带有默认的黑色背景。如果有人对此感到好奇,我在《使用MATLAB的计算颜色科学》一书中找到了一个非常简单的解决方案来绘制RGB三角形:
xy=[0 0; 1 0; 0.5 sqrt(3)/2];
col=[1 0 0; 0 1 0; 0 0 1];
patch('Vertices',xy, 'Faces',[1:size(xy,1)], 'EdgeColor','none','FaceVertexCData', col,'FaceColor','interp');
axis off
text(xy(1,1), xy(1,2)-0.05,'Vertice 1','HorizontalAlignment', 'center')
text(xy(2,1), xy(2,2)-0.05,'Vertice 2','HorizontalAlignment', 'center')
text(xy(3,1), xy(3,2)+0.05,'Vertice 3','HorizontalAlignment', 'center')
如果有人对此感到疑惑,我在《使用MATLAB的计算颜色科学》一书中找到了一个非常简单的解决方案来绘制RGB三角形:
xy=[0 0; 1 0; 0.5 sqrt(3)/2];
col=[1 0 0; 0 1 0; 0 0 1];
patch('Vertices',xy, 'Faces',[1:size(xy,1)], 'EdgeColor','none','FaceVertexCData', col,'FaceColor','interp');
axis off
text(xy(1,1), xy(1,2)-0.05,'Vertice 1','HorizontalAlignment', 'center')
text(xy(2,1), xy(2,2)-0.05,'Vertice 2','HorizontalAlignment', 'center')
text(xy(3,1), xy(3,2)+0.05,'Vertice 3','HorizontalAlignment', 'center')
您使用哪个函数进行绘图?您的R、G、B输入值是多少?如增强问题中所示,rgb值在0和1之间相等分布,并且使用
plot
进行绘图。您使用哪个函数进行绘图?您的R、G、B输入值是多少?如增强问题中所示,rgb值在0和1之间相等分布,并且使用plot
进行绘图。这只给了我一个50x50像素的蓝色正方形。m只包含值0或1,而RGBtriple包含颜色值。事实上,它必须是m(1+floor((xaxis(k)+0.5)*imgsize),1+floor((数据(k)+0.5)*imgsize),:)=RGBtriple(k,:)
。它形成了一个漂亮的三角形。但是我想我必须找到一种方法直接迭代三角形的点,因为在高分辨率的情况下,三角形中的黑色点仍然存在,这只会给我一个50×50像素的蓝色正方形。m只包含值0或1,而RGBtriple包含颜色值。事实上,它必须是m(1+floor((xaxis(k)+0.5)*imgsize),1+floor((数据(k)+0.5)*imgsize),:)=RGBtriple(k,:)
。它形成了一个漂亮的三角形。但是我想我必须找到一种方法,直接迭代三角形的点,因为在高分辨率的情况下,三角形中的黑点仍然存在。