Steve on Image Processing

March 15th, 2006

Quick tip: Use the functions true and false

All I've been doing this week is writing. Unfortunately, I haven't been writing blog postings. Between my other work responsibilities and an upcoming trip, I have almost no time left for this blog until sometime next week.

Just to keep it going, though, here's a quick tip. I saw some code recently that looked something like this:

B = zeros(size(A));
B = logical(B);

The intent of the code was to initialize an all-zero binary image with the same size as A. Note, however, that the output of the zeros function in the first line is a double-precision array, which requires eight bytes per element. Logical arrays in MATLAB, on the other hand, use only one byte per element. It's quicker and more memory efficient to initialize B using the false function.

The functions true and false are most often seen without input arguments, in which case they return a scalar logical 1 or a scalar logical 0, respectively. But they also support an optional size input. So the following code works just as well for initializing the binary image:

B = false(size(A));

Note: The true and false functions were introduced in MATLAB 6.5.

Comments are closed.


MathWorks
Steve Eddins is a software development manager in the MATLAB and image processing areas at MathWorks. Steve coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

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