Steve on Image Processing

August 20th, 2008

Image visualization using transparency

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

6 Responses to “Image visualization using transparency”

  1. Cara Schiek replied on :

    Hi.

    How could I do this same thing with image vales within the patches?

    cara

  2. Steve replied on :

    Cara—I do not understand your question. Can you clarify it?

  3. Cara replied on :

    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

  4. Steve replied on :

    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.

  5. DP replied on :

    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

  6. Steve replied on :

    DP—Check out the segmentation demos in the Image Processing Toolbox.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • murat: Hi Steve, I have an rgb image of a kind of cream and it contains some small black particles (black dots). In...
  • Steve: Ernest—Look at setting the FaceColor property. The code for setting that is shown on the page you asked...
  • Ernest Miller: Hi Steve, Understood. However, can you explain how to change the colors? Thanks, Ernest
  • Jan: Hi Steve Very useful code, yet what if I parts of my rotated+translated object are outside the original...
  • Steve: MoHDa—It might be possible. You’ll need to use one of the options that produces closed edge...
  • MoHDa: I have one question about the ROIPOLY: I have an image with stripes, I use the “edge” command for...
  • Steve: Shahn—My November 17, 2006 post shows you how to do it.
  • Steve: Kay-Uwe—Thanks for following up. I am planning to make it easier to use test directories in a package....
  • shahn: Hello Steve Instead of superimposing a star on the image to show the centroide. How would you superimpose a...
  • Kay-Uwe: Having TestSuite.fromPackag e() would be nice to have, but so far using simple “test” subdirs...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.