Transparent graphics objects can be used effectively to visualize image processing concepts. Two particularly useful techniques are:
- Highlighting image regions with transparent patches
- Displaying one image transparently over another
Today I'll show how to highlight image regions with patches. For this example I'll use the 'Extrema' measurement returned by regionprops. The extrema for a given object are eight points: the left-most pixel on the bottom, the right-most pixel on the bottom, the top-most pixel on the right, the bottom-most pixel on the right, and so on.
I'll start with the rice image, segmenting it using techniques I've shown before.
I = imread('rice.png');
imshow(I)
Even out the illumination with the tophat operator, threshold, and then clean up the thresholded image a bit.
I2 = imtophat(I, ones(15, 15)); bw = im2bw(I2, graythresh(I2)); bw2 = bwareaopen(bw, 5); bw3 = imclearborder(bw2); imshow(bw3)
Label the binary objects and compute the extrema.
L = bwlabel(bw3);
s = regionprops(L, 'Extrema');Each object has 8 extrema points associated with it.
s(1).Extrema
ans =
11.5000 86.5000
12.5000 86.5000
34.5000 100.5000
34.5000 102.5000
33.5000 103.5000
27.5000 103.5000
9.5000 89.5000
9.5000 87.5000
We can superimpose the extrema-bounded shapes on top of the original rice image by using patch objects.
imshow(I) hold on for k = 1:numel(s) x = s(k).Extrema(:,1); y = s(k).Extrema(:,2); patch(x, y, 'g') end hold off
The above visualization is pretty clear. If you zoom in on some of the odd, larger shapes, though, you can't really tell what's going on.
axis([120 200 1 75])
We solve that by displaying the patches transparently.
imshow(I) hold on for k = 1:numel(s) x = s(k).Extrema(:,1); y = s(k).Extrema(:,2); patch(x, y, 'g', 'FaceAlpha', 0.3) end hold off
Now if you zoom in on the same region, we can see exactly what caused the unusual region.
axis([120 200 1 75])
Two of the rice grains were touching.
Next time I'll show a couple of techniques for visualizing one image transparently superimposed on another.
Get
the MATLAB code
Published with MATLAB® 7.6



Hi.
How could I do this same thing with image vales within the patches?
cara
Cara—I do not understand your question. Can you clarify it?
Hi.
Sorry for the confusion. I would like to produce an image with a transparency similar to what you have done here. Except I would to fill the patches with data from another image. For example, I would like to make a transparency made of patches, where the patches are filled with elevation data. I would like to then overlay the patch transparency onto another image.
cara
Cara—You can use your elevation data as a texture map on a flat surface object. You might want to take a look at the texture mapping section in the MATLAB 3-D visualization doc, or at the doc for surface properties.
Hi Steve,
As you shown in your example above you have connected two objects on the image, i want to do the opposite, can we seperate the connected two or more objects, in the image?
thanks
sincerely
DP
DP—Check out the segmentation demos in the Image Processing Toolbox.
Hi Steve,
Thanks for all your helpful tutorials. I have a problem trying to overlay a PET image with gray colormap onto a CT image with hot colormap. My data set is unit16. I am SUCCESSFULLY able to overlay the images but the background CT image is VERY unclear/dark (hardly visible). My code looks like this…. Kindly advise
I_CT = Load CT_IMAGE;
imshow(I_CT,’displayrange’,[]); colormap(gray)
I_PET = Load PET_IMAGE;
hold on
imshow(I_PET,’displayrange’,[]); colormap(hot)
set(I_PET_h,’alphadata’,0.5)
hold off
Hi again, i could successfully show PET and CT image in 2 different axes in one figure, but still having difficulty seeing the background CT image after overlaying them. please help. Thanks in advance
Abc—You could try varying the display range for I_CT and also the ‘AlphaData’ parameter.