Change white pixels to invisible

16 views (last 30 days)
Jacob L
Jacob L on 26 Nov 2020
Commented: Matt J on 26 Nov 2020
Hi, I have a .png I'm trying to overlay on top of an existing figure in matlab. Most of the image is just a white background. Is it possible to overlay everything but the white background from the .png to the existing figure? In other words, can I make the white background transparent/invisible?
The image is RGB 'unit8'. When I do this:
alphachannel = all(image == 255, 3);
imwrite(image,'invisible.png','Alpha',alphachannel);
This error comes up:
Error using writepng>parseInputs (line 349)
The value of 'alpha' is invalid. Expected input to be one of these types:
double, uint8, uint16
Instead its type was logical.
When I do this:
alphachannel = double(all(image ~= 255, 3));
imwrite(image,'invisible.png','Alpha',alphachannel);
The .png file appears to have the invisible pixels instead of the white pixels, but ,when I try to overlay the new .png on top of the existing figure in matlab, the "invisible" background is black. Is there anything I can do? Any help is appreciated, thanks.

Answers (1)

Matt J
Matt J on 26 Nov 2020
Edited: Matt J on 26 Nov 2020
Shouldn't you have
alphachannel = 1-double(all(image == 255, 3));
  3 Comments
Matt J
Matt J on 26 Nov 2020
I recommend you attach all images so we can play with them.
Jacob L
Jacob L on 26 Nov 2020
Okay - here is a summary of what's going on:
Starting with the Teddy_Bear.png
Then using
teddyBearProcessed = padarray(image,[1000,1000],255,'post');
to get Teddy_Bear_Padded.png
Then using
shiftedTeddy = imtranslate(image,[150, 150],'FillValues',255);
to get Teddy_Bear_Shifted.png
Then using
alphachannel = 1-double(all(image == 255, 3));
imwrite(image,'invisible.png','Alpha',alphachannel);
to get Teddy_Bear_Invisible.png
In the meantime I have another figure open in MATLAB. The output of the figure is saved in the attached as Stationary_Teddy_Centroid.png.
Then using
hold on
imshow('Teddy_Bear_Invisible.png');
to get problem.png.
What I'm trying to do is move the teddy bear on the screen so I can see the the path it takes. Kind of like how the path of the arm is seen in Stationary_Teddy_Centroid.png.

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!