Download presentation
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 Where to find more helps?
Textbook: Appendix A Where to find more helps?
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
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
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: >>
6
Basic Use of MATLAB >> 2+2
MATLAB is command line driven; all commands are entered by typing them after the prompt symbol. >> 2+2 ans = 4
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/7 1.5714 >> format long ans = Entering the command format returns to the default format
8
Basic Use of MATLAB MATLAB has all the elementary mathematical functions built in: sqrt, sin, log, log10, pi, etc.
9
Variables and the Workspace
Variables are used to store values. >> a = 5^(7/2) a = >> log(a^2)/log(5) ans = 7
10
Variables and the 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 1x double array ans 1x double array Grand total is 2 elements using 16 bytes
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: >> who Your variables are a ans MATLAB’ standard data type for numbers is double, and they are stored as double-precision 8-byte values.
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 = [ ; ; ; ]
13
Dealing with Matrices Matrix Elements >> a(2, 3) ans = -3
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 a(2,3) = a(10) = -3
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 =
15
Dealing with Matrices >> a(3,1:3) Apply to the matrix a, ans =
>> a(2:4,3) -3 -5 >> a(2:3,3:4) -3 2 >> a(3,:)
16
Dealing with Matrices ans = -2 -5 -8 3 4 1 6 -7 . 7 2 -6
17
Dealing with Matrices Matrix Operations >> flipud(a)
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 =
18
Dealing with Matrices >> reshape([1:20],5,4) ?
The reshape function produces a matrix with elements taken column by column from the given matrix. >> c=[ ; ; ; ] C = >> 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)’ ?
19
Dealing with Matrices Array and Matrix arithmetic operators
Dot operator = Array operator Note: 1/0 return Inf
20
Dealing with Matrices Vectorization 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 = >>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.
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’);
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
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.
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, …
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)
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, …)
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
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)
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!!! >>
30
Example: Display Grayscale Image (cont)
Figure 2.1 The wombats image with pixel on.
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 = index of a: a(row, column, page) page: 1 = R, 2 = G, 3 = B
32
RGB Color Cube (a) (b)
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).
34
Example: Display Pixel Value
>> inshow(a) >> impixel(a,88,137) ans = >> a(100,200,2) >> a(100,200,1:3) >> a(100,200,:) Figure 2.2 The lily flower image with pixel on.
35
Example: Load Indexed Image
>> em = imread(‘emu.tif’); >> imshow(em); Correct?
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?
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
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)
39
Example: Display Indexed Image
40
Example: Display Indexed Image
Display without color map Display with color map
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
42
MATLAB: Data Types Data Type Description Range
int bit integer to 127 uint bit unsigned integer to 255 int bit integer to 32767 uint bit unsigned integer to 65535 double Double precision real number machine specific Note: Arithmetic operation allowed only for the data type double.
43
MATLAB: Data Conversion
>> b = uint8(a); >> b b = 23 >> whos a b Name Size Bytes Class a 1x double array b 1x uint8 array Don’t forget to convert the image to double data before performing any computations.
44
MATLAB: Image Conversion
Function Use Format ind2gray 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);
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, …
46
Microsoft BMP Format Header (RAW format, unreadable) BM……….
Header format (54 bytes) File header: (Bytes 0-13) Byte Signature BM (42 4D) Byte 2-5 FileSize File size in Byte Byte 6-9 Reserved All zeros Byte DataOffset File offset to raster data
47
Microsoft BMP Format Information header (Byte 14-53)
Byte Size Size of information header Byte Width Image width [pixel] Byte Height Image height [pixel] Byte Planes Number of image plane (=1) Byte BitCount Bit/Pixel (1, 4, 8, 16, 24) Byte Compression Type of compression 0 : no compression, 1, 2: RLE encoding (rarely used)
48
Microsoft BMP Format Byte 34-37 ImageSize Image size
Byte HorizontalRes Horizontal resolution [pixel/meter] Byte VerticalRes Vertical resolution [pixel/meter] Byte ColorsUsed #color used in the image (0=allcolor,2BitCount) Byte ImportantColors #important color
49
Number Format in BMP Format: Least-endian (little-endian)
LSB on the lowest address Ex. Byte from BMP file (width): actual number
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
51
GIF Format Alternative format: PNG 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, …
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 length Byte JFIF\ 0 ASCII JFIF. Byte JFIF version Version Byte 13 Units 0: arbitrary, 1: pixel/inch, 2: pixel/cm.
53
JPEG Format Byte 14-15 Horizontal pixel density
Byte Vertical pixel density Byte 18 Thumbnail width 0 : no thumbnail Byte 19 Thumbnail height 0 : no thumbnail
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
55
TIFF Format Header: 8 bytes only
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
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 : signature (DICM)
57
DICOM Format Info in Header
image information (size, #slices, modality used: CAT, MRI, …) patient information (name, patient ID, …) compression used (JPEG, RLE, …) Complex!!!
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([' '], 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)']
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.