# Images and Programming

W
Shared by:
Categories
Tags
-
Stats
views:
23
posted:
10/27/2011
language:
English
pages:
58
Document Sample

```							Images and Programming
• Basic MATLAB programming
• Grayscale Images
• RGB Images
• Indexed Color Images
• Data Types and Conversions
• Image Files and Formats

1
MATLAB
• Basic Use of MATLAB
– Textbook: Appendix A

• Where to find more helps?
– http://www.mathworks.com/access/helpdesk/help/
toolbox/images/

2
Introduction to MATLAB
• MATLAB is a data analysis and visualization tool
• It has been designed with powerful support for
matrices and matrix operations.
• It has excellent graphics capabilities and its own
powerful programming language.
• There are sets of MATLAB programs designed to
– Image Processing Toolbox

3
Introduction to MATLAB
• MATLAB’s standard data type is the matrix, all data
are considered to be matrices of some sort.
– Images are matrices whose elements are the gray values or
RGB values of its pixels.
– A single value is considered to be a 1x1 matrix
– A string is a 1xn matrix of characters where n is the string’s
length

4
Introduction to MATLAB

When you start up
MATLAB, the
MATLAB desktop will
appear as shown in
the Figure.

The prompt consists
of two right arrows:
>>

5
Basic Use of MATLAB
• MATLAB is command line driven; all commands are
entered by typing them after the prompt symbol.

>> 2+2
ans =
4

6
Basic Use of MATLAB
• MATLAB does all its calculations internally to double
precision.
• The default display format is to use only four decimal
places.
• We can change this by using the format function.
>> 11/7
1.5714
>> format long
>> 11/7
ans =
1.57142857142857

• Entering the command format returns to the default
format

7
Basic Use of MATLAB
• MATLAB has all the elementary
mathematical functions built in: sqrt,
sin, log, log10, pi, etc.

8
Variables and the Workspace
• Variables are used to store values.

>> a = 5^(7/2)
a =
279.5085

>> log(a^2)/log(5)
ans =
7

9
Variables and the Workspace
• Workspace
– It lists all your currently defined variables, their
numeric data types, and their sizes in bytes. The
same information can be obtained using the whos
function
>> whos
Name      Size        Bytes   Class
a         1x1           8     double array
ans       1x1           8     double array
Grand total is 2 elements using 16 bytes

10
Variables and the Workspace
• Workspace (cont)
– Note that ans is a variable. It is automatically created
by MATLAB.
– A listing of the variable names is obtained using who
function:
>> who
a ans

– MATLAB’ standard data type for numbers is double,
and they are stored as double-precision 8-byte
values.

11
Dealing with Matrices
• MATLAB has an enormous number of commands for
generating and manipulating matrices.
• We can use some of these commands to investigate
aspects of the image.
• We can enter a small matrix by listing its elements row
by row, using spaces or commas as delimiters for the
elements in each row and semicolons to separate the
rows, for example,

>>   a = [4 -2 -4 7;1 5 -3 2; 6 -8 -5 -6; -7 3 0 1]

12
Dealing with Matrices
• Matrix Elements
– Matrix elements can be obtained by using the standard row, column-
indexing scheme.
>> a(2, 3)
ans =
-3
– Returns the element of the matrix in row 2 and column 3.
– MATLAB allows matrix elements to be obtained using a single number;
this number being the position where the matrix is written out as a single
column. Thus, the order of elements in a is

1      5     9       13
2      6    10       14
3      7    11       15
4      8    12       16

• a(2,3) = a(10) = -3
13
Dealing with Matrices
– In general, for a matrix M with r rows and c columns,
element m(i, j) corresponds to m(i + r*(j - 1)).

– To obtain a row of values, or block of values, we use
MATLAB’s colon operator (:), for example,

>> 2:3:16
ans =
2 5 8 11 14

14
Dealing with Matrices
– Apply to the matrix a,
>> a(3,1:3)
ans =
6       -8      -5
>> a(2:4,3)
ans =
-3
-5
0
>> a(2:3,3:4)
ans =
-3       2
-5      -6
>> a(3,:)
ans =
6      -8      -5   -6
15
Dealing with Matrices
>> a(:,2)
ans =
-2
-5
-8
3

>> a(:)
ans =
4
1
6
-7
-2
.
.
.
7
2
-6
1

16
Dealing with Matrices
• Matrix Operations
– All the standard operations are supported (add, subtract, multiply, invert
matrix, matrix power, transpose)
>>   2*a-3*b
>>   a^3*b^4
>>   inv(a)
>>   a’
– MATLAB supports some geometric operations on matrices; flipud -
flip a matrix up and down, fliplr - flip a matrix left and right., rot90
rotates a matrix by 90 degree.
>>   flipud(a)
>>   fliplr(a)
>>   rot90(a)
ans   =
-7     3 0 -1          7    -4    -2      4         7 2 -6 1
6   -8 -5 -6          2    -3     5      1        -4 -3 -5 0
1     5 -3 2         -6    -5    -8      6        -2 5 -8 3
4   -2 -4 7           1     0     3     -7         4 1 6 -7                17
Dealing with Matrices
– The reshape function produces a matrix with elements taken column
by column from the given matrix.

>> c=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16
17 18 19 20]
C =      1     2     3     4     5
6     7     8     9     10
11    12    13    14    15
16    17    18    19    20

>> reshape(c,2,10)
>> reshape(c,5,4)
– Reshape produces an error if the product of the two values is not
equal to the number of elements of the matrix.

>> reshape([1:20],5,4) ?
>> reshape([1:20],5,4)’ ?
18
Dealing with Matrices
– Array and Matrix arithmetic operators

Dot operator
= Array operator

Note:
1/0 return Inf

19
Dealing with Matrices
– Special Matrices
• zeros(m,n)       produces a zeros matrix of size m by n
• ones(m,n)        produces an ones matrix pf size m by n

• Vectorization
– Refers to an operation carried out over entire matrix or vector

>> tic, for i=1:10^6, sin(i); end, toc
elapsed_time =
27.4969

>>tic, i=1:10^6; sin(i); toc
elasped_time =
1.3522

– Vectorized commands perform very quickly. They should be used
20
Plots
• MATLAB has outstanding graphics capabilities.

– The plot function can be used to produce many different plots.

>> plot(x,sin(x))

>> plot(x, sin(x), ‘.’, x, cos(x), ‘o’);

21
Help in MATLAB
information.
• To obtain information on a particular command, you can
use help.
>> help lookfor

>> doc help

>> lookfor exp

22
Programming in MATLAB
• MATLAB has a small set of built-in functions, others are
written in MATLAB’s own programming language.
• We consider two distinct programs
– Script files
– Functions

• Script file
– It is a list of commands to be executed

• Function
– It takes an input (one or several variables) and returns one or
several values.

23
• Syntax:
X : 8-bit/16-bit unsigned int matrix

MAP: color palette (Type: Double, Range: [0,1])

• Supported format: BMP, GIF, JPEG, PBM, PCX,
PMG, PNG, PNM, PPM, RAS, TIFF, …
24
MATLAB: Display Image

• Command: imshow
• Syntax (not completed):
imshow(‘filename.ext’)
imshow(X)
imshow(X, MAP)
• Show pixel value by command pixval on.
(Format: column, row = pixel)

25
MATLAB: Write Image

• Command: imwrite
• Syntax:
imwrite(X,   ‘filename.ext’)
imwrite(X,   MAP, ‘filename.ext’)
imwrite(X,   ‘filename’, format)
imwrite(X,   ‘filename.ext’, param1,
value1, param2, value2,
…)

26

Press ENTER and finish..
• What happened with this command?
– intensities of wombats.tif image is
– size of the matrix w = height  width
• No single quote (‘) if the filename is stored in
variable

27
Example: Display Grayscale Image

>> figure
>> imshow(w)
>> pixval on
• What happened with these commands?
– create figure window (figure command)
– display image inside matrix w (imshow command)
– show intensity of the pixel (pixval on command)

28
Example: Display Grayscale Image (cont)

>> figure
>> imshow(‘wombat.tif’)
>> pixval on
• What happened with these commands?
– create figure window (figure command)
– display wombat.tif image (imshow command)
– show intensity of the pixel (pixval on command)
• No image stored in memory!!!

29
Example: Display Grayscale Image (cont)

Figure 2.1 The wombats image with pixel on.
30

Press ENTER and finish..
• What happened with this command?
– size of the matrix a = height  width  3
>> size(a)
ans =
186   230    3

– index of a: a(row, column, page)
• page: 1 = R, 2 = G, 3 = B

31
RGB Color Cube

(a)      (b)

32
Example: Display Pixel Value

>> impixel(a, 200, 100)

• What happened with this command?
– show pixel value of image a at column 200 and row
100.
• If a is the color image, show R G B values at position
(100,200).
• If a is the grayscale image, show I I I where I is the
intensity at position (100,200).

33
Example: Display Pixel Value
>> inshow(a)

>> impixel(a,88,137)
ans =
162 170 182

>> a(100,200,2)
>> a(100,200,1:3)
>> a(100,200,:)

Figure 2.2 The lily flower image with pixel on.

34
>> imshow(em);

Correct?
35

We get something wrong. Why?
– em = index of emu.tif image
– relationship between index and color?

36

Press ENTER.
• What happened with this command?
matrix em.
– size of the matrix em = height  width
matrix emap.
– size of the matrix emap = 256  3

37
Example: Display Indexed Image

>> figure
>> imshow(em, emap)
>> pixval on
• What happened with these commands?
– create figure window (figure command)
– display color from index em with the palette emap
(imshow command)
– show value of the pixel (pixval on command)

38
Example: Display Indexed Image

39
Example: Display Indexed Image

Display without color map   Display with color map

40
MATLAB: Image Information
• Command: imfinfo
• Syntax: imfinfo(‘filename.ext’)
• Information shown:
– Filename, FileModDate, FileSize, Format,
FormatVersion, Width, Height, BitDepth(no of
bits per pixel), ColorType (truecolor,
grayscale, or indexed), etc

41
MATLAB: Data Types
Data Type            Description                     Range
int8                 8-bit integer                   -128 to 127
uint8                8-bit unsigned integer           0 to 255
int16                16-bit integer                  -32768 to 32767
uint16               16-bit unsigned integer          0 to 65535
double               Double precision real number     machine specific

Note: Arithmetic operation allowed only for the data type double.

42
MATLAB: Data Conversion
>> a = 23;
>> b = uint8(a);
>> b
b =
23
>> whos a b
Name      Size        Bytes        Class
a         1x1          8           double array
b         1x1          1           uint8 array

Don’t forget to convert the image to double data before
performing any computations.

43
MATLAB: Image Conversion

Function                Use                 Format
ind2gray      indexedgrayscale   y = ind2gray(x,map);
gray2ind      grayscaleindexed   [y,map] =gray2int(x);
rgb2gray      RGBgrayscale       y = rgb2gray(x);
gray2rgb      grayscaleRGB       x = gray2rgb(y);
rgb2ind       RGBindexed         [x,map] =rgb2int(y);
ind2rgb       indexedRGB          y = ind2rgb(x,map);

44
Vector Image VS Raster Image

• Image = collection of   • Image = collection of
line                      point
• Scale up without loss   • Popular for image
of sharpness              files
• Not suitable for        • Recommended for
natural screen            image processing
• E.g. Adobe PostScript   • E.g. PGM, BMP,
JPEG, …

45
Microsoft BMP Format

BM……….
Byte 0-1      Signature    BM (42 4D)
Byte 2-5      FileSize     File size in Byte
Byte 6-9      Reserved     All zeros
Byte 10-13    DataOffset   File offset to raster data

46
Microsoft BMP Format

Byte 14-17      Size      Size of information header
Byte 18-21      Width     Image width [pixel]
Byte 22-25      Height    Image height [pixel]
Byte 26-27      Planes    Number of image plane (=1)
Byte 28-29      BitCount  Bit/Pixel (1, 4, 8, 16, 24)
Byte 30-33    Compression Type of compression
0 : no compression,
1, 2: RLE encoding
(rarely used)
47
Microsoft BMP Format
Byte 34-37   ImageSize         Image size
Byte 38-41   HorizontalRes     Horizontal resolution
[pixel/meter]
Byte 42-45   VerticalRes       Vertical resolution
[pixel/meter]
Byte 46-49   ColorsUsed        #color used in the
image (0=allcolor,2BitCount)
Byte 50-53   ImportantColors   #important color

48
Number Format in BMP

• Format: Least-endian (little-endian)
• LSB on the lowest address

Ex. Byte 18-21 from BMP file (width):
42 00 00 00  actual number 00 00 00 42

49
GIF Format

• Data are compressed by using LZW (Lempel-Ziv-Welch)
compression (lossless compression)
• Allowed a maximum 256 colors
• Not allowed for grayscale/binary images
• Allow multiple images in one file (can create animated
GIFs)
• It is one of the standard formats supported by the WWW
– signature (3 bytes): GIF
– version (3 bytes): 87a or 89a

50
GIF Format

– screen width (2 bytes) : unsigned
– screen height (2 bytes) : unsigned
– etc.
• Alternative format: PNG
– non-patented algorithm (zlib compression)
– also support binary, grayscale, truecolor and
indexed images
– possible to include alpha channel, gamma
correction, …
51
JPEG Format

• Lossy compression
Byte 0-1      Signature           FF D8 (HEX)
Byte 2-3      Application Maker   FF E0 (HEX)
Byte 4-5      Segment length
Byte 5-10     JFIF\ 0             ASCII JFIF.
Byte 11-12    JFIF version        Version
Byte 13       Units               0: arbitrary,
1: pixel/inch,
2: pixel/cm.

52
JPEG Format

Byte 14-15   Horizontal pixel density
Byte 16-17   Vertical pixel density
Byte 18      Thumbnail width            0 : no thumbnail
Byte 19      Thumbnail height           0 : no thumbnail

53
TIFF Format

• Be able to store multiple image files
• Allow different compression routines (LZW,
JPEG, Huffman, RLE…)
• Allow different byte ordering (little or big-endian)
• Allow binary, grayscale, indexed, truecolor
images
• Good for data exchange

54
TIFF Format

Byte 0-1   Byte order     4D 4D: ASCII MM for big endian
49 49: ASCII II for little endian
Byte 2-3   TIFF version   Always 42 (00 2A: big endian,
2A 00: little endian)
Byte 4-8   Image offset    Pointer to the position of the data
for the first image

55
DICOM Format

•   Format of medical images
•   Able to store multiple images
•   Allow for describing 3D images
•   Header (Very long and variable length!!):
Byte 0-127: preamble (not used)
Byte 128-131: signature (DICM)

56
DICOM Format

– image information (size, #slices, modality
used: CAT, MRI, …)
– patient information (name, patient ID, …)
– compression used (JPEG, RLE, …)
• Complex!!! 

57
Example of MATLAB function
function dumphex(filename, n)
%
% DUMPHEX(FILENAME, N) prints the first 16*N bytes of the file FILENAME
% in hex and ASCII. For example:
%
% dumphex('picture.bmp’, 4)
fid = fopen(filename,'r');
if fid == -1
error('File does not exist or is not in your Matlab path');
end;