Steve on Image Processing

June 25th, 2008

False-color visualization of binary image object sets

Today I want to demonstrate a useful technique to produce a false-color visualization of different sets of binary image objects. Here's the sample image that we'll use:

url = 'http://blogs.mathworks.com/images/steve/2008/segmented_rice.png';
bw = imread(url);
imshow(bw)

Let's look at two sets of objects: Those that touch the image border, and those that do not.

notouch = imclearborder(bw);
imshow(notouch)

The objects that do touch the border can be computed using logical operators:

touch = bw & ~notouch;
imshow(touch)

Here's one way to turn these two sets of objects into a single, false-color, indexed image. First, initialize the index matrix:

X = zeros(size(bw), 'uint8'); % Did you know about
                              % this way to call zeros?

Now assign 1 to the elements of X corresponding to the border-touching set, and assign 2 to the elements corresponding to the interior set.

X(touch) = 1;  % Logical indexing!
X(notouch) = 2;

Now we just need to pick some colors for the color map. I'll make the background white:

map(1,:) = [1 1 1];

Make the touching objects be purple-ish.

map(2,:) = [0.7 0.3 0.8];

And use a green shade for the removed objects.

map(3,:) = [0.4 0.8 0.7];

Now we can display the resulting indexed image.

imshow(X,map)


Get the MATLAB code

Published with MATLAB® 7.6

2 Responses to “False-color visualization of binary image object sets”

  1. Ted replied on :

    Much simpler:
    1. Append 1 pixel white border to original image
    2. Flood fill starting from upper left hand white pixel (0,0) with purple. The 1 pixel border added guarantees that all rice grains touching the edge get filled with purple.
    3. Replace white with aquamarine
    4. Remove 1 pixel from each of the 4 sides

    Longer alternative series of steps given I is input image
    1. A = I with edge touching elements removed
    2. B = I minus A
    3. C = A with white replaced with green
    4. D = B with white replaced with purple
    5. RESULT = C + D

  2. Steve replied on :

    Ted—Thanks.

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.

  • Steve: Kezia—Try imrotate.
  • kezia: steve, how to perform rotation of structuring element by 15 degrees. kindly answer my question. thank u kezia...
  • Steve: Tasha—I only accept comments that are relevant to the particular blog post or are questions or comments...
  • Tasha: Steve,I send you a comment here but still didn’t get any reply yet.I did not see my comment posted here...
  • Steve: Carsten—Thanks for your input.
  • Carsten: Another vote for either imtranslate.m, or at least a blurb in the imtransform help why pure translation...
  • Loren Shure: If you look towards the end of the fftfilt program, you will see that there’s a check to see if...
  • Steve: Sonja—My imwritesize submission on the MATLAB Central File Exchange might be helpful. It was posted...
  • Steve: Grant—Sorry, but it won’t be for R2010a. That development deadline has already passed.
  • Sonja: My publisher is wanting images for a new book to be 300 dpi. Only 5 of the 19 images are 300, the rest are...

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