Lecture 3: Matlab Fundamentals: computer representation, data classes, image display and types

Learning Objectives:

  • Number representation and data classesin Matlab
  • Data storage and image headers
  • Create and scale display of a test imagein Matlab
  • Identify basic image types using Matlab internal test images

Assignment:

  1. Read pages 34-42 in Chapter 1 of “Numerical Computing with Matlab,” by Cleve Moler (
  1. Number representation in Matlab
  2. Default is “double precision floating point”
  3. IEEE standard
  4. Recall that most of the Real numbers are not computable.
  1. This would require infinite precision and unlimited computer memory.
  2. Practically, Real number operations are approximated within finite memory registers.

x=±(1+f)∙2ef ≡ mantissa, 0≤f<1. Determines precision

e ≡ exponent. Determines range

Single precision (SP): 32 bit (4 bytes): 23 bit mantissa, 1 bit sign, 8 bit exponent.

Double precision (DP): 64 bit (8 bytes): 52 bit mantissa, 1 bit sign, 11 bit exponent.

What does this mean in practical terms?

SP: The product 223f is an integer in the interval 0≤223f<223, -127≤e<128.

DP: The product 252f is an integer in the interval 0≤252f<252,-1023≤e<1024.

The sign of the exponent is handled by offsetting the exponent by 1023 for DP and by 255 for SP.

Examples:

Consider a simple example where 3 bits are used to represent f, and the range of the exponent is -4≤e<3 (also a 3 bit number).

What is the smallest positive number that can be represented for this specific example?

s:[0] f:[0 0 0]e:[0 0 1]

+ 1 ∙ 2-4 = 2-4 = 1/16,

where we have offset the exponent by 4 to represent it as a positive binary number.

What is the largest positive number that can be represented?

s:[0] f:[1 1 1] e:[1 1 1]

+ 1.875 ∙ 23 = 15.

23f is an integer in the range 0≤23f<23. For maximum mantissa, then 23f = 7, which implies f = 7/8, and 1+f = 1.875.

What is the increment between numbers?

s:[0] f:[0 0 1] e:[0 0 1]

+ 1.125 ∙ 2-4 = 2-4 = 9/128.

Increment between small positive and next largest number is 9/128-1/16 = 1/128.

s:[0] f:[1 1 0] e:[1 1 1]

+ 1.75 ∙ 23 = 14.

Increment between largest positive and next smallest number is 15-14 = 1.

What is the fundamental resolution of this number representation?

Intuitively we notice that f is changing in increments of 1/8. Consider the distance from 1 to the next largest floating point number:

s:[0] f:[0 0 0] e:[1 0 0] = 1

s:[0] f:[0 0 1] e:[1 0 0] = 1.125

Increment is 2-3 = 1/8. This is the definition of the “machine epsilon (eps).” It is a measure of the numerical precision of the floating point representation of the machine.

What is eps for SP floating point number representation?

SP: f is represented by 24 bits implies that eps = 2-23.

DP: f is represented by 52 bits implies that eps = 2-52.

  1. Data classes
  2. Double and single fpn
  1. uint8,uint16, uint32

int: [1 1 1 1 1 1 1 1] = 8 bits of memory or 1 byte

27+26+25+24+23+22+21+20 = 128+64+32+16+8+4+2+1 = 255, range [0, 255].

uint16 –> 16 bits or 2 bytes [0, 65535]

uint32 -> 32 bits or 4 bytes [0, 4294967295]

  1. Int8, int16, int32

s:[0] int:[1 1 1 1 1 1 1] = 64+32+16+8+4+2+1 = 127, range [-128, 127]

int16 –> 16 bits or 2 bytes [-32768, 32767]

int32 -> 32 bits or 4 bytes [-2147483648, 2147483647]

  1. Char
  2. Unicode representation -> 2 bytes per element
  3. Logical(Binary)
  4. 1 byte per element
  1. Data Storage
  2. Big and little Endian (“Byte order” or “byte swapping:”)
  3. Data storage
  4. memory address begins with most significant byte (MSB) = “Little endian”
  5. memory address begins with least significant byte (LSB) = “Big endian”

Consider a 2-byte (16 bit) integer depicted by the brackets below:

[MSB...(byte 1)][(byte2)...LSB](Little Endian)

Address: ... 21

Or, after reflection, or swapping of the bytes

[(byte 2)...LSB][MSB...(byte 1)](Big Endian)

Address: ... 21

  1. Real vs. Complex data storage
  2. Recall that the complex numbers are displayed as a single number in the Matlab interface
  3. However, Matlab and other image processing languages actually store complex data as 2 vectors (or arrays) of double precision numbers
  4. Real matrices require only a single vector or array.
  5. Often an additional vector stores the dimensions (N,M) of the matrix or image size; this vector can be stored with the data in a structure called a “header.”
  1. “Raw” Images vs. Specific image formats (“Headers”)
  2. Raw image data is stored without a header
  3. Conventional file formats such as “bitmap” have “headers”
  4. The Header is usually a structure formatted with a “field” and value associated with that field.
  5. The fields and values might describe the matrix size, type of image (i.e. intensity vs. RGB or color), compression used etc…

Consider an example of the Digital Imaging and Communications in Medicine (DICOM) format standard. The header contains specific information about the image data that follows (Figure 1a). Specific memory addresses (or fields) in the header contain a range of information including, matrix size, data class, image type, and default display options.

Figure 1aFigure 1b