1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images...

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

Transcript of 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images...

Page 1: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

1

Images and Programming

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

Page 2: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

2

MATLAB

• Basic Use of MATLAB– Textbook: Appendix A

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

help/toolbox/images/

Page 3: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

3

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

support particular tasks called toolboxes– Image Processing Toolbox

Page 4: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

4

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

Page 5: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

5

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: >>

Page 6: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

6

Basic Use of MATLAB

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

>> 2+2

ans =

4

Page 7: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

7

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/71.5714

>> format long>> 11/7

ans = 1.57142857142857

• Entering the command format returns to the default format

Page 8: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

8

Basic Use of MATLAB

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

Page 9: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

9

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

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

279.5085

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

7

Page 10: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

10

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

arrayGrand total is 2 elements using 16 bytes

Page 11: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

11

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:

>> whoYour variables are a ans

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

Page 12: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

12

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]

Page 13: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

13

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 132 6 10 143 7 11 154 8 12 16

• a(2,3) = a(10) = -3

Page 14: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

14

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

Page 15: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

15

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

Page 16: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

16

Dealing with Matrices >> a(:,2)

ans =-2-5-8 3

>> a(:)ans =

416

-7 -2

.

.

.72

-61

Page 17: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

17

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

Page 18: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

18

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 56 7 8 9 1011 12 13 14 1516 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)’ ?

Page 19: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

19

Dealing with Matrices – Array and Matrix arithmetic operators

Dot operator= Array operator

Note: 1/0 return Inf

Page 20: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

20

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 instead of for loop.

Page 21: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

21

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’);

Page 22: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

22

Help in MATLAB • MATLAB comes with a vast amount of online help and

information.• To obtain information on a particular command, you can

use help.

>> help lookfor

>> doc help

>> lookfor exp

Page 23: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

23

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.

Page 24: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

24

MATLAB: Load Image• Command: imread• Syntax:

X = imread(‘Filename.ext’); X : 8-bit/16-bit unsigned int matrix

[X, MAP]= imread(‘Filename.ext’); MAP: color palette (Type: Double, Range: [0,1])

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

Page 25: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

25

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)

Page 26: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

26

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, …)

Page 27: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

27

Example: Load Grayscale Image

w = imread(‘wombats.tif’) ;

Press ENTER and finish..• What happened with this command?

– intensities of wombats.tif image is downloaded to matrix w.

– size of the matrix w = height width• No single quote (‘) if the filename is stored in

variable

>>

Page 28: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

28

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)

>>

Page 29: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

29

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!!!

>>

Page 30: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

30

Example: Display Grayscale Image (cont)

Figure 2.1 The wombats image with pixel on.

Page 31: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

31

Example: Load RGB Image

a = imread(‘lily.tif’) ;Press ENTER and finish..• What happened with this command?

– color of lily.tif image is downloaded to matrix a.– 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

>>

Page 32: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

32

RGB Color Cube

(a) (b)

Page 33: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

33

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).

>>

Page 34: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

34

Example: Display Pixel Value

Figure 2.2 The lily flower image with pixel on.

>> inshow(a)

>> impixel(a,88,137) ans =

162 170 182

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

Page 35: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

35

Example: Load Indexed Image>> em = imread(‘emu.tif’);

>> imshow(em);

Correct?

Page 36: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

36

Example: Load Indexed Image

>> em = imread(‘emu.tif’);

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

Page 37: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

37

Example: Load Indexed Image

[em, emap] = imread(‘emu.tif’);

Press ENTER.• What happened with this command?

– index of emu.tif image is downloaded to matrix em.

– size of the matrix em = height width– palette of emu.tif image is downloaded to

matrix emap.– size of the matrix emap = 256 3

>>

Page 38: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

38

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)

Page 39: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

39

Example: Display Indexed Image

Page 40: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

40

Example: Display Indexed Image

Display without color map Display with color map

Page 41: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

41

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

Page 42: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

42

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.

Page 43: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

43

MATLAB: Data Conversion

>> a = 23;>> b = uint8(a);>> b

b =23

>> whos a bName Size Bytes Classa 1x1 8 double arrayb 1x1 1 uint8 array

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

Page 44: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

44

MATLAB: Image Conversion

Function Use Formatind2gray indexedgrayscale y = ind2gray(x,map);

gray2ind grayscaleindexed [y,map] =gray2int(x);

rgb2gray RGBgrayscale y = rgb2gray(x);

gray2rgb grayscaleRGB x = gray2rgb(y);

rgb2ind RGBindexed [x,map] =rgb2int(y);

ind2rgb indexedRGB y = ind2rgb(x,map);

Page 45: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

45

Vector Image VS Raster Image

• Image = collection of line

• Scale up without loss of sharpness

• Not suitable for natural screen

• E.g. Adobe PostScript

• Image = collection of point

• Popular for image files

• Recommended for image processing

• E.g. PGM, BMP, JPEG, …

Page 46: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

46

Microsoft BMP Format

• Header (RAW format, unreadable)

BM……….

• Header format (54 bytes)– File header: (Bytes 0-13)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

Page 47: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

47

Microsoft BMP Format

– Information header (Byte 14-53)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)

Page 48: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

48

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

Page 49: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

49

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

Page 50: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

50

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• Header

– signature (3 bytes): GIF– version (3 bytes): 87a or 89a

Page 51: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

51

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, …

Page 52: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

52

JPEG Format

• Lossy compression • Header:

Byte 0-1 Signature FF D8 (HEX)Byte 2-3 Application Maker FF E0 (HEX)Byte 4-5 Segment lengthByte 5-10 JFIF\ 0 ASCII JFIF.Byte 11-12 JFIF version VersionByte 13 Units 0: arbitrary,

1: pixel/inch,

2: pixel/cm.

Page 53: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

53

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

Page 54: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

54

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

Page 55: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

55

TIFF Format

• Header: 8 bytes onlyByte 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

Page 56: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

56

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)

Page 57: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

57

DICOM Format

• Info in Header– image information (size, #slices, modality

used: CAT, MRI, …)– patient information (name, patient ID, …)– compression used (JPEG, RLE, …)

• Complex!!!

Page 58: 1 Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats.

58

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;a=fread(fid,16*n,'uchar');

idx=find(a>=32 & a <=126);ah=dec2hex(a);

b=repmat([' '],1 6 * n, 3 ); b2=repmat('.', 16, n);

b2(idx)=char(a(idx));b(:,1:2)=ah;

[reshape(b', 48, n)' repmat(' ', n, 2) reshape(b2, 16, n)']