MATLAB Answers

How to change background color of an image in a GUI

45 views (last 30 days)
Rocco Didio
Rocco Didio on 11 Sep 2019
Commented: Rocco Didio on 12 Sep 2019
I want to insert an image (an arrow) on a button. I created the arrow in PowerPoint without background and then I saved that as image file. When I upload the image on the button it creates a white background. Can I substitute the white pixels with grey ([0.8 0.8 0.8]) pixels?
That's the code I use to upload the image
[a,map]=imread('arrow.jpg');
[r,c,d]=size(a);
x=ceil(r/100);
y=ceil(c/100);
g=a(1:x:end,1:y:end,:);
g(g==255)=5.5*255;
set(handles.btn,'CData',g);

  0 Comments

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 11 Sep 2019
If you can save your image as a png instead, see this post about displaying png images with transparent backgrounds.
Even with a png, images get displayed in an axes. Even if the image doesn't have a background, it will at least originally display in an axes with a background color set to white. You can turn that off using
axis off
of
set(gca,'Color','none')

  4 Comments

Show 1 older comment
Rocco Didio
Rocco Didio on 12 Sep 2019
Thank you! I did this, but it still has a black backgroung
[A,map,transparency] = imread('arrow.png');
image(A,'AlphaData',transparency);
imshow(A,map,'Parent',handles.fig)
axis off
set(gca,'Color','none')
Cris LaPierre
Cris LaPierre on 12 Sep 2019
You are doing image and imshow. Both display an image, but imshow does not accept the transparency settings. Can you make this work with just image(A,'AlphaData',transparency);

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Translated by