740 likes | 878 Views
Introduction to Computer Vision. Lecture 3 Dr. Roger S. Gaborski. REMINDER: As stated on webpage: Quiz Thursday MATLAB Vectors and Matrices in MATLAB Scripts and User Defined Functions MATLAB Built-in Functions Plotting Data Classes Basic image operations. A Few Matlab Details.
E N D
Introduction to Computer Vision Lecture 3 Dr. Roger S. Gaborski
REMINDER: As stated on webpage: Quiz Thursday • MATLAB • Vectors and Matrices in MATLAB • Scripts and User Defined Functions • MATLAB Built-in Functions • Plotting • Data Classes • Basic image operations RS Gaborski
A Few Matlab Details • Scalar: Single number • Vector: Ordered list of numbers • May be a column or a row • Matrix: Rectangle array of number • Rows and Columns • Multidimensional Rows, Columns and Planes Example: Image(R.C,3) three planes, each R x C RS Gaborski
Matrix Operations • True Matrix Multiply =1*5+2*7 = 19 =1*6+2*8 = 22 Etc = * RS Gaborski
Matrix Operations • Point by Point Matrix Multiply =1*5 = 5 =2 * 6 = 12 Etc. .* = RS Gaborski
Another Example f * g vs f .* g >> f=[1 2;3 4]; g=[1 2; 2 1]; >> f .* g ans = 1 4 6 4 >> f=[1 2;3 4]; g=[1 2; 2 1]; >> f * g ans = 5 4 11 10 Element by Element True Matrix Multiply RS Gaborski
Calculating Square Root of Each Member of a Matrix Given the matrix C, calculate the square root of each member of the matrix (with LOOPS) >> [rows columns] = size(C); >> for r = 1:rows for c = 1:columns SqC(r,c) = sqrt(C(r,c)); end end >> SqC SqC = 1.0000 2.0000 1.4142 2.2361 1.7321 2.4495 2.0000 2.0000 C = 1 4 2 5 3 6 4 4 RS Gaborski
Calculating Square Root of Each Member of a Matrix >> SqC = sqrt(C) (without loops) SqC = 1.0000 2.0000 1.4142 2.2361 1.7321 2.4495 2.0000 2.0000 RS Gaborski
MATLAB Built-in Functions Rounds towards nearest integer, either up or down Rounds towards minus infinity Rounds towards positive infinity >> round(7.8) ans = 8 >> round (7.2) ans = 7 >> floor(7.8) ans = 7 >> ceil(7.8) ans = 8 >> sqrt(64) ans = 8 RS Gaborski
What if I want 10 evenly spaced numbers between 1 and 105? • >> s = linspace(1, 105, 10) • s = • Columns 1 through 10 • 1.0000 12.5556 24.1111 35.6667 47.2222 58.7778 70.3333 81.8889 93.4444 105.0000 RS Gaborski
>> help linspace • LINSPACE Linearly spaced vector. • LINSPACE(X1, X2) generates a row vector of 100 linearly • equally spaced points between X1 and X2. • LINSPACE(X1, X2, N) generates N points between X1 and X2. • For N < 2, LINSPACE returns X2. • Class support for inputs X1,X2: • float: double, single • See also logspace, :. RS Gaborski
Sum >> FirstColumn = [1;2;3]; >> SecondColumn = [4;5;6]; >> Sfc = sum(FirstColumn) %Column Vector Sfc = 6 >> Sfc = sum(FirstColumn') %Converted to a row vector Sfc = 6 RS Gaborski
Sum, continued >> C (Defined on previous slide) C = 1 4 2 5 3 6 4 4 >> sumC = sum(C) sumC = 10 19 BUT: We want the sum of the matrix, not of the columns RS Gaborski
Sum, continued >> sumC = sum(sum(C)) sumC = 29 >> CC = C(:)(convert to column) CC = 1 2 3 4 4 5 6 4 >> sumC = sum(C(:)) sumC = 29 RS Gaborski
MATLAB: Plotting RS Gaborski
Sine Curve >> input_points = [-pi : pi/8 : pi] input_points = Columns 1 through 10 -3.1416 -2.7489 -2.3562 -1.9635 -1.5708 -1.1781 -0.7854 -0.3927 0 0.3927 Columns 11 through 17 0.7854 1.1781 1.5708 1.9635 2.3562 2.7489 3.1416 >> sine_curve = 3*sin(input_points); >> plot(input_points, sine_curve) RS Gaborski
Output of plot RS Gaborski
Plotting • s = [1:360]; %integers 1 to 360 • >> plot(s,sind(s)) %sind takes an argument in degrees RS Gaborski
>> s = [1:360]; %integers 1 to 360 >> plot(s,sind(s),'r',s, cosd(s)) >> grid on >> title('Sine and Cosine Curves') >> xlabel('Angle (in degrees)') >> ylabel('Trig Function Value') >> legend('Sine','Cosine') Plots RS Gaborski
Simple 3D Plotsfrom Mathworks Tutorial >> X = [10 20 30 40]; >> Y = [10 20 30 40]; >> Z = [0 210 70 500]; >> plot3(X,Y,Z); grid on; >> xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis'); RS Gaborski
3D Plot RS Gaborski
meshgrid >> [x,y] = meshgrid(-2:1:2); >> x x = -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 >> y y = -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 RS Gaborski
3D Plots • >> [x,y] = meshgrid(-8:.5:8); • >> r = sqrt(x.^2+y.^2)+eps; • >> z = sin(r)./r; • >> mesh(z); RS Gaborski
surf(z) RS Gaborski
shading flat RS Gaborski
IMAGES:Sampling and Quantization • In the real world images are represented by continuous functions • Images are continuous in both in their spatial coordinates and amplitude • To create digital images must convert both the coordinates and the amplitude • Sampling: digitizing the coordinate values • Quantization: digitizing amplitude values RS Gaborski
Grayscale and Color Images • Grayscale refers to monochrome images • Color images are formed from combination of individual 2 D images • Common color images consists of three planes of data: red, green and blue • RGB is referred to as a color space • Other color spaces are also used, for example HSV (Hue, Saturation and Value) RS Gaborski
Digital Image • Has a finite number of spatial coordinates • 1200 x 1600 rows x columns • Rows M • Columns N • Image size is M x N • Has a finite number of amplitude levels • Typically 8 bits • Medical images: 12 bits • A digital camera image may have 1200 rows and 1600 columns and 256 ( 8 bits) levels of intensity for each color • 1200x1600x8x3 = 46,080,000 bits = 5,760,000 bytes RS Gaborski
MATLAB Matrix Representation • Image size is M x N • Origin is either (0,0) or (1,1) • MATLAB uses (1,1) as the original and refers to a spatial coordinate as (r,c) • Image has a range of: r=1to M and c=1 to N f(1,1) f(1,2) … f(1,N) f(2,1) f(2,2) … f(2,N) . . . f(M,1) f(M,2) … f(M,N) f = RS Gaborski
Reading Images in MATLAB >> help imread IMREAD Read image from graphics file. A = IMREAD(FILENAME,FMT) reads a grayscale or color image from the file specified by the string FILENAME, where the string FMT specifies the format of the file. See the reference page, or the output of the function IMFORMATS, for a list of supported formats. If the file is not in the current directory or in a directory in the MATLAB path, specify the full pathname of the location on your system. If IMREAD cannot find a file named FILENAME, it looks for a file named FILENAME.FMT. IMREAD returns the image data in the array A. If the file contains a grayscale image, A is a two-dimensional (M-by-N) array. If the file contains a color image, A is a three-dimensional (M-by-N-by-3) array. The class of the returned array depends on the data type used by the file format. RS Gaborski
Reading Images in MATLAB • imread(‘filename’) • At Command line: >> f = imread(‘chestxray.jpg’); The semicolon suppresses the output. Without the semicolon the output is displayed Without path information imread reads the image from the current directory, if not found, then searches the MATLAB search path Include path: >> f = imread(‘D:\images\chestxray.jpg’); RS Gaborski
Chapter 2 – Fundamentals www.prenhall.com/gonzalezwoodseddins JPEG images RS Gaborski
Digital Image >> Size(f) ans = 1600 1200 3 Digital image: 1600 rows x 1200 columns x 3 planes Planes: red, green and blue (RGB) impixel() Pixel values: 1: 217 184 29 2: 108 93 90 3: 122 73 68 3 1 2 RS Gaborski
who >> whos A Name Size Bytes Class A 1600x1200x3 5760000 uint8 array Grand total is 5760000 elements using 5760000 bytes uint8 is a data class RS Gaborski
Displaying Images • imshow( f ) • f is image array • imshow( f, [ low high ] ) • Displays as black values less than low • Displays as white all values equal or greater than high • imshow( f, [ ] ) • Sets variable low to lowest value in f • Sets variable high to highest value in f RS Gaborski
Intensity Image Display Demo %imshowDemo %Create intensity image - values between 0 and 1 %f= (rand([30 30])/5); %for low contrast image f= (rand([30 30])); %Display image figure, subplot(1,3,1), imshow(f), title('imshow(f)') subplot(1,3,2), imshow(f, [.25 .75]), title('imshow(f, [.25 .75]') subplot(1,3,3), imshow(f, [ ] ), title('imshow(f, [ ] )') RS Gaborski
Results - 1 RS Gaborski
Results - 2 Low contrast image RS Gaborski
Chapter 2 – Fundamentals www.prenhall.com/gonzalezwoodseddins RS Gaborski
IMPORTANT POINT • In the previous slides the data values for each image didn’t change, but how they were displayed did change. RS Gaborski
Writing Images to Disk- Syntax • Syntax: • imwrite( f, ‘filename’ ) string filename must contain file format extension imwrite( f, ‘xray1.tif’) OR, third input argument can specify format • imwrite( f, ‘xray1’, ‘tif’) RS Gaborski
Writing Images to Disk- Parameters • imwrite(f, ‘filename.jpg’, ‘quality’, q) • jpg (jpeg) are compressed images • There is a trade off between file size (degree of compression) and image quality • Highly compressed images have small file sizes, but poorer image quality • q is an integer between 0 and 100 • Smaller numbers result in more compression RS Gaborski
Data Classes • Eight numeric data classes • One character class • One logical class • All numeric operations in MATLAB are done using double quantities • Class uint8 is common in reading data from devices RS Gaborski
Chapter 2 – Fundamentals www.prenhall.com/gonzalezwoodseddins RS Gaborski
help class help class CLASS Return class name of object. S = CLASS(OBJ) returns the name of the class of object OBJ. Possibilities are: double -- Double precision floating point number array (this is the traditional MATLAB matrix or array) single -- Single precision floating point number array logical -- Logical array char -- Character array cell -- Cell array struct -- Structure array function_handle -- Function Handle int8 -- 8-bit signed integer array uint8 -- 8-bit unsigned integer array int16 -- 16-bit signed integer array uint16 -- 16-bit unsigned integer array int32 -- 32-bit signed integer array uint32 -- 32-bit unsigned integer array int64 -- 64-bit signed integer array uint64 -- 64-bit unsigned integer array RS Gaborski
>> f = imread('Flags.jpg'); >> J = class(f) >> J = uint8 >> f= (rand([30 30])/5); %for low contrast image >> J =class(f) J = double RS Gaborski
Converting Between Data Classes • B = data_class_name(A) where data_class_name is from first column of Table 2.2 • If A is an array of class uint8. Can generate a double precision array B by command: B = double(A); RS Gaborski
Example >> A = imread(''image20.JPG''); >> whos A Name Size Bytes Class Attributes A 640x480x3 921600 uint8 640 rows, 480 columns and three planes of data Data class uint8 RS Gaborski