Thread Subject:
make a color of a png image transparent

Subject: make a color of a png image transparent

From: Ivan

Date: 28 Feb, 2009 21:10:10

Message: 1 of 13

i load my png image, the background should be transparent but it's
black

      im = imread('asd.png');
      hold on;
      imOb = imagesc([a b], [c d], im);

how could I make transparent the black background? Would be fine too
that all the black pixel become transparent pixel

Subject: make a color of a png image transparent

From: Bruno Luong

Date: 28 Feb, 2009 21:20:19

Message: 2 of 13

Ivan <ivan.dipaola@gmail.com> wrote in message <fa4d9906-3ef5-4196-a723-8e7256035532@h20g2000yqn.googlegroups.com>...
> i load my png image, the background should be transparent but it's
> black
>
> im = imread('asd.png');
> hold on;
> imOb = imagesc([a b], [c d], im);
>
> how could I make transparent the black background?

You can make black the transparent background (c.f. option property 'BackgroundColor' of imread), not the opposite.

Bruno

Subject: make a color of a png image transparent

From: Ivan

Date: 2 Mar, 2009 11:57:17

Message: 3 of 13

On 28 Feb, 22:20, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> Ivan <ivan.dipa...@gmail.com> wrote in message <fa4d9906-3ef5-4196-a723-8=
e7256035...@h20g2000yqn.googlegroups.com>...
> > i load my png image, the background should be transparent but it's
> > black
>
> > =A0 =A0 =A0 im =3D imread('asd.png');
> > =A0 =A0 =A0 hold on;
> > =A0 =A0 =A0 imOb =3D imagesc([a b], [c d], im);
>
> > how could I make transparent the black background?
>
> You can make black the transparent background (c.f. option property 'Back=
groundColor' of imread), not the opposite.
>
> Bruno

but if I use for example

        im =3D imread('asd.png','BackgroundColor',[1 1 1]);

the background become white, and not transparent!
(I have to plot this image over rectangle(...) and line(...) objects,
so the background must be invisible)

Subject: make a color of a png image transparent

From: Bruno Luong

Date: 2 Mar, 2009 12:51:01

Message: 4 of 13

Ivan <ivan.dipaola@gmail.com> wrote in message <bab6f880-1ed3-40b2-a782-5c9234ef5b06@33g2000yqm.googlegroups.com>...
> On 28 Feb, 22:20, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> > Ivan <ivan.dipa...@gmail.com> wrote in message <fa4d9906-3ef5-4196-a723-8=
> e7256035...@h20g2000yqn.googlegroups.com>...
> > > i load my png image, the background should be transparent but it's
> > > black
> >
> > > =A0 =A0 =A0 im =3D imread('asd.png');
> > > =A0 =A0 =A0 hold on;
> > > =A0 =A0 =A0 imOb =3D imagesc([a b], [c d], im);
> >
> > > how could I make transparent the black background?
> >
> > You can make black the transparent background (c.f. option property 'Back=
> groundColor' of imread), not the opposite.
> >
> > Bruno
>
> but if I use for example
>
> im =3D imread('asd.png','BackgroundColor',[1 1 1]);
>
> the background become white, and not transparent!
> (I have to plot this image over rectangle(...) and line(...) objects,
> so the background must be invisible)

But that's what I wrote: You can assign the transparent background with specified color, NOT THE OPPOSITE.

A workaround: You could use 'alphadata' property to set specified pixel to transparent. But you must able to detect which pixel are originally transparent by setting a unique BackgroundColor (a color that does not exist in your image) when reading the image.

uniquebgcolor=[0 0 0]; % <- select a color that does not exist in your image!!!
im = imread('asd.png','BackgroundColor',uniquebgcolor);
mask = bsxfun(@eq,im,reshape(uniquebgcolor,1,1,3));
image(im,'alphadata',1-double(all(mask,3)));

% Bruno

Bruno

Subject: make a color of a png image transparent

From: Ivan

Date: 2 Mar, 2009 13:33:58

Message: 5 of 13

On 2 Mar, 13:51, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> Ivan <ivan.dipa...@gmail.com> wrote in message <bab6f880-1ed3-40b2-a782-5=
c9234ef5...@33g2000yqm.googlegroups.com>...
> > On 28 Feb, 22:20, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> > > Ivan <ivan.dipa...@gmail.com> wrote in message <fa4d9906-3ef5-4196-a7=
23-8=3D
> > e7256035...@h20g2000yqn.googlegroups.com>...
> > > > i load my png image, the background should be transparent but it's
> > > > black
>
> > > > =3DA0 =3DA0 =3DA0 im =3D3D imread('asd.png');
> > > > =3DA0 =3DA0 =3DA0 hold on;
> > > > =3DA0 =3DA0 =3DA0 imOb =3D3D imagesc([a b], [c d], im);
>
> > > > how could I make transparent the black background?
>
> > > You can make black the transparent background (c.f. option property '=
Back=3D
> > groundColor' of imread), not the opposite.
>
> > > Bruno
>
> > but if I use for example
>
> > =A0 =A0 =A0 =A0 im =3D3D imread('asd.png','BackgroundColor',[1 1 1]);
>
> > the background become white, and not transparent!
> > (I have to plot this image over rectangle(...) and line(...) objects,
> > so the background must be invisible)
>
> But that's what I wrote: You can assign the transparent background with s=
pecified color, NOT THE OPPOSITE.
>
> A workaround: You could use 'alphadata' property to set specified pixel t=
o transparent. But you must able to detect which pixel are originally trans=
parent by setting a unique BackgroundColor (a color that does not exist in =
your image) when reading the image.
>
> uniquebgcolor=3D[0 0 0]; =A0% <- select a color that does not exist in yo=
ur image!!!
> im =3D =A0imread('asd.png','BackgroundColor',uniquebgcolor);
> mask =3D bsxfun(@eq,im,reshape(uniquebgcolor,1,1,3));
> image(im,'alphadata',1-double(all(mask,3)));
>
> % Bruno
>
> Bruno

thanks a lot!!! :)

Subject: make a color of a png image transparent

From: Ivan

Date: 2 Mar, 2009 15:36:58

Message: 6 of 13

just another little thing, is it possible to specify the visualization
layer of a graphic object? (usually each new graphic object that you
add go on a "upper" layer, so it go over and cover all objects
precedently added)

Subject: make a color of a png image transparent

From: Bruno Luong

Date: 2 Mar, 2009 16:02:54

Message: 7 of 13

Ivan <ivan.dipaola@gmail.com> wrote in message <a7adb68a-7b39-4dd5-920d-4503637ac1ce@q11g2000yqh.googlegroups.com>...
> just another little thing, is it possible to specify the visualization
> layer of a graphic object? (usually each new graphic object that you
> add go on a "upper" layer, so it go over and cover all objects
> precedently added)

Permuting the children of the current axe.

c=get(gca,'children');
set(gca,'children',c(randperm(length(c)))

% Bruno

Subject: make a color of a png image transparent

From: Ivan

Date: 2 Mar, 2009 19:08:19

Message: 8 of 13

On 2 Mar, 17:02, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> Ivan <ivan.dipa...@gmail.com> wrote in message <a7adb68a-7b39-4dd5-920d-4503637ac...@q11g2000yqh.googlegroups.com>...
> > just another little thing, is it possible to specify the visualization
> > layer of a graphic object? (usually each new graphic object that you
> > add go on a "upper" layer, so it go over and cover all objects
> > precedently added)
>
> Permuting the children of the current axe.
>
> c=get(gca,'children');
> set(gca,'children',c(randperm(length(c)))
>
> % Bruno

thanks! changing the order of get(gca,'children') it works!

But only till the image that you helped to make transparent is
invisible, when i call:
         set(imOb, 'Visible', 'on');
the layers mess up.... O.o

Subject: make a color of a png image transparent

From: Bruno Luong

Date: 2 Mar, 2009 19:46:01

Message: 9 of 13

> the layers mess up.... O.o

Too bad. But reading related issue:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/245715

could show us one or two useful tricks.

Bruno

Subject: make a color of a png image transparent

From: Ivan

Date: 3 Mar, 2009 13:11:54

Message: 10 of 13

On 2 Mar, 20:46, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> > the layers mess up.... O.o
>
> Too bad. But reading related issue:
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/245715
>
> could show us one or two useful tricks.
>
> Bruno

the problem is that i can define the Z coordinate only for the line
function, and not for the rectangle function and the imagesc/image
function

Subject: make a color of a png image transparent

From: Bruno Luong

Date: 3 Mar, 2009 13:26:02

Message: 11 of 13

Ivan <ivan.dipaola@gmail.com> wrote in message <d002069a-1021-4201-bca2-ba5413d508fc@33g2000yqm.googlegroups.com>...
> On 2 Mar, 20:46, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> > > the layers mess up.... O.o
> >
> > Too bad. But reading related issue:
> >
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/245715
> >
> > could show us one or two useful tricks.
> >
> > Bruno
>
> the problem is that i can define the Z coordinate only for the line
> function, and not for the rectangle function and the imagesc/image
> function

The image have default z value of 0. Use patch or surf to put an image at different depth. Beside that I don't understand the need of overlaying multiple images/object in the same layer. That's a very good way to confuse graphics rendering.

Bruno

Subject: make a color of a png image transparent

From: Ivan

Date: 3 Mar, 2009 19:00:03

Message: 12 of 13

On 3 Mar, 14:26, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> Ivan <ivan.dipa...@gmail.com> wrote in message <d002069a-1021-4201-bca2-ba5413d50...@33g2000yqm.googlegroups.com>...
> > On 2 Mar, 20:46, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> > > > the layers mess up.... O.o
>
> > > Too bad. But reading related issue:
>
> > >http://www.mathworks.com/matlabcentral/newsreader/view_thread/245715
>
> > > could show us one or two useful tricks.
>
> > > Bruno
>
> > the problem is that i can define the Z coordinate only for the line
> > function, and not for the rectangle function and the imagesc/image
> > function
>
> The image have default z value of 0. Use patch or surf to put an image at different depth. Beside that I don't understand the need of overlaying multiple images/object in the same layer. That's a very good way to confuse graphics rendering.
>
> Bruno

i have in the same axes, some rectangles and line, used to make a node-
graph and a little image that is moved around the nodes. So i need
that the image is over all, after the nodes (rectangle) and at last
the bow that connect the nodes (line).

Subject: make a color of a png image transparent

From: Zhuo Li

Date: 28 Jun, 2012 21:12:13

Message: 13 of 13

Ivan <ivan.dipaola@gmail.com> wrote in message <fa4d9906-3ef5-4196-a723-8e7256035532@h20g2000yqn.googlegroups.com>...
> i load my png image, the background should be transparent but it's
> black
>
> im = imread('asd.png');
> hold on;
> imOb = imagesc([a b], [c d], im);
>
> how could I make transparent the black background? Would be fine too
> that all the black pixel become transparent pixel

I haven't found a way to make it really transparent.
I think instead of making the background color of the png image really transparent. one way to go is using:

logo = imread('LOGO.png','BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
axes(handles.lamLOGO);
imshow(lam);

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
axes Zhuo Li 28 Jun, 2012 17:14:14
image background c... Zhuo Li 28 Jun, 2012 17:14:14
png Zhuo Li 28 Jun, 2012 17:14:14
rssFeed for this Thread

Contact us