Functions A complex program may be divided into
Functions A complex program may be divided into several functions. The format of a function is: function [outputValue] = FuncN(inputValue) ... ... ... ... end These functions can improve readability of the code, as well as promote re-usability of the code. Each function must be saved into a different file, with the filename similar to the function.
Example function [Total] = addFcn(number1, number2) Total = number1 + number2; end to call this function, call the fcn name % using your own parameters. c = addFcn(3,4) c = addFcn(a,b) Note: Set path and current directory
Image Manipulation
Image Types Intensity images scaled to represent intensities (uint8 – [0,255], double [0,1]) grayscale Binary images logical array of 0s and 1s Indexed images Look up table [x, map] Color Image RGB images truecolor, array of (MxNx3) Checking the image type : isind, isbw, isgray, isrgb Converting image types: rgb2ind, rgb2gray, gray2ind, ind2gray,….
5 Image Conversion Gray2ind- intensity image to index image im2bw - image to binary Im2double- image to double precision Im2uint8- image to 8-bit unsigned integers Im2uint16- image to 16-bit unsigned integers Ind2gray - indexed image to intensity image mat2gray - matrix to intensity image rgb2gray - RGB image to grayscale rgb2ind - RGB image to indexed image
Reading and Displaying an Image To read an image, use the imread command. Example: read an image file named ‘c:\aaa.jpg’ and store it in matrix A. A = imread(‘c:\aaa.jpg’); A = imread(‘aaa.jpg’); To display an image, use the imshow(A) command. figure, imshow(A)
7 Image Display image - create and display image object imagesc - scale and display as image imshow - display image colorbar - display colorbar getimage- get image data from axes truesize - adjust display size of image zoom - zoom in and zoom out of 2D plot
Saving an Image To save an image, use the imwrite command. Example: write image a into file xxx.jpg: imwrite(A,’xxx.jpg’,’jpg’) imwrite(A,’xxx’,’jpg’) imwrite(A,’xxx.jpg’)
Convert Color Image to Grayscale To convert an RGB image to grayscale, use the rgb2gray command. Example: convert image A to grayscale: B = rgb2gray(A) aaa = imread('d:\...\pepper512.bmp'); figure, imshow(aaa) aaa= im2double(aaa) bbb = rgb2gray(aaa); figure, imshow(bbb) imwrite(bbb,’pepp512.bmp’) imwrite(bbb,’pepper.jpg’)
Examples - Image Manipulation sss = bbb(end:-1:1, :); imshow(sss) sss = bbb(:,end:-1:1); imshow(sss) ppp = bbb(200:300,200:400); imshow(ppp) % Number of pixel = 0 NumZ= numel(find(ppp==0))
Convert Grayscale to Binary Image To convert an grayscale to binary image to, use the im2bwa command. Example: convert image A to grayscale: B = im2bw(A) ccc=im2bw(bbb); figure, imshow(ccc) imwrite(ccc,’pepp512bw.bmp’)
MATLAB Logical Operators MATLAB supports three logical operators. not ~ highest precedence C= ~A and & equal precedence with or C= A & B or | equal precedence with and C= A | B
Image Histogram To display a image histogram use the imhist(I) command. The number of bins in the histogram is specified by the image type. grayscale image - 256 bins. binary image - 2 bins. figure, imhist(bbb) [count,x]=imhist(bbb); figure, imhist(ccc)
ДЗ-IV Function and Image Создать функцию: Fam. Создать функцию: [count,x]=imhLAR(‘image’,FAM) COLOR –> GS(512x512) Show original GS FRAGMENT START- min(FAM)*5 END - max(FAM)*10 Show fragment Negative binary fragment Hist – original GS Hist – orig. frament [count,x]
2422-matlab_ruso_4_-_matlab_fundamentals.ppt
- Количество слайдов: 15

