Overlaying images and making background transparent

9 次查看(过去 30 天)
Hello,
I'm a student who is interested in sprite stacking as a method of making "fake 3D images" from stacking 2D slices. I wanted to try to recreate what I saw in an article in matlab (see article below).
I have a png strip exported from an examples 3D voxel object (the example is a plane). The background for the slices is black. The code you see below reads in a png strip with all the slices, separate the slices in a cell array. I attempt to overlay the the slices and remove the black background but I end up a stack of the alphas with the blakc background still there. I attached the slice file of the plane.
So I have a two part question:
1) How do I stack the stack several images with the black backgrounds removed
2) Sprite stacking involves shifting each layer by a certain amount in one direction (in the case from the article 1 pixel up)...So how do I stack up the images while shifting each progressive slice by one pixel? should I just add padding to each image based on how much they will be shifted?
thank you
Andrew
%%set folder of images
[file,path] = uigetfile; % get the sprite strip file
[Sprite_Image,~,Sprite_Alpha] = imread(strcat(path,file)); %read in the image file
%% Sprite Strip Variables
Sprite_Width = 70; %dimensions of the slices from the sprite slip
Sprite_Length = 78;
outCell_image=mat2tiles(Sprite_Image,[Sprite_Length,Sprite_Width]); % mat2tiles function I found that can separate the
outCell_alpha=mat2tiles(Sprite_Alpha,[Sprite_Length,Sprite_Width]); % individual slices
%% variables
% initiate with the top layer
Background = outCell_image{1,1}; %take top image of sprite strip as the background
for i=2:length(outCell_image) %stacking the layers on top of the
Layer_image = outCell_image{i,1};
Layer_alpha = outCell_alpha{i,1};
Stack = repmat(Layer_alpha,[1,1,3]).*Layer_image + (1-repmat(Layer_alpha,[1,1,3])).*Background;
Background = Stack;
end
imshow(Background)
https://medium.com/@avsnoopy/beginners-guide-to-sprite-stacking-in-gamemaker-studio-2-and-magica-voxel-part-1-f7a1394569c0
  2 个评论
Matt J
Matt J 2024-3-26
Please run the code here in the online environment, so that we can see the effect you are talking about.
Andrew Luce
Andrew Luce 2024-3-26
Maybe I'm doing this wrong but the code didn't work when I tried it online...I did use the mat2files funcrion file I found from a different answer...but the image below shows the result.

请先登录,再进行评论。

回答(1 个)

Milan Bansal
Milan Bansal 2024-4-5
I understand that you want to perform sprite stacking to create the illusion of a 3D object by stacking the 2D slices from the attached image, but you are facing an issue where the transparent background in the slices is getting plotted as a black background. Additionally, you also want to be able to shift the slices in some directions.
Here is a workaround by which this can be achieved in MATLAB. Plot each slice in 3D space, progressively below the previous slice, as a surface with a constant gap between the slices. Use the "surf" function in MATLAB and set the name-value pairs as follows:
  • "CData" as the current slice image,
  • "FaceColor" as "texturemap"
  • "EdgeColor" as "none",
  • "FaceAlpha" as "texturemap"
  • "AlphaData" as alpha values of the current slice.
This will ensure that the plotted slices have the same colors as expected and that the transparent background in each slice does not become opaque (black).
Please refer to the code snippet below to achieve this.
%% Get the slices and corresponding alpha values from the attatched image.
[Sprite_Image,~,Sprite_Alpha] = imread("Plane01.png"); %read in the image file
% Sprite Strip Variables
Sprite_Width = 70; %dimensions of the slices from the sprite slip
Sprite_Length = 78;
outCell_image=mat2tiles(Sprite_Image,[Sprite_Length,Sprite_Width]); % mat2tiles function I found that can separate the
outCell_alpha=mat2tiles(Sprite_Alpha,[Sprite_Length,Sprite_Width]); % individual slices
%% Plot the slices in 3D space. Each slice must be plotted below the previous slice with consistent gap between the slices.
figure;
for i=1:15 %stacking the layers on top of the
Layer_image = outCell_image{i,1};
Layer_alpha = outCell_alpha{i,1};
% Get x y z coordinates where the image will be plotted
[x, y] = meshgrid(1:size(Layer_image, 2), 1:size(Layer_image, 1));
z = zeros(size(x)) - i; % All points at the same z level and shifting the z coordinates by equal amount
% Plot the slice in 3D space
surf(x, y, z, 'CData', Layer_image, 'FaceColor', 'texturemap', 'EdgeColor', 'none', 'FaceAlpha', 'texturemap', 'AlphaData', Layer_alpha);
hold on;
end
view(3); % Change to a 3D view
axis equal; % Keep the aspect ratio consistent
axis("off");
In the above code, shifts can be added in the x, y and z coordinates as per the requirement. No padding will be required while shifting the slices.
Please refer to the following documentation link to learn more about "surf" function.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Read, Write, and Modify Image 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by