Apply transparent background to a figure

I want to apply only to the background (white) of the figure "background.png" a certain level of transparency (thus keeping the pixels black).
This figure then needs to overlap with the "background_pink.png" figure.
I am using the following code:
figure();
folder_immagine_png = 'C:\...';
filename_png = 'background_pink.png';
ful_file_png = fullfile(folder_immagine_png,filename_png);
imshow(ful_file_png)
hold on
figure();
folder_immagine = 'C:\....';
filename = 'background.png';
ful_file = fullfile(folder_immagine,filename);
imshow(ful_file)
hold off
I have tried using the AlphaData command but to no avail.

4 Comments

It is not getting clear to me, what this means: "I want to apply only to the background (white) of the figure "background.png" a certain level of transparency (thus keeping the pixels black)."
What is the wanted output? A displayed image on the screen or another file? What do you want to see through the transparent parts?
Dear @Jan, Let me explain in more detail.
1) First of all, the figure "background.png" must be positioned above the figure "background_pink.png".
2) I want to figure out how to make the figure "background.png" transparent (with variable transparency values) so that I can see the figure "background_pink.png".
3) Since transparency is applied to the whole "background.png" figure, I would like (always if it is possible!) that only the white background of the "background.png" figure is transparent (with varying transparency values) while keeping the color of the black curve.
"What is the wanted output? A displayed image on the screen or another file?"
The output is to plot a figure to be displayed on the screen.

Sign in to comment.

 Accepted Answer

im_bw = imread('background.png');
im_pink = imread('background_pink.png');
% make an image of the pink background as-is:
image(im_pink)
hold on
% varying (in this case, random) opacity values:
alpha = rand(size(im_bw));
% set alpha to 1 (fully opaque) where im_bw is 0 (black):
alpha(im_bw == 0) = 1;
% add the new black-and-white image with opacity:
image(repmat(255*im_bw,[1 1 3]),'AlphaData',alpha)

5 Comments

OK, thank you!
How can I save this created image?
I also wanted to retrieve images from appropriate folders, but the code doesn't work. Any recommendations?
folder_IMMAGINE = 'C:\Users\......';
filename_IMMAGINE = 'background.png';
ful_file_IMMAGINE = fullfile(folder_IMMAGINE, filename_IMMAGINE);
% imshow(ful_file_IMMAGINE)
folder_pink = 'C:\Users\.....';
pink = 'background_pink.jpg';
ful_file_pink = fullfile(folder_pink, pink);
imshow(ful_file_pink)
hold on
% varying (in this case, random) opacity values:
alpha = rand(size(im_bw));
% alpha = rand(size(im_bw));
% set alpha to 1 (fully opaque) where im_bw is 0 (black):
alpha(filename_IMMAGINE == 0) = 1;
% add the new black-and-white image with opacity:
image(repmat(255*filename_IMMAGINE,[1 1 3]),'AlphaData',alpha)
alpha(filename_IMMAGINE == 0) = 1;
You are comparing the file name characters to 0. You never read the content of the file.
Could you tell me how to save the created image?
Again, you compose images by working on the data itself, not trying to capture a figure.
im_bw = imread('background.png');
im_pink = imread('background_pink.png');
% varying (in this case, random) opacity values:
alpha = rand(size(im_bw));
% set alpha to 1 (fully opaque) where im_bw is 0 (black):
alpha(im_bw == 0) = 1;
% compose the image directly
outpict = alpha.*im2double(im_bw) + (1-alpha).*im2double(im_pink);
imwrite(outpict,'op1.png')
The above example assumes that the alpha array is of floating-point class.

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Asked:

on 2 Nov 2022

Commented:

DGM
on 3 Nov 2022

Community Treasure Hunt

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

Start Hunting!