简体   繁体   English

用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立方体

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. 此外,调用hsv(8)会产生您不想要的其他颜色(在命令窗口中打印出来以查看),但不包括纯白色或黑色。 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(6)并附加[0 0 0][1 1 1] ,但是您需要确保排序与其余代码( fmvm )一致。

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 这导致一个看起来像这样的图

三维RGB立方体图

By the time, I finished the code, @horchler's answer was online already. 到那时,我完成了代码,@ horchler的答案已经在线了。 It looks perfect. 它看起来很完美。 Anyway posting mine as well. 无论如何也发布我的。

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. 请参阅此链接以了解Matlab颜色代码。 Hence we can apply the colors to each vertex manually based on your requirement. 因此,我们可以根据您的要求手动将颜色应用于每个顶点。 I changed your code as follows. 我按如下方式更改了您的代码。

% 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: 输出:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM