Last August I posted an example showing how to display patch objects transparently over an image. I meant to follow that up with another post showing a couple of ways to display one image transparently over another. I was embarrassed to discover recently that I had completely forgotten to post the follow-up.
So here it is!
Handle Graphics image objects can be displayed transparently. In fact, each individual pixel can be assigned a different level of transparency. This can be used in various ways to view one image on top of another. In my first example for today, I'll use a "checkerboard" transparency pattern to view a gray-scale image on top of the original color image.
First, display the color image and the gray-scale image together, in the same place.
rgb = imread('peppers.png'); imshow(rgb); I = rgb2gray(rgb); hold on h = imshow(I); % Save the handle; we'll need it later hold off
Not too surprisingly, only the gray-scale is visible. That's because it's "covering up" the color image. Let's give it a "checkerboard" transparency pattern, so that some of the pixels are fully opaque, and others are fully transparent.
[M,N] = size(I); block_size = 50; P = ceil(M / block_size); Q = ceil(N / block_size); alpha_data = checkerboard(block_size, P, Q) > 0; alpha_data = alpha_data(1:M, 1:N); set(h, 'AlphaData', alpha_data);
Now we can see some of both images. This visualization technique is often used to evaluate image fusion algorithms.
My second transparency example gets a bit more creative. I'll display an image that's a solid color, but I'll use another data set to vary the solid color image's transparency on a pixel-by-pixel basis.
Here's a digital elevation model (DEM) of Peppercorn Hill and North Pond in Massachusetts.
E = imread('peppercorn_hill.png'); imshow(E, 'InitialMag', 'fit')
The bright blob at the upper left is Peppercorn Hill, and the flat, dark plateau in the upper middle is North Pond.
Below is an "influence map." This is a visualization of down-hill water flow, starting from the peak of Peppercorn Hill.
I = imread('peppercorn_hill_influence_map.png'); imshow(I, 'InitialMag', 'fit')
It's difficult to interpret the influence map image on its own, apart from the original DEM. Let's visualize the two images together as follows:
- Display the original DEM image.
- Display a solid green "image" on top of the original image.
- Use the influence map pixels to control the transparency of each pixel of the green image.
imshow(E, 'InitialMag', 'fit') % Make a truecolor all-green image. green = cat(3, zeros(size(E)), ones(size(E)), zeros(size(E))); hold on h = imshow(green); hold off
% Use our influence map image as the AlphaData for the solid % green image. set(h, 'AlphaData', I)
Now it's easy to understand the water flow in the context of the original DEM image. We can see that the water flows from the peak into the pond, then out the southern end of the pond.
So there you go, better late than never. Two more image visualization techniques to add to your bag of tricks.
Get the MATLAB code
Published with MATLAB® 7.7
Comments are closed.
61 CommentsOldest to Newest
print -dtiff myfile.tif
fig = figure('Visible', 'off')Then plot into the figure and use print -dpng to print the figure.
H = hadamard(20); pcolor(H) colormap(gray(2)) axis ij axis square hold on contour(1.5:20.5, 1.5:20.5, H, [.5 .5], 'b')Note that I had to supply the X and Y coordinates explicitly in the call to contour. That's because pcolor and contour assume slightly different coordinate systems by default. Also, I explicitly set the desired color for the contour lines. That's because the contour line colors are usually drawn from the figure colormap, but the figure colormap has been set to grayscale in order to draw the grayscale background image. If you really want the contour line colors to be set automatically from the figure colormap and for that colormap not to be gray, then you'll need to display the background image in truecolor format, whether you use imshow or pcolor.
get(0, 'ScreenPixelsPerInch')5. Print using your monitor's resolution with the '-r' parameter. Let me know if this doesn't work for you. In reference to posts 31-33: Dear Amina, I think you're looking for this function:
image(xLim, yLim, yourImageAsArray)xLim and yLim are two-element row vectors specifying the bounding rectangle into which the image should be placed. Does that help?
%%data is a bunch of images h=image(data(:,:,1),'cdatamapping','scaled'); colormap(gray) hold on; h2=image(data(:,:,2),'cdatamapping','scaled'); colormap(jet) index=find(data(:,:,2)<1); alphadata=zeros(size(data(:,:,1))) alphadata(index)=1; set(h2,AlphaData,alphadata,AlphaDataMapping,'none');Unfortunately this code above doesn't work because I cant set two different color maps for the images in the same figure? Is there a work around besides just making a color map with ala colormap([grey(64),jet(64)]). I cant quite seem to get that to work. Any suggestions / help would be greatly appreciated.
set(0, 'DefaultFigureVisible', 'off') imshow(GrayLevelImage); Mask = cat(3, RedComponent+ zeros(size(GrayLevelImage)), GreenComponent+zeros(size(GrayLevelImage)), BlueComponent+zeros(size(GrayLevelImage))); hold on h = imshow(Mask); hold off set(h, 'AlphaData', TransperancyLevel*BWImage); fig=get(0,'CurrentFigure'); Temp=getframe(fig); Temp=Temp.cdata; close(fig); set(0, 'DefaultFigureVisible', 'on')