簡體   English   中英

用Matlab繪制三維RGB立方體模型

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

我寫這個代碼來繪制一個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立方體

需要更新顏色貼圖才能使您的繪圖看起來像鏈接中的繪圖。 您不能簡單地使用內置函數直接生成正確的序列。 此外,調用hsv(8)會產生您不想要的其他顏色(在命令窗口中打印出來以查看),但不包括純白色或黑色。 您可以使用hsv(6)並附加[0 0 0][1 1 1] ,但是您需要確保排序與其余代碼( fmvm )一致。

這是您的代碼的修訂版本 - 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);

這導致一個看起來像這樣的圖

三維RGB立方體圖

到那時,我完成了代碼,@ horchler的答案已經在線了。 它看起來很完美。 無論如何也發布我的。

要了解您正在應用的顏色,我打印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

但你想要實際應用的是紅色,綠色,藍色,白色和青色,品紅色,黃色,黑色。 請參閱此鏈接以了解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);

輸出:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM