a l l a h

79
1 A L L A H

Upload: beyla

Post on 23-Feb-2016

42 views

Category:

Documents


0 download

DESCRIPTION

A L L A H. The Matlab Command window - Finding your way around. Workspace & Directory. Command-Window. Command- History. The Working Directory. - PowerPoint PPT Presentation

TRANSCRIPT

Slide 1

1

A L L A H

Command-WindowWorkspace & DirectoryCommand- HistoryThe Matlab Command window - Finding your way aroundThe Working DirectoryWhen you start Matlab it runs under a default directory/folder which is determined at the time of installation. Issue the command pwd (present working directory) to find out what your default working directory/folder is

To change this to my own chosen directory e:/chris/my_matlabstuff, we use the command cd (change directory).

The Matlab Search PathTyping the Matlab function path will show the current Matlab search path on yourcomputer. Try typing :

Getting started - The Matlab workspaceThe first and simplest way to use Matlab is to enter commands at the Matlab prompt(usually one at a time) and allow Matlab to respond accordingly. Enter the following sequence of Matlab commands :

Typing the command whos at the Matlab prompt shows what variables currently exist in the workspace:

Saving, clearing and loading DataThe easiest way to save the results of a Matlab session is to use the save command :

Typing clear with no specific variables named will clear all variables from the workspace.

Loading the previous session as a result all variable will be restored

M files scripts and functionsTyping directly into the command window is a useful way to get familiar with Matlab and is ideal for trying out relatively simple sequences of commands. the next step up is to write programs. Matlab programs are called m-filesIn the Matlab command window, left-click the file drop-down menu and select1. New Blank M-file.2 The Matlab text editor appears.3 We type a sequence of Matlab commands : and there are two basic kinds scripts and functionsCreating and running a Matlab Script file

To run this file, type the name of the script file at the Matlab prompt and pressreturn -Matlab Function FilesThe second kind of m-file is the Matlab function. Functions are also sequences of Matlab commands but differ from scripts in an important way. The key difference between scripts and functions is that when a function is executed only the declared output arguments are returned to the workspace. Any other variables created within the function vanish when Matlab returns to the workspace.To run this file and see what it does, type the following at the Matlab prompt -

Building Expressions in MatlabExpressions in Matlab employ a combination of variables, operators, numbers and functions. For example, in the following expression :-

Vectorizations:The nice thing about implicit vectorisation is that it helps to avoid ungainly loops and allows us to write more compact,intuitive expressions.

Saving and printing graphicsYou use the Matlab print function with the specified options. print allows you to save an image or graph in a very large number of standard formats. Type For example, typing :-

Programming in Matlab

Creating arrays in MatlabThe simplest way of creating small arrays in Matlab is to manually enter the elements in the command window, row by row. Try entering the commands given below:

A vector is just a 1-D array :

Programming in Matlab contd..

We can turn a row vector into a column vector, by using the transpose operatoror, of course, transpose a 2-D array :We can also create string arrays which contain text

Individual array elements are accessed using subscripts as follows:-We can change or correct the values of array elements by indexing them individually:Arrays can be 3-D (or have even higher dimensionality) :

Matlab has a very important and powerful colon operator ( : ). This can be used to create vectors and for subscripting arrays. Heres a couple of examples of its use in creating vectors -We can also access groups of elements in an array by using the colon operator to specify the vector indices. Try the following :-We can also use the colon operator to extract entire rows or columns of an array:-Assignment

We can easily concatenate (join together) arrays to make a larger arrayThe arrays which form the elements of the concatenated array must be of conformable dimension i.e. the resulting array must be rectangular. For example, trying to form thearrays :

Creating and Dealing With Larger Arrays

The next example creates an array that randomly allocates values of 0 or 1 to the rows of an array by flipping a coin:A second way in which larger arrays can be constructed is through use of appropriate loop constructs.Avoiding loops

Determining the size of arrays

Here are some fun examples to try out :Relational and logical operators

Try the examples below :-Try the examples for logical operators below :-

Try some of these basic examples

If statementsIf else statements

If else statements

For loop

Indexing Vectors

Indexing Vectors contd..

Indexing Vectors contd..

Indexing Vectors contd..

Indexing Vectors contd..

MATLABImage Processing Toolbox

IntroductionCollection of functions (MATLAB files) that supports a wide range of image processing operations

