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 support particular tasks called toolboxes – 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 Your variables are 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, columnindexing 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 >> a(2:4,3) ans = -3 -5 0 >> a(2:3,3:4) ans = -3 -5 >> a(3,:) ans = 6 -8 -5 2 -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. >> >> >> ans -7 6 1 4 flipud(a) fliplr(a) rot90(a) = 3 0 -1 -8 -5 -6 5 -3 2 -2 -4 7 7 2 -6 1 -4 -3 -5 0 -2 5 -8 3 4 1 6 -7 7 2 -6 1 -4 -3 -5 0 -2 5 -8 3 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) • ones(m,n) produces a zeros matrix of size m by 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. 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 • 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 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 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, … 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, imwrite(X, imwrite(X, imwrite(X, ‘filename.ext’) MAP, ‘filename.ext’) ‘filename’, format) ‘filename.ext’, param1, value1, param2, value2, …) 26 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 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 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 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 Example: Load Indexed Image >> em = imread(‘emu.tif’); >> imshow(em); Correct? 35 Example: Load Indexed Image >> em = imread(‘emu.tif’); We get something wrong. Why? – em = index of emu.tif image – relationship between index and color? 36 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 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 int8 uint8 int16 uint16 double Description 8-bit integer 8-bit unsigned integer 16-bit integer 16-bit unsigned integer Double precision real number Range -128 to 127 0 to 255 -32768 to 32767 0 to 65535 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 a 1x1 b 1x1 Bytes 8 1 Class double array uint8 array Don’t forget to convert the image to double data before performing any computations. 43 MATLAB: Image Conversion Function ind2gray gray2ind rgb2gray gray2rgb rgb2ind ind2rgb Use indexedgrayscale grayscaleindexed RGBgrayscale grayscaleRGB RGBindexed indexedRGB Format y = ind2gray(x,map); [y,map] =gray2int(x); y = rgb2gray(x); x = gray2rgb(y); [x,map] =rgb2int(y); y = ind2rgb(x,map); 44 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, … 45 Microsoft BMP Format • Header (RAW format, unreadable) BM………. • Header format (54 bytes) – File header: (Bytes 0-13) Byte 0-1 Byte 2-5 Byte 6-9 Byte 10-13 Signature FileSize Reserved DataOffset BM (42 4D) File size in Byte All zeros File offset to raster data 46 Microsoft BMP Format – Information header (Byte 14-53) Byte 14-17 Byte 18-21 Byte 22-25 Byte 26-27 Byte 28-29 Byte 30-33 Size Size of information header Width Image width [pixel] Height Image height [pixel] Planes Number of image plane (=1) BitCount Bit/Pixel (1, 4, 8, 16, 24) Compression Type of compression 0 : no compression, 1, 2: RLE encoding (rarely used) 47 Microsoft BMP Format Byte 34-37 Byte 38-41 ImageSize HorizontalRes Byte 42-45 VerticalRes Byte 46-49 ColorsUsed Byte 50-53 ImportantColors Image size Horizontal resolution [pixel/meter] Vertical resolution [pixel/meter] #color used in the image (0=allcolor,2BitCount) #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 • Header – 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 • Header: Byte 0-1 Byte 2-3 Byte 4-5 Byte 5-10 Byte 11-12 Byte 13 Signature Application Maker Segment length JFIF\ 0 JFIF version Units FF D8 (HEX) FF E0 (HEX) ASCII JFIF. Version 0: arbitrary, 1: pixel/inch, 2: pixel/cm. 52 JPEG Format Byte 14-15 Byte 16-17 Byte 18 Byte 19 Horizontal pixel density Vertical pixel density Thumbnail width Thumbnail height 0 : no thumbnail 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 • Header: 8 bytes only Byte 0-1 Byte order Byte 2-3 TIFF version Byte 4-8 Image offset 4D 4D: ASCII MM for big endian 49 49: ASCII II for little endian Always 42 (00 2A: big endian, 2A 00: little endian) 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 • Info in Header – 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; a=fread(fid,16*n,'uchar'); idx=find(a>=32 & a <=126); ah=dec2hex(a); b=repmat([' '], 16*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)'] 58