Transparent PNG file on background in Matlab GUI

8 visualizaciones (últimos 30 días)
Frank
Frank el 5 de Jun. de 2012
Comentada: Serge el 6 de Nov. de 2022
Hello,
For my GUI i have set a background image and now I want to put a (partly) transparent PNG image on a pushbutton on top of this background image, while keeping the background visible in the parts where the button image is transparent. How do I do this?
Thanks in advance!

Respuestas (3)

Robert Cumming
Robert Cumming el 12 de Dic. de 2014
I know this is an old post - but if anyone comes across this you may be interested in this blog on undocumentedmatlab

Klont
Klont el 26 de Jul. de 2015
Editada: Klont el 26 de Jul. de 2015
Simply make a matrix of size height (pixels) x width (pixels) x 3 (rgb) of doubles between 0 and 1, with NaN indicating transparency.
  1 comentario
Serge
Serge el 6 de Nov. de 2022
This works if you are happy with alpha being all or nothing...

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 5 de Jun. de 2012
You cannot do this directly. The uicontrol() that can have images placed on them (such as pushbutton) have a CData property that must be truecolor (RGB) values, but there is no alpha property, so there is no way to define transparency for such images.
The work-around is to figure out which part of the background image the button would be covering up, and to store the corresponding pixel values into the image array locations that should be transparent, and then to use that as the CData.
Ideally you would re-do this when the figure was resized; that would have to be triggered off of the figure resize callback, as uicontrol do not have a resize callback.
  2 comentarios
Frank
Frank el 6 de Jun. de 2012
Thanks for the tip!
I tried the following code:
bg_image=imread('bg.png'); %background
pos_button=[500,500];
[button,button_map]=imread('button.png.'); %button image
[w,l,d]=size(button);
bg_button=bg_image(500:500+w,500:500+l); % part of background that is behind button
button = uicontrol('style','pushbutton','position',[pos_button,[w,l]]);
button_image=button;
for i=1:w
for j=1:l
for k=1:d
if button_image(i,j,k)==255 % places where background should be seen
button_image(i,j,k)=bg_button(i,j,k); %replace with background values
end
end
end
end
button_image_RGB=ind2RGB(button_image,button_off_map);
set(button,'cdata',button_image_RGB);
This works, but the colors of the background are not the colors they should be, but grey.
Any ideas why?
Walter Roberson
Walter Roberson el 6 de Jun. de 2012
I suspect bg_image is 3 dimensional (truecolor, RGB) rather than 2 dimensional. You probably need to add a third index when extracting bg_button

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by