MATLAB:如何基于相机视图创建存储在 3D 修补对象的 FaceVertexCData 中的数据的 2D 像素图?

问题描述 投票:0回答:1

最小可行示例:

% Define a simple 6 sided cube with dimensions 0-1 in X, Y, and Z
vertices = [0,0,1;  % 1 Front-Top-Left (FTL)
            1,0,1;  % 2 Front-Top-Right (FTR)
            1,0,0;  % 3 Front-Bottom-Right (FBoR)
            0,0,0;  % 4 Front-Bottom-Left (FBoL)
            0,1,1;  % 5 Back-Top-Left (BaTL)
            1,1,1;  % 6 Back-Top-Right (BaTR)
            1,1,0;  % 7 Back-Bottom-Right (BaBoR)
            0,1,0]; % 8 Back-Bottom-Left (BaBoL)
faces = [1,2,3,4;  % 1 Front
         2,6,7,3;  % 2 Right
         6,5,8,7;  % 3 Back
         5,1,4,8;  % 4 Left
         1,5,6,2;  % 5 Top
         4,3,7,8]; % 6 Bottom
temps = [5;10;15;20;25;30]; % One temperature per face
% Plot the cube
fig1 = figure;
ax1 = axes(fig1);
patch('Faces',faces,'Vertices',vertices,'FaceColor','flat','EdgeColor','none','FaceVertexCData',temps,'Parent',ax1);
% Rotate to an isometric type of view angle
ax1.View = [-42.5 28];
% Turn off axes
ax1.XAxis.Visible = 'off';
ax1.YAxis.Visible = 'off';
ax1.ZAxis.Visible = 'off';
% Set data aspect ratio to 1 1 1
daspect(ax1,[1 1 1]);

然后我需要生成一个看起来像这样的矩阵的代码(对于上面的代码):

% -------------------------------------------------------------------------
% Code goes here that should produce a 2D matrix that looks something like:
% (15x15 as example, but would need to be more like 1024x1024 up to 
%  4096x4096 in real world use cases)
%       1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
% 1  % 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
% 2  % 00 00 00 00 00 00 25 00 00 00 00 00 00 00 00 
% 3  % 00 00 00 00 00 25 25 25 00 00 00 00 00 00 00 
% 4  % 00 00 00 00 25 25 25 25 25 00 00 00 00 00 00 
% 5  % 00 00 00 25 25 25 25 25 25 25 00 00 00 00 00 
% 6  % 00 00 20 25 25 25 25 25 25 10 00 00 00 00 00 
% 7  % 00 00 20 20 20 25 25 25 10 10 00 00 00 00 00 
% 8  % 00 00 20 20 20 20 25 10 10 10 00 00 00 00 00 
% 9  % 00 00 20 20 20 20 20 10 10 10 00 00 00 00 00 
% 10 % 00 00 20 20 20 20 20 10 10 10 00 00 00 00 00 
% 11 % 00 00 00 20 20 20 20 10 10 00 00 00 00 00 00 
% 12 % 00 00 00 00 20 20 20 10 00 00 00 00 00 00 00 
% 13 % 00 00 00 00 00 20 20 10 00 00 00 00 00 00 00 
% 14 % 00 00 00 00 00 00 20 00 00 00 00 00 00 00 00 
% 15 % 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

现实世界的用例包括从多个不同角度投影到包含超过 100k 多边形的复杂 3D 几何图形。还存在几何体的一部分与几何体的其他部分重叠的情况,并且解决方案需要仅考虑特定视角下的可见几何体,并忽略“最接近相机”面后面的所有其他看不见的面。当前的绘图解决方案将每个主体绘制一个面片对象作为 hgtransform 的子对象,并且模型中可以有数百个主体。

如有任何帮助,我们将不胜感激!

matlab 3d 2d projection
1个回答
0
投票

假设您可以访问 MATLAB 图窗并且它位于所需的视图中。这个想法是捕获图中显示的图像,并使用

colormap
及其关联的
clim
将颜色映射到初始值。

% Extract colormap limits
h = gca;
lims = h.CLim;

% Extract colormap RGB values and create correspondence vector from lims
map = colormap();
vals = linspace(lims(1),lims(2),size(map,1));

% Get the figure color content from the current view (convert to double
% precision)
F = getframe(fig1);
Fdouble = im2double(F.cdata);

% Map each pixel RGB value in F.cdata to the corresponding value, using the map
% and the corresponding vals
% To do so, for each pixel color (F.cdata(ii,jj,:)) get idx of the closest color in map
out = zeros(size(F.cdata,1),size(F.cdata,2));

for ii = 1:size(F.cdata,1)
    for jj = 1:size(F.cdata,2)
        
        [M,I] = min(sqrt((map(:,1)-Fdouble(ii,jj,1)).^2+(map(:,2)-Fdouble(ii,jj,2)).^2+(map(:,3)-Fdouble(ii,jj,3)).^2));
        % if the color is close enough, get the corresponding value in
        % vals. if not keep 0;

        if M < 0.1

            out(ii,jj) = vals(I);

        end

    end
end

% Flip the rows in out to match the input:
out = out(end:-1:1,:);

输出

有趣的是,表示输出的最简单方法是将其

surf
返回到图像。您可以通过数据提示看到这些值是正确的:

enter image description here

旁注

输出是一个与原始图形具有相同分辨率的数组(这意味着您可以通过调整实际输入图形的大小直接调整数组的分辨率)。请参阅下面的两个示例:

enter image description here 较低分辨率输入

enter image description here 更高分辨率的输入

© www.soinside.com 2019 - 2024. All rights reserved.