Download presentation
Presentation is loading. Please wait.
Published byNorah Cooper Modified over 6 years ago
1
MA/CS 375 Fall 2002 Lecture 9 MA/CS375 Fall 2002
2
This Lecture Introduce the sparse matrix format
Finding edges in an image MA/CS375 Fall 2002
3
Sparse Matrices in Matlab
Quite often we need to use a matrix that has a large fraction of it’s entries being zero For instance I could need a 1000x1000 matrix which only has 3000 non-zero entries. If we stored it as a full matrix, then we would be wasting 1,000, entries.. MA/CS375 Fall 2002
4
Using spalloc in Matlab
We can create a sparse matrix in Matlab by calling spalloc We can S = spalloc(M,N,NZMAX) creates an: M-by-N all zero sparse matrix with room to eventually hold NZMAX nonzeros. MA/CS375 Fall 2002
5
Plot the sparsity pattern
Create a sparse matrix with 3 non-zeros Plot the sparsity pattern MA/CS375 Fall 2002
6
(2,4) (4,5) (60,75) MA/CS375 Fall 2002
7
Using spdiags To Create a Sparse Matrix With a Set of Diagonals
The following function first fills the three central diagonals with 1/3 Then the top and bottom rows are filled with 1/2 Volunteer to explain what happens when I premultiply a matrix with this 1/2 1/ 1/3 1/3 1/ 0 1/3 1/3 1/3 0 /3 1/3 1/3 /2 1/2 MA/CS375 Fall 2002
8
MA/CS375 Fall 2002
9
Explanation When we premultiply a matrix A by this tridiagonal matrix the resulting matrix has the following rows: Row 1) average of rows 1 and 2 of A Row 2) average of rows 1,2, and 3 of A Row 3) average of rows 2,3, and 4 of A .. Last row) average of last and penultimate rows of A The new matrix is a “smoothed” version of A MA/CS375 Fall 2002
10
Smoothing An Image imagesmooth.m
We load an image We build two smoothing matrices We premultiply the intensity matrix with the smoothing matrices Use imagesc to plot the image using the full palette color range MA/CS375 Fall 2002
11
Lines 8,9 ) calls our home made averaging matrices
Lines 17) calculate a grayscale intensity Line 20) average the row entries Line 21) average the column entries Line 24) plot the original and the averaged image in grayscale MA/CS375 Fall 2002
12
MA/CS375 Fall 2002
13
Close Up MA/CS375 Fall 2002
14
Taking The Gradient of The Image Intensity imagegradient.m
The intensity we have calculated can be thought of as a two dimensional function. As such we can take approximate derivatives in the horizontal and vertical derivatives. MA/CS375 Fall 2002
15
MA/CS375 Fall 2002
16
plotting the top 90 % of the image gradient range in white
We are: plotting the top 90 % of the image gradient range in white the bottom 10% of the image gradient range in black. MA/CS375 Fall 2002
17
This is a close up of the edges of the writing behind my head
MA/CS375 Fall 2002
18
Group Exercise Part 1 1) Find a family friendly image (tif, jpeg)
2) Modify rowaverage.m to use four rows instead of three to create a smoothing matrix 3) Modify imagesmooth.m to read in your image 4) Modify imagegradient.m to plot the top 95% of the image gradient range in white instead of the current 90% MA/CS375 Fall 2002
19
Group Exercise Part 2 1 -- dx
5) Use rowaverage.m as a base to create your own derivative routine myderivmatrix.m. It should return a square matrix whose entries resemble: but for an arbitrary number of rows. - ½ ½ 0 -½ ½ ½ ½ 0 ½ ½ 1 -- dx MA/CS375 Fall 2002
20
Group Exercise Part 3 6) Use the new matrix to differentiate a vector of function values of evaluated at 1000 points between -1 and 1 7) plot the error in the approximate derivative as a function of x Observations? MA/CS375 Fall 2002
21
Explanation of The Accuracy of The Derivative Matrix
We can use Taylor’s theorem to show us how accurate the approximate derivative operator we have constructed is. The first and last rows give the error we saw in Lecture 8. MA/CS375 Fall 2002
22
for some x1 in (x,x+delta) and some x2 in (x-delta,x)
1) We can expand f(x+delta) and f(x-delta) about x for some x1 in (x,x+delta) and some x2 in (x-delta,x) MA/CS375 Fall 2002
23
3) So the error term looks like
2) Now subtract the two and divide by delta: 3) So the error term looks like MA/CS375 Fall 2002
24
Summary of Error Analysis
In words – using the approximate derivative: yields an error which decrease quadratically with decreasing delta: MA/CS375 Fall 2002
25
Summary of Error Analysis Cont
In other words, just using a slightly different formula has improved the accuracy of the derivative at all points not at the end points. MA/CS375 Fall 2002
26
Summary We have begun to use sparse matrices
We have been able to smooth and differentiate images.. MA/CS375 Fall 2002
27
Next Class We will actually isolate individual features of an image and count them. MA/CS375 Fall 2002
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.