Displaying Image over Background - Making Top Image Background Color Transparent

10 views (last 30 days)
Hey everyone. I've put a lot of time into researching how to solve my problem, but so far I haven't gotten anywhere.
I'm working on a school project, and I'm having trouble with a small part of my visuals. I have a 640x320 image, and that will be the background for my "game". I am trying to plot a character image on top of it, in a smaller dimension (29x29), and I have a small bit of background around a few of the sides of the character png. I've made these transparent in Photoshop, but matlab makes them whatever the default BG color is.
Here's my code, my current goal is for someone to help me make the bg-colored pixels transparent so the character is right on the background without ugly borders.
handles.plrY1 = imread('linkY1.png');
handles.level = 1;
plot(1,1)
axis([0 460 0 320]);
hold on
image([460,0],[320,0],handles.bgimg);
hold on
axis off;
handles.playerVec = [0 0 29 29;0 29 29 0];
%%Default Variables Initilization Section.
handles.initialSpotR1 = [220 220 249 249; 60 89 89 60];
handles.playerVec = handles.initialSpotR1;
handles.currentPlayer = fill(handles.playerVec(1,:),handles.playerVec(2,:),[1 0 1]);
hold on
handles.spr = image([handles.playerVec(1,1),handles.playerVec(1,4)],[handles.playerVec(2,3),handles.playerVec(2,1)],handles.plrY1);
hold on
#EDIT I may have forgotten to mention, these images are 2D, if that affects your answer.
Also, by edges of the image I don't mean a set width of pixels. I mean that there is a black background Matlab forces onto an object that had transparency from the original files. I need some way to make all pixels of a certain color transparent, and not the entire image.
I need help with alphadata if I'm going to use it, as I don't know how to make the transparency only apply to a certain part of the image (as in the bgcolor and not the rest of it becoming transparent)
The thing I need to have the edges removed from is handles.spr, which is a sprite mapped on a vector I move around with user input.
Thanks!

Accepted Answer

Image Analyst
Image Analyst on 2 Aug 2014
  1 Comment
James
James on 2 Aug 2014
The second link was exactly what I needed. Here's the end result for anyone with a similar problem.
Excluding all unchanged code, I just had to edit the imread line, and the two lines for displaying the character sprite.
[handles.plrY1,handles.map3,handles.alphamap3] = imread('linkY1.png');
handles.player = image([handles.playerVec(1,1),handles.playerVec(1,4)],[handles.playerVec(2,3),handles.playerVec(2,1)],handles.plrY1);
set(handles.player,'AlphaData',handles.alphamap3)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!