为 3D 立方体的每个面分配 2D 图像:MATLAB [英] Assigning a 2D image for every face of a 3D cube: MATLAB

查看:27
本文介绍了为 3D 立方体的每个面分配 2D 图像:MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 MATLAB 中构建一个立方体并为其面分配不同的 2D 图像.我认为这称为纹理映射.我已经搜索过这样的代码,但我发现的是一个能够将单个图像分配给所有立方体面的代码,该代码可在此处获得 (http://www.mathworks.com/matlabcentral/answers/32070-rgb-images-on-a-3d 立方体).这是代码,

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