Introduction to MATLAB

Dr. C. SELDEV CHRISTOPHER Professor , CSE Department, St. Xavier’s Catholic College of Engineering, Chunkankadai. [email protected]

Power Of Matrix Computations

• A major branch of numerical analysis is devoted to the development of efficient algorithms for matrix computations, a subject that is centuries old and is today an expanding area of research. • Matrix decomposition methods simplify computations, both theoretically and practically. • Algorithms that are tailored to particular matrix structures, such as sparse matrices and near- diagonal matrices, expedite computations in finite element method and other computations. Algorithm Development Environment for Image Processing Widely used commercial packages

1. MATLAB 2. IDL 3. Khoros 4. LabVIEW 5. Scilab 6. Python

WORKSHOP: Soft Computing Tools in Signal 7/10/2020 3 Processing and Image Processing

MATLAB • MATLAB is a program for doing numerical computation. • It was originally designed for solving linear algebra type problems using matrices. • It’s name is derived from MATrix LABoratory. • MATLAB is basically a high level language which has many specialized toolboxes for making things easier for us • Using MATLAB, you can solve technical computing problems faster than with traditional programming languages, such as C, C++, and Fortran • More than a million engineers and scientists in industry and academia use MATLAB

4 Strengths of MATLAB

• MATLAB is relatively easy to learn • MATLAB code is optimized to be relatively quick when performing matrix operations • MATLAB may behave like a calculator or as a programming language • MATLAB is interpreted, errors are easier to fix • Although primarily procedural, MATLAB does have some object-oriented elements • The Mathworks Web site (http:// www.mathworks.com) contains freely

distributable MATLAB addons. 5

Applications of MATLAB

1. Statistics and machine learning(ML) 2. Curve fitting 3. Control systems 4. Signal Processing 5. Mapping 6. Deep learning 7. Financial analysis 8. Image processing 9. Text analysis 10. Electric vehicles designing 11. Aerospace 12. Audio toolbox MATLAB and images

• The help in MATLAB is very good, use it! • An image in MATLAB is treated as a matrix • Every pixel is a matrix element • All the operators in MATLAB defined on matrices can be used on images: +, -, *, /, ^, sqrt, sin, cos etc.

Images in MATLAB

• MATLAB can import/export • Data types in MATLAB several image formats – Double (64-bit double-precision – BMP (Microsoft Windows Bitmap) floating point) – GIF (Graphics Interchange Files) – Single (32-bit single-precision floating point) – HDF (Hierarchical Data Format) – Int32 (32-bit signed integer) – JPEG (Joint Photographic Experts Group) – Int16 (16-bit signed integer) – PCX (Paintbrush) – Int8 (8-bit signed integer) – PNG (Portable Network Graphics) – Uint32 (32-bit unsigned integer) – TIFF (Tagged Image File Format) – Uint16 (16-bit unsigned integer) – XWD (X Window Dump) – Uint8 (8-bit unsigned integer) – MATLAB can also load raw-data or other types of image data

Image import and export

Read and write images in Matlab >> I=imread(' tape.png '); >> imshow(I) >> size(I) ans = 479 600 3 (RGB image) >> Igrey=rgb2gray(I); >> imshow(Igrey) >> imwrite(lgrey, 'cell_gray.tif', '')

Image import and export

Gui read [filename, pathname] = uigetfile('*.jpg', 'Pick an jpg image'); a=imread([pathname,filename]); Alternatives to imshow >>imagesc(I) >>imtool(I) >>image(I)

Images and Matrices

• How to build a matrix (or image)? >> A = [ 1 2 3; 4 5 6; 7 8 9 ]; A = 1 2 3 4 5 6 7 8 9 >> B = zeros(3,3) B = 0 0 0 0 0 0 0 0 0 >> C = ones(3,3) C = 1 1 1 1 1 1 1 1 1

>>imshow(A) (imshow(A,[]) to get automatic pixel range) Images and Matrices

• Accesing image elements (row, column) X >> A(2,1) ans = 4 • : - can be used to extract a whole column or row Y >> A(:,2) ans = 2 5 8 • or a part of a column or row A = >> A(1:2,2) 1 2 3 ans = 4 5 6 2 7 8 9 5 Image Arithmetic

• Arithmetic operations such as addition, subtraction, multiplication and division can be applied to images in MATLAB – +, -, *, / performs matrix operations >> A+A ans = 2 4 6 8 10 12 A = 14 16 18 1 2 3 >> A*A 4 5 6 ans = 30 36 42 66 81 96 7 8 9 102 126 150

• To perform an element wise operation use . (.*, ./, .*, .^ etc) >> A.*A ans = 1 4 9 16 25 36 49 64 81

Logical Conditions

• equal (==) , less than and greater than (< and >), not equal (~=) and not (~) • find(‘condition’) - Returns indexes of A’s elements that satisfies the condition. >> [row col]=find(A==7) A = row = 3 1 2 3 col = 1 4 5 6 >> [row col]=find(A>7) 7 8 9 row = 3 3 col = 2 3

Flow Control

• Flow control in MATLAB - if, else and elseif statements (row=1,2,3 col=1,2,3) if row==col A(row, col)=1; elseif abs(row-col)==1 A(row, col)=2; A = else 1 2 0 A(row, col)=0; 2 1 2 end 0 2 1 Flow Control

• Flow control in MATLAB - for loops

for row=1:3 for col=1:3 if row==col A(row, col)=1; elseif abs(row-col)==1 A = A(row, col)=2; else 1 2 0 2 1 2 A(row, col)=0; 0 2 1 end end end Working with Group of images name= ‘image'; for i=1:10 temp=strcat(name,num2str(i)); filename=strcat(temp,'.jpg'); X=imread(filename); figure;imshow(X); end

Working with M-Files

• M-files can be scripts that simply execute a series of MATLAB statements, or they can be functions that also accept input arguments and produce output. • MATLAB functions: – Are useful for extending the MATLAB language for your application. – Can accept input arguments and return output arguments. – Store variables in a workspace internal to the function.

Use of M-File

Click to create a new M-File

• Extension “.m” • A text file containing script or function or program to run

Image Arithmetic Adding Images clc; close all; clear all; I = imread('rice.png'); J = imread('cameraman.tif'); K = imadd(I, J); figure;imshow(I);title('input image 1'); figure;imshow(J);title('input image 2'); figure;imshow(K);title('Output image'); figure; subplot(2,2,1);imshow(I); subplot(2,2,2);imshow(J); subplot(2,2,3);imshow(K); Adding Images (cont.) Brighten an image results saturation

RGB = imread(‘flowers.tif’); RGB2 = imadd(RGB, 50); subplot(1, 2, 1); imshow(RGB); subplot(1, 2, 2); imshow(RGB2);

Subtracting Images

• Background of a scene rice = imread(‘rice.png’); background = imopen(rice, strel(‘disk’, 15)); rice2 = imsubtract(rice, background); imshow(rice), figure, imshow(rice2); • Negative values imabsdiff Subtracting Images (cont.) Multiplying Images

• Scaling: multiply by a constant • (brightens >1, darkens <1) • Preserves relative contrast I = imread(‘moon.tif’); J = immultiply(I, 1.2); imshow(I); figure, imshow(J)

Multiplying Images (cont.) Dividing Images (Ratioing)

I = imread(‘rice.png’); background = imopen(I, strel(‘disk’, 15)); Ip = imdivide(I, background); imshow(Ip, []) Dividing Images (cont.) Spatial Transformations

• Map pixel locations in an input image to new locations in an output image – Resizing – Rotation – Cropping Resizing Images

• Change the size of an image I = imread(‘ic.tif’); J = imresize(I, 1.25); K = imresize(I, [100 150]); figure, imshow(J) figure, imshow(K) Resizing Images (cont.) Rotating Images

• Rotate an image by an angle in degrees I = imread(‘ic.tif’); J = imrotate(I, 35, ‘bilinear’); imshow(I) figure, imshow(J)

Rotating Images (cont.)