250 likes | 606 Vues
Programming and Image Processing with Matlab. m-files Input/Output control control flow relational and logical operators Image Processing Basics. Contents:. M-files. Files that contain a Matlab code are called the m-files .
E N D
Programming and Image Processing with Matlab • m-files • Input/Output control • control flow • relational and logical operators • Image Processing Basics Contents:
M-files • Files that contain a Matlab code are called the m-files. • The m-files may take input arguments and/or return output arguments. Creating a new m-file Save the m-file as,e.g. function_name.m
Input/Output • To read in a value, we can use the ‘input’ function a=input('Please enter an integer: '); Please enter an integer: 5 • To display as output a number or a string • disp(‘The value of the variable b is ’), disp(b) • The value of the variable b is 5
Passing function arguments/results • Here is an example of a function file function[c] = add(a,b) c=a+b; disp('The sum of a and b is'),disp(c) Input arguments Output argument To call this function: >>f=add(5,8)
Control flow To control the flow of commands, MATLAB supplies four methods • the for loops • the while loops • the if-else-end constructions • the switch-case constructions
For Loops for k = array commands end The commands between the for and end statements are executed for all values stored in the array. Example: sum = 0; %set sum to 0 for n = 1:100 %set n to 1, increment by 1 (default) until 100 is reached disp(n) %display n sum = sum + n %add n to the sum end
While Loops while condition statement(s) end The commands between the while and end statements are executed until the condition is satisfied. While loop is used when the programmer does not know the number of repetitions a priori. Example: q = 3; while q > 0.01 %Keeps divinding q by 2 as long as q q = q/2; %is greater than 0.01 end q = 0.005859375
If-else statement if expression commands (evaluated if expression is true) else commands (evaluated if expression is false) end Example: a = input('Please input an integer :'); if mod(a,2)==0 %if modulus after division by 2 is 0, the number is even disp('You entered an even number') else disp('You entered an odd number') end
Relational Operators Comparisons in MATLAB are performed with the aid of the following operators Operator == compares two variables and returns ones when they are equal and zeros otherwise.
Logical Operators There are three logical operators available in MATLAB | Or & And ~ Not Suppose we want to select all entries x that satisfy the inequalities x >=1 or x < -0.2 >>x = randn(1,7) x = -0.4326 -1.6656 0.1253 0.2877 -1.1465 1.1909 1.1892 %index of true(1) or false(0) is shown as solution >>ind = (x >= 1) | (x < -0.2) ind = 1 1 0 0 1 1 1 >>y = x(ind) y = -0.4326 -1.6656 -1.1465 1.1909 1.1892
Digital Image Processing • A set of algorithms for manipulating digital images • A preprocessing step necessary for image understanding • Techniques to transform an image into a meaningful signal DIP is an important component of many fields – Remote sensing/Photogrammetry – Medical Imaging – Robotics – Multimedia – Visualization
Matlab: Review • The trick behind Matlab is that everything is represented in the form of arrays or matrices. • Mathematical Operations starting from simple algebra to complex calculus may be conveniently carried out using this environment. • The main use of Matlab in Image Processing is for algorithm implementations and development. • Code developed in Matlab can be converted into C, C++ or Visual C++.
Three types of images: • Gray-scale images • 8 bit image I(x,y) [0..255] • Binary images • 0,1 binary I(x,y) {0 , 1} • Color images • 3 different grey scale images • RGB IR(x,y) IG(x,y) IB(x,y)
Grayscale Image x = 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 210 209 204 202 197 247 143 71 64 80 84 54 54 57 58 206 196 203 197 195 210 207 56 63 58 53 53 61 62 51 201 207 192 201 198 213 156 69 65 57 55 52 53 60 50 216 206 211 193 202 207 208 57 69 60 55 77 49 62 61 221 206 211 194 196 197 220 56 63 60 55 46 97 58 106 209 214 224 199 194 193 204 173 64 60 59 51 62 56 48 204 212 213 208 191 190 191 214 60 62 66 76 51 49 55 214 215 215 207 208 180 172 188 69 72 55 49 56 52 56 209 205 214 205 204 196 187 196 86 62 66 87 57 60 48 208 209 205 203 202 186 174 185 149 71 63 55 55 45 56 207 210 211 199 217 194 183 177 209 90 62 64 52 93 52 208 205 209 209 197 194 183 187 187 239 58 68 61 51 56 204 206 203 209 195 203 188 185 183 221 75 61 58 60 60 200 203 199 236 188 197 183 190 183 196 122 63 58 64 66 205 210 202 203 199 197 196 181 173 186 105 62 57 64 63 y =
Image Representation • An image I is a matrix of pixel values • • Pixel value at p = [x,y]T is I(p) • or I(x,y) • • Origin is top left corner – I is a matrix – p is a vector – x, y, I(p), I(x,y) are scalars Image may be displayed with imshow command.
Image Histogram Source Image Histogram of Image >>imhist(image) Image histogram (see above) is a chart that shows the distribution of intensities in an indexed or intensity image, i.e. the x-axis contains the grey values and the y-axis the count how many pixels in the image have this grey value
Image I/O • You can open an image as a matrix using imread command. • The matrix may simply be m x n form or it may be 3 dimensional array or it may be an indexed matrix, depending upon image type. • Changed image may then be saved with imwrite command.
Basic IP funtions %reading an image ic = imread('image_rgb.jpg'); %converting colour image to greyscale image imggr=rgb2gray(ic); %Resizing an image imgsmall = imresize(igr, 0.5, 'bicubic'); %Cropping an image interactively imgcropped = imcrop(igr); %convert to black-and-white (binary) image imgbw_auto=im2bw(imgcropped, graythresh(imgcropped)); %applying a predefined image filter sharp = fspecial('unsharp'); imgsharp =imfilter(igr,sharp,'replicate'); figure('name','Sharpened image'); imshow(imgsharp)