EN161 Image Understanding Matlab lab section Sep 16, 2004 Ming-Ching Chang mcchang@lems.brown.edu ======================================================= Matlab 6 introduction - Matrix Laboratory . environment for scientific computation . workspace: commandline, editor, figures . plotform independent - a tool for mathematical computation . process matrices, variables, strings - basic array operations - multi-dementional arrays . solve algebra, polynomial, equations . do optimization, differentiation/intergration, solve differential equations - a tool for visualization/GUI (Graphic User Interface) . 2D/3D plotting . showing images, grids . figures, subplots, axis labels . contour plots, surface plots, volumn visualization . videos, multimedia/web support . GUI: mouse interactivity/controls, dialog boxes - a programming language . variables, strings . logical operations . flow control - if, for loop, while loop . function *.M files - a programming environment . command line interpreter . *.m program editor . debugger, variable watcher . profiler . C, FORTRAN, JAVA ======================================================= Useful Matlab commands/functions help help imread (help ANY_FUNCTION) help complex demo (good starting demonstration) who (see current variables) whos (with detailed info) A=1 (variable name '3A' is not allowed.) % comment pi 3.1416 format long pi 3.14159265358979 ver path cd.. (or select the 'Current Directory...' control) pwd sin, cos, tan, exp, log, log2, sqrt, floor, ceil, round, erf, gamma, dot, cross imread (image I/O, try 'help imread') imwrite imshow BASIC OPERATIONS: 54.4 + 23*6 input A pause ARRAY OPERATIONS: A = [1 2 3;4 5 6;7 8 9] ======================================================= Matlab matrix manupulation: Learn to use ':' syntex to speedup computation (avoid loops) K>> A=[1 2 3; 4 5 6] A = 1 2 3 4 5 6 K>> size(A) ans = 2 3 K>> A(2:end) (column than row) ans = 4 2 5 3 6 K>> A(1:2,2:3) (part of an array) ans = 2 3 5 6 K>> A(:,2)=7 A = 1 7 3 4 7 6 K>> A(:,:)=8 A = 8 8 8 8 8 8 K>> A=7 (re-assign A) A = 7 K>> B=(0:0.1:1)*pi B = Columns 1 through 8 0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850 2.1991 Columns 9 through 11 2.5133 2.8274 3.1416 ARRAY TRANSPOSE C=1:5 C' INITIALIZE AN ARRAY K>> ones(3,4) ans = 1 1 1 1 1 1 1 1 1 1 1 1 K>> zeros(2,3) ans = 0 0 0 0 0 0 K>> d=pi d = 3.1416 K>> D=repmat(d,3,4) (Replicate matrix, 3x4) ans = 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 K>> E=reshape(D, 4, 3) (Reshape to 4x3) E = 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 K>> E(2:3,1:3)=[1 2 3; 4 5 6] (process sub-image of an image) E = 3.1416 3.1416 3.1416 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 3.1416 3.1416 3.1416 LOGICAL ARRAY K>> B=[5 -3; 2 -4] B = 5 -3 2 -4 K>> x=abs(B)>2 x = 1 1 0 1 K>> K>> y=-3:3 y = -3 -2 -1 0 1 2 3 K>> k=find(abs(y)>1) k = 1 2 6 7 K>> max(y) ans = 3 K>> [my, index]=max(y) (return 2 values of a function call) my = 3 index = 7 K>> ======================================================= Image Manipulation: - read image from file (JPG, GIF, BMP, TIFF, PNG, PCX...) - write image to file - show image on screen, zoom in/out . get pixel value - process image as an array of pixels . UINT8: unsigned 8-bit (0 to 255) % what happen if we subtract the image? . DOUBLE: double-precision (0 to 1) . convert between the 2 format: im2double, im2uint8 'whos' to check the image format EXAMPLE CODE OF PROBLEM1: THE SLOW VERSION: IMAGE = imread ('quart.jpg'); INFO = imfinfo ('quart.jpg'); height = INFO.Height; width = INFO.Width; threshold = 150; for i=1:height for j=1:width if IMAGE(i,j) > threshold IMAGE(i,j) = 0; else IMAGE(i,j) = 255; end end end imshow (IMAGE); imwrite (IMAGE, 'problem1.jpg'); THE FAST VERSION (WITHOUT LOOP): IMAGE = imread ('quart.jpg'); IMAGE = im2double(IMAGE); IMAGE(:,:) = (IMAGE(:,:)>150/255); imshow (IMAGE); THE FAST AND NEAT VERSION: threshold = 182 QUARTERS = imread('quart.jpg'); PROBLEM1 = QUARTERS> invert_image(1,2) ans = 1e 1*16+14=30 >> invert_image(PROBLEM3(1,2),255) ans = e1 >>