Steve on Image Processing with MATLAB

Image processing concepts, algorithms, and MATLAB

Note

Steve on Image Processing with MATLAB has been archived and will not be updated.

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.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.