Matlab:用透明纹理覆盖3D表面

时间:2017-10-02 12:35:40

标签: matlab

我想在3D视图中用一些强度数据覆盖一些高度数据。在2D中很容易(参见下面代码生成的第一个图像),但在3D中,表示看起来有些奇怪,请参见下面示例中的第二个和第三个图像。我希望rgb图像中的点看起来更“坚固”。

figure;
subplot(1,3,1);
% Get some data
[x,y,z] = peaks(128);
pos = (rand(2,5).*4)-2;
i = zeros(128,128);
for n = 1:5
    i = i+exp(-((((x-pos(1,n))/.2).^2+(((y-pos(2,n)))/.2).^2))); 
end

% Convert to uin16 since real data is uint16
i = i./max(i(:));
i = i * 2^16;
i = uint16(i);

i_rgb = ind2rgb(i, hot(2^16));

% Display data

% This is how it should look like in 3D

imagesc(z);
colormap(gray);
axis image;
hold on;

img = imshow(i_rgb);
img.AlphaData = i;

% now in 3D, here the texture looks strange

subplot(1,3,2);

s = surface(x,y,z);
s.EdgeColor = 'none';
axis ij tight equal off; colormap(gray);

hold on; 

s2 = surface(x,y,z);
s2.EdgeColor = 'none';
s2.FaceColor = 'texturemap';
s2.CData = i_rgb;
s2.FaceAlpha = 'texturemap';
s2.AlphaData = i;
hold off;

% Again in 3D, but without FaceAlpha = 'texturemap';
% The intensity-dots look good, but the remaining area looks strange.
subplot(1,3,3);

s3 = surface(x,y,z);
s3.EdgeColor = 'none';
axis ij tight equal off; colormap(gray);

s4 = surface(x,y,z);
s4.EdgeColor = 'none';
s4.FaceColor = 'texturemap';
s4.CData = i_rgb;
s2.AlphaData = i;

left: looks fine, but 2D only. center: a lot of gaps in the intensity image. right: right: the surface underneath looks weird.

更新:系统信息:

OS:SuSE Linux 42.1

>> version

ans =

    '9.2.0.538062 (R2017a)'

>> opengl info
                          Version: '4.5.0 NVIDIA 375.66'
                           Vendor: 'NVIDIA Corporation'
                         Renderer: 'Quadro K1000M/PCIe/SSE2'
                   MaxTextureSize: 16384
                           Visual: 'Visual 0x27, (RGBA 32 bits (8 8 8 8), Z depth 16 bits, Hardware acceleration, Double buffer, Antialias 8 samples)'
                         Software: 'false'
             HardwareSupportLevel: 'full'
        SupportsGraphicsSmoothing: 1
    SupportsDepthPeelTransparency: 1
       SupportsAlignVertexCenters: 1
                       Extensions: {330×1 cell}
               MaxFrameBufferSize: 16384

1 个答案:

答案 0 :(得分:3)

好吧,似乎linux下的渲染或者至少我的opengl版本存在一些问题。有一种解决方法可以解决我的问题:我没有使用具有透明度的纹理,而是将高度和强度数据组合到rgb图像中并将其用作纹理:

figure;

% Get some data
[x,y,z] = peaks(128);
pos = (rand(2,5).*4)-2;
i = zeros(128,128);
for n = 1:5
    i = i+exp(-((((x-pos(1,n))/.2).^2+(((y-pos(2,n)))/.2).^2))); 
end

% Convert to uin16 since real data is uint16
i = i./max(i(:));
i = i * 2^16;
i = uint16(i);

% Do the same with the z-data

z_idx = z - min(z(:));
z_idx = z_idx / max(z_idx(:));
z_idx = uint16(2^16 * z_idx );

i_rgb = ind2rgb(i, hot(2^16));
z_rgb = ind2rgb(z_idx, gray(2^16));

% merge the images using i as alpha values.

alphas = double(i)/2^16;
alphas_inv = ones(size(alphas)) - alphas;

% Display data

merged_rgb = zeros(size(i,1), size(i,2), 3);

for channel = 1:3
    merged_rgb(:,:,channel) = z_rgb(:,:,channel).*alphas_inv + i_rgb(:,:,channel) .* alphas;
end

s = surface(x,y,z);
s.EdgeColor = 'none';
s.FaceColor='texturemap';
s.CData = merged_rgb;

这产生了预期的结果 enter image description here