用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


注意!

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



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