Thread Subject: Overlay image on a plot, with transparent background on image

Subject: Overlay image on a plot, with transparent background on image

From: Stephen

Date: 27 Jan, 2009 17:45:04

Message: 1 of 4

Hello,

I've got an outline map of the US I need to overlay on a patch plot. The map was created in Photoshop and is a 300dpi .png with black lines for the state borders, etc, and everything else transparent. I want to create the patch plot, then overlay the map such that the colors of the patch plot show through the transparent parts of the map.

I have tried doing this according to the two procedures laid out here:

http://www.mathworks.com/support/solutions/data/1-1AK7N.html

However, the transparency of the image read in using imread always seems to default to black, even if I try changing it to something else using whitebg. It also seems to wipe out the patch plot, no matter what.

Does anyone have any suggestions? I would greatly appreciate it, as I've been beating my head going over the same threads again and again. Thanks.

Subject: Overlay image on a plot, with transparent background on image

From: Pete

Date: 27 Jan, 2009 19:53:03

Message: 2 of 4

I've never tried to use image transparencies in MATLAB, so I don't know how to solve it that way. Can you do it the other way around - patch plot on top of the image?

imshow(im, []);
h = patch([10, 200, 200], [10, 10, 200], [1, 0 0]);
set(h, 'FaceAlpha', .4, 'EdgeColor', 'none');

If that means your patch vertices would have to be too accurate, you could load in a simple black and white image, use bwtraceboundary or bwboundaries to get the coordinates of the borders, and plot them on top using line. The image is then only used to get the coordinates, you don't actually display it. This would also give you control over how thick you want the borders to display and it could scale more nicely.

The 'tracing object boundaries' part here might help:
http://www.mathworks.com/access/helpdesk/help/toolbox/images/index.html?/access/helpdesk/help/toolbox/images/f11-11942.html

Subject: Overlay image on a plot, with transparent background on image

From: Pete

Date: 27 Jan, 2009 19:59:02

Message: 3 of 4

Oops, I mistakenly thought imread was part of the Image Processing Toolbox. If you don't have that, my suggestion won't have been very helpful, sorry...

Subject: Overlay image on a plot, with transparent background on image

From: Stephen

Date: 27 Jan, 2009 21:17:02

Message: 4 of 4

Pete,

Thanks for your response. I'm not sure how overlaying a patch plot doesn't wipe out the black lines of the map, unless I give the patch plot an alpha value for transparency, which seems to be what your code does. However, I believe this compromises the original shades of color of the patch plot, right? It's important for me to keep that intact.

I will look into the bwtraceboundary solution. The map has quite complex lines. I assume this is immaterial?

This also still leaves unaddressed the problem of the transparency defaulting to black. When I do imshow(img w/transparent bg) with no other superposition, this always happens. Why does this go to black instead of white or the figure gray?

I believe I do have the image toolbox as well, if it makes a difference.

Thanks.

"Pete " <pete.dot.bankhead@btinternet.dot.com> wrote in message <glnomv$qe$1@fred.mathworks.com>...
> I've never tried to use image transparencies in MATLAB, so I don't know how to solve it that way. Can you do it the other way around - patch plot on top of the image?
>
> imshow(im, []);
> h = patch([10, 200, 200], [10, 10, 200], [1, 0 0]);
> set(h, 'FaceAlpha', .4, 'EdgeColor', 'none');
>
> If that means your patch vertices would have to be too accurate, you could load in a simple black and white image, use bwtraceboundary or bwboundaries to get the coordinates of the borders, and plot them on top using line. The image is then only used to get the coordinates, you don't actually display it. This would also give you control over how thick you want the borders to display and it could scale more nicely.
>
> The 'tracing object boundaries' part here might help:
> http://www.mathworks.com/access/helpdesk/help/toolbox/images/index.html?/access/helpdesk/help/toolbox/images/f11-11942.html

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
superimpose ima... Stephen 27 Jan, 2009 12:50:18
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com