用Matlab绘制三维RGB立方体模型。

[英]Drawing 3-D RGB cube model with Matlab


I wrote this code to draw an RGB cube, but it's color not exact as true?

我写了这个代码来画一个RGB立方体,但是它的颜色不像真的?

%Define a six row by four column matrix to define the six cube faces
fm = [1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8]

%Define an eight row by three column matrix to define the vertices at which
%the faces meet
vm = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1]

%Plot the cube ----- gives each face a different color and creates the cube at a convenient viewing angle
patch('Vertices',vm,'Faces',fm,'FaceVertexCData',hsv(8),'FaceColor','interp');
view(3);

RGB cube

2 个解决方案

#1


2  

By the time, I finished the code, @horchler's answer was online already. It looks perfect. Anyway posting mine as well.

到那时,我已经完成了代码,@horchler的答案已经在网上了。它看起来完美。无论如何,我也要发布我的。

To understand what colors you are applying, I printed values of hsv(8) as follows.

为了了解您所使用的颜色,我将hsv(8)的值如下所示。

1.0000         0         0
1.0000    0.7500         0
0.5000    1.0000         0
     0    1.0000    0.2500
     0    1.0000    1.0000
     0    0.2500    1.0000
0.5000         0    1.0000
1.0000         0    0.7500

But what you want to apply actually is red, green, blue, white, and cyan, magenta, yellow, black. Please refer to this link to know about Matlab color codes. Hence we can apply the colors to each vertex manually based on your requirement. I changed your code as follows.

但是你想要应用的是红色,绿色,蓝色,白色,青色,品红,黄色,黑色。请参考这个链接了解Matlab的颜色代码。因此,我们可以根据您的需求手动将颜色应用到每个顶点。我改变了你的代码如下。

% Define a six row by four column matrix to define the six cube faces
fm = [1 2 6 5; 2 3 7 6; 3 4 8 7; 4 1 5 8; 1 2 3 4; 5 6 7 8];

% Define an eight row by three column matrix to define the vertices at which
% the faces meet
vm = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1];

% Plot the cube ----- gives each face a different color and creates the 
% cube at a convenient viewing angle
clear cdata;
cdata = [
    0 0 0; % black
    1 0 0; % red
    1 0 1; % magenta
    0 0 1; % blue
    0 1 0; % green
    1 1 0; % yellow
    1 1 1; % white
    0 1 1; % cyan
    ];

patch('Vertices',vm,'Faces',fm,'FaceVertexCData',cdata,'FaceColor','interp');

axis equal;
axis off;
view(3);

Output:

输出:

enter image description here

#2


3  

It is the color map that needs to be updated to get your plot to look like the one in your link. You can't simply use a built-in function to directly generate the right sequence. Additionally, calling hsv(8) produces additional colors that you don't want (print it out in the Command Window to see), but doesn't include pure white or black. You can use hsv(6) and append [0 0 0] and [1 1 1], but you'll need to make sure the ordering aligns with the rest of your code (fm and vm).

它是需要更新的颜色映射,以使您的图看起来像您的链接中的一个。您不能简单地使用内置函数直接生成正确的序列。另外,调用hsv(8)会产生你不想要的额外颜色(在命令窗口中打印出来),但不包括纯白色或黑色。您可以使用hsv(6)和append[0 0 0]和[1 1],但是您需要确保顺序与代码的其余部分对齐(fm和vm)。

Here's a revised version of your code – the cm matrix encodes the pattern of colors for each vertex:

这是你的代码的修订版- cm矩阵编码每个顶点的颜色模式:

% Define a six row by four column matrix to define the six cube faces
fm = [1 2 6 5;
      2 3 7 6;
      3 4 8 7;
      4 1 5 8;
      1 2 3 4;
      5 6 7 8];

% Define an eight row by three column matrix to define the vertices at which the faces meet
vm = [0 0 0;
      1 0 0;
      1 1 0;
      0 1 0;
      0 0 1;
      1 0 1;
      1 1 1;
      0 1 1];

% RGB colors for each vertex
cm = [0 0 0;
      0 1 0;
      1 1 0;
      1 0 0;
      0 0 1;
      0 1 1;
      1 1 1;
      1 0 1];

% Plot the cube - gives each face a different color and creates the cube at a convenient viewing angle
figure('Color','w')
patch('Vertices',vm,'Faces',fm,'FaceVertexCData',cm,'FaceColor','interp');
view(120,30);

% Plot axes
axis equal;
axis off;
d1 = 1.25;
line([0 0 0;d1 0 0],[0 0 0;0 d1 0],[0 0 0;0 0 d1],'Color','k','LineWidth',2);

% Label axes
d2 = 0.1;
text([0 1 0],[1 -d2 -d2],[-d2 0 1],'255','FontSize',11,'HorizontalAlignment','center');
text([0 d1 0],[d1 d2 d2],[d2 0 d1],{'R','G','B'},'FontSize',16);

This results in a figure that looks like this

这个结果是这样的。

3-D RGB cube plot

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/04/29/72023fbb8e0f065222ed4d6e313ee1b1.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告