为3D立方体的每个面分配2D图像:MATLAB [英] Assigning a 2D image for every face of a 3D cube: MATLAB
问题描述
我想在MATLAB中构建一个多维数据集,并为其面孔分配不同的2D图像.我认为这称为纹理映射.我已经搜索了这样的代码,但是我发现的是能够为所有多维数据集面分配单个图像的代码,可以在此处找到该代码(
I want to build a cube in MATLAB and assign different 2D images for its faces. 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). 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);
我想为不同的面孔分配不同的图片,我尝试设置不同的cdata变量,因为cdata1,cdata2,...,cdata6各自具有不同的图像路径,但是出现错误,表明未定义cdata1.
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.
这是我尝试仅使用2张图像作为示例的
Here is what I've tried using only 2 images as an example,
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);
上面的代码产生一个错误,指出未定义CData1.
The code above yields an error saying that CData1 is not defined.
谁能告诉我如何获得上面的代码,该代码能够将不同的图像分配给多维数据集的不同面?
Can anyone tell me how to get the above code capable of assigning different images to different faces of the cube?
谢谢.
推荐答案
应该简单明了!
cdata1 = flipdim( imread('peppers.png'), 1 );
cdata2 = rgb2gray(flipdim( imread('peppers.png'), 1 ));
似乎您只是没有创建具有正确名称的变量( cdata1
, cdata2
, cdata3
,...)
It seems that you just did not create the variables with the correct name (cdata1
, cdata2
, cdata3
,...)
另外:
您要在每个 surface
调用中设置的属性的名称不会更改.您总是想将 CData
设置为某些值.表面上没有名为 CData1
的属性,因为没有名为 PutImageHere
的属性!用'CData'
替换所有呼叫,如下所示:
the name of the property you wan to set in each of the surface
calls does not change. You always want to set CData
to something. There is no property in surface called CData1
as there is no property called PutImageHere
! replace all the calls with 'CData'
as in:
surface([1 1; 1 1], [-1 1; -1 1], [-1 -1; 1 1], ...
'FaceColor', 'texturemap', 'CData', yourvariablename);
% in this case yourvariablename is cdata1
这篇关于为3D立方体的每个面分配2D图像:MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!