简体   繁体   English

为3D立方体的每个面分配2D图像:MATLAB

[英]Assigning a 2D image for every face of a 3D cube: MATLAB

I want to build a cube in MATLAB and assign different 2D images for its faces. 我想在MATLAB中建立一个立方体,并为其面孔分配不同的2D图像。 I think this is called texture mapping. 我认为这称为纹理映射。 I've searched for such a code, but what I found is a code that is able to assign a single image to all of the cube faces, the code is available here ( http://www.mathworks.com/matlabcentral/answers/32070-rgb-images-on-a-3d-cube ). 我已经搜索了这样的代码,但是我发现的是能够为所有多维数据集面分配单个图像的代码,可在此处找到该代码( http://www.mathworks.com/matlabcentral/answers / 32070-rgb-images-on-a-3d-cube )。 Here is the code, 这是代码,

cdata = flipdim( imread('peppers.png'), 1 );
cdatar = flipdim( cdata, 2 );

% bottom
surface([-1 1; -1 1], [-1 -1; 1 1], [-1 -1; -1 -1], ...
    'FaceColor', 'texturemap', 'CData', cdatar );
% top
surface([-1 1; -1 1], [-1 -1; 1 1], [1 1; 1 1], ...
    'FaceColor', 'texturemap', 'CData', cdata );

% font
surface([-1 1; -1 1], [-1 -1; -1 -1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData', cdata );
% back
surface([-1 1; -1 1], [1 1; 1 1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData', cdatar );

% left
surface([-1 -1; -1 -1], [-1 1; -1 1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData', cdatar );
% right
surface([1 1; 1 1], [-1 1; -1 1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData', cdata );

view(3);

I want to assign different pictures for different faces, I've tried to set different cdata variables as cdata1, cdata2,..., cdata6 each has a different image path, but I got an error indicating that cdata1 is not defined. 我想为不同的面孔分配不同的图片,我尝试设置不同的cdata变量,因为cdata1,cdata2,...,cdata6每个都有不同的图像路径,但是出现错误,表明未定义cdata1。

EDIT: Here is what I've tried using only 2 images as an example, 编辑:这是我尝试仅使用2张图像作为示例的内容,

cdata1 = flipdim( imread('face1.jpg'), 1 );
cdatar1 = flipdim( cdata1, 2 );

cdata2 = flipdim( imread('interface 1.png'), 1);
cdatar2 = flipdim( cdata2, 2 );

% bottom
surface([-1 1; -1 1], [-1 -1; 1 1], [-1 -1; -1 -1], ...
    'FaceColor', 'texturemap', 'CData1', cdatar1 );
% top
surface([-1 1; -1 1], [-1 -1; 1 1], [1 1; 1 1], ...
    'FaceColor', 'texturemap', 'CData2', cdata2 );

% font
surface([-1 1; -1 1], [-1 -1; -1 -1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData1', cdata1 );
% back
surface([-1 1; -1 1], [1 1; 1 1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData2', cdatar2 );

% left
surface([-1 -1; -1 -1], [-1 1; -1 1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData1', cdatar2 );
% right
surface([1 1; 1 1], [-1 1; -1 1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData1', cdata1 );

view(3);

The code above yields an error saying that CData1 is not defined. 上面的代码产生一个错误,指出未定义CData1。

Can anyone tell me how to get the above code capable of assigning different images to different faces of the cube? 谁能告诉我如何获得上述代码,以便将不同的图像分配给多维数据集的不同面?

Thanks. 谢谢。

It should be straightforward! 它应该很简单!

cdata1 = flipdim( imread('peppers.png'), 1 );
cdata2 = rgb2gray(flipdim( imread('peppers.png'), 1 ));

It seems that you just did not create the variables with the correct name ( cdata1 , cdata2 , cdata3 ,...) 似乎您只是没有使用正确的名称( cdata1cdata2cdata3 ,...)创建变量。

Additionally: 另外:

the name of the property you wan to set in each of the surface calls does not change. 您希望在每个surface调用中设置的属性的名称不会更改。 You always want to set CData to something. 您总是想将CData设置为某种值。 There is no property in surface called CData1 as there is no property called PutImageHere ! 表面上没有名为CData1属性,因为没有名为PutImageHere属性! replace all the calls with 'CData' as in: 将所有呼叫替换为'CData'如下所示:

surface([1 1; 1 1], [-1 1; -1 1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData', yourvariablename); 
% in this case yourvariablename is cdata1

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

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