Change white pixels to invisible

9 次查看(过去 30 天)
Jacob L
Jacob L 2020-11-26
评论: Matt J 2020-11-26
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.

回答(1 个)

Matt J
Matt J 2020-11-26
编辑:Matt J 2020-11-26
Shouldn't you have
alphachannel = 1-double(all(image == 255, 3));
  3 个评论
Matt J
Matt J 2020-11-26
I recommend you attach all images so we can play with them.
Jacob L
Jacob L 2020-11-26
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by