Documentation www.mathworks.comRead an ImageRead in an imageValidates the graphic format(bmp, hdf, jpeg, pcx, png, tiff, xwd)Store it in an arrayclear, close allI = imread(pout.tif`);[X, map] = imread(pout.tif);Display an Imageimshow(I)

Check the Image in Memory< Name, Size, Bytes, Class >whos Name Size Bytes Class ans 291x240 69840 uint8 arrayGrand total is 69840 elements using 69840 bytes

uint8[0, 255]uint16[0, 65535]double[0, 1]Histogram EqualizationHistogram: distribution of intensitiesfigure, imhist(I)Equalize Image (contrast)I2 = histeq(I);figure, imshow(I2)figure, imhist(I2)

Histogram Equalization (cont.)

Histogram Equalization (cont.)

Write the ImageValidates the extensionWrites the image to disk

imwrite(I2, pout2.png);imwrite(I2, pout2.png, BitDepth, 4);Morphological OpeningRemove objects that cannot completely contain a structuring elementEstimate background illuminationclear, close allI = imread(rice.tif);imshow(I)background = imopen(I, strel(disk, 15));imshow(background)Morphological Opening (cont.)

Subtract ImagesCreate a more uniform backgroundI2 = imsubtract(I, background);figure, imshow(I2)

Adjust the Image Contraststretchlim computes [low hight] to be mapped into [bottom top]I3 = imadjust(I2, stretchlim(I2), [0 1]);figure, imshow(I3)

Apply Thresholdingto the ImageCreate a binary thresholded imageCompute a threshold to convert the intensity image to binaryPerform thresholding creating a logical matrix (binary image)level = graythresh(I3);bw = im2bw(I3, level);figure, imshow(bw)Apply Thresholdingto the Image (cont.)

Labeling Connected ComponentsDetermine the number of objects in the imageAccuracy (size of objects, approximated background, connectivity parameter, touching objects)[labeled, numObjects] = bwlabel(bw, 4);numObjects{= 80}max(labeled(:))Select and DisplayPixels in a RegionInteractive selectiongrain = imcrop(labeled)Colormap creation functionRGB_label = label2rgb(labeled, @spring, c, shuffle);imshow(RGB_label);rect = [15 25 10 10];roi = imcrop(labeled, rect)

Object PropertiesMeasure object or region propertiesgraindata = regionprops(labeled, basic)graindata(51).Area{296}graindata(51).BoundingBox{142.5 89.5 24.0 26.0}graindata(51).Centroid{155.3953 102.1791}Create a vector which holds just one property for each objectallgrains = [graindata.Area];whosStatistical Propertiesof Objectsmax(allgrains){ 695 }Return the component label of a grain sizebiggrain = find(allgrains == 695){ 68 }Mean grain sizemean(allgrains){ 249 }Histogram (#bins)hist(allgrains, 20)Statistical Propertiesof Objects (cont.)

Storage Classesdouble (64-bit), uint8 (8-bit), and uint16 (16-bit)Converting (rescale or offset)doubleim2double (automatic rescale and offsetting)RGB2 = im2uint8(RGB1);im2uint16imapprox (reduce number of colors: indexed images)Image TypesIndexData matrix (uint8, uint16, double)Colormap matrix (m x 3 array of double [0 1])Intensity (black = 0, white = )Binary (0, 1)B = logical(uint8(round(A))); (logical flag on)B = +A; (logical flag off)RGB (m x n x 3 of truecolor)Converting Image Typesdithergray2indgraysliceim2bwind2gray

ind2rgbmat2grayrgb2grayrgb2indMultiframe Image ArraysSame size, #planes, colormapStore separate images into one multiframe arrayA = cat(4, A1, A2, A3, A4, A5)Extract frames from a multiframe arrayFRM3 = MULTI(:, :, :, 3)Display a frameimshow(MULTI(:, :, :, 7))

Image Arithmeticimabsdiffimaddimcomplementimdivideimlincombimmultiplyimsubtract

Adding ImagesI = imread(rice.tif);J = imread(cameraman.tif);K = imadd(I, J);imshow(K)Brighten an image results saturationRGB = imread(flowers.tif);RGB2 = imadd(RGB, 50);subplot(1, 2, 1); imshow(RGB);subplot(1, 2, 2); imshow(RGB2);Adding Images (cont.)

Adding Images (cont.)

Subtracting ImagesBackground of a scenerice = imread(rice.tif);background = imopen(rice, strel(disk, 15));rice2 = imsubtract(rice, background);imshow(rice), figure, imshow(rice2);Negative valuesimabsdiffSubtracting Images (cont.)

Multiplying ImagesScaling: multiply by a constant(brightens >1, darkens