






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Finite Differences
Recall the definition of the derivative:
On an image, we can't let h go to zero. We are restricted to the pixel size, so the smallest h can be is 1 pixel.
The approximation of the derivate is called a finite difference.
Forward Difference: h=
fx = f(x+1,y) - f(x,y)
Backward Difference: h=-
fx = f(x,y) - f(x-1,y)
Center Difference: h=
fx = (f(x+1,y) - f(x-1,y)) / 2
h f fx h f^ x x h lim ( ) (^ ) 0 = +^ − →
Finite Differences
We have to be careful at the borders of the image. We generally assume Neumann boundary conditions (du/dn=0). This means the values at the border are repeated. Remember Matlab lists row then column, so the derivative in x is on the second subscript. Forward Difference m = length(f); f_x = f(2:m, m) - f(1:m); Backward Difference f_x = f(1:m) - f(1,1:m-1);
function [u] = heat_equation (u0, c)
m = length(u0); u0 = double(u0); subplot(121); plot(u0); title('Original');
dt = 0.2; T = 50; u = u0;
for t = 0:dt:T u_xx = u(2:m m) - 2u + u(1 1:m-1); u = u + dtc*u_xx; subplot(122); plot(u); title(['t=',num2str(t)]); drawnow; end;
1D Heat Equation Function
The 2D Heat Equation
Can you extend the heat equation to 2D?
So instead of a signal u, we will evolve a
matrix u.
The 2D heat equation is:
If c is constant (isotropic diffusion), we get
ut=c∆u
ut =∇⋅ ( c ∇ u )
Anisotropic Heat Equation
What if the conductivity c is not-constant
(anisotropic diffusion)?
Write a m file that handles the case when
c is a matrix.
For example, in the picture below we have
an insulating block (c=0) in the middle.
c= c=
Now on to images...
2D numerical analysis has a neat
connection to image processing.
Let's talk about image processing in
Matlab now.
(We'll see the heat equation come up
again at the end.)
Subplots
The subplot command divides the figure into windows. subplot(TotalNumRows, TotalNumCols, index) The index goes from left to right, top to bottom (raster order). For example, subplot(2,3,4) gets box#4 in a 2x3 figure grid.
If the numbers are all single digits, we can omit the commas: subplot(234)
2D Convolution
The discrete convolution is given by
The idea is that we have an image f and a smaller "mask" g.
g is generally assumed to be an odd square matrix (3x3, 5x5, etc)
To compute (f*g)(x), we center g over the pixel f(x), do pointwise multiplication, and add it up.
1 2 3 4 5 6 7 8 9
g=
10 45 19 17 99 22 f =^39 8
6 20 33 84 12 66 71 3 52 0 18 26 21 37 98
57 64 76 7 43 88
At f(x) = 42...
(fg)(x) = 199 + 222 + 384 + 48 + 542 + 671 + 718 + 826 + 9 = 1910
Linear Filters
An operation that can be written as a convolution is called a linear filter. The matlab function for 2D convolution is conv C = conv2(f,g); Note the borders of f are a special case. You can specify how to handle the borders in the input parameter. The parameter 'same' makes the result C have the same size as f. We can do multi-channel filtering with imfilter. C = imfilter(f,g);
Linear Filters
To preserve the mean gray value of the image, we usually assume the mask values sum to 1.
The command fspecial allows us to build some standard masks. subplot(131); subplot(132); subplot(133); imagesc(A); G = fspecial('gaussian',5,0.7); M = fspecial('motion',20,45); imagesc(imfilter(A,G)); imagesc(imfilter(A,M));
Original Gaussian Blurring Motion Blur
The Gradient
The norm of the gradient is often used for
edge detection.
The gradient should be small in smooth
regions.
The gradient should be large at edges.
Complicated textures can confound
gradient-based edge detection.
2 2 ∇ u = ux + u y
Logicals
Logicals
If we want to find the pixel positions where a boolean statement is true, use the find function. ind = find(A>100 & A<200);
ind will be a column vector of indices where the boolean statement was true. Note ind could be empty (size 0).
Note an index is a single number, not a subscript (row,column). Matlab reads matrices columnwise.
We can convert between index and subscript with ind2sub and sub2ind. Note you have to pass the matrix size as input.
You also get indices from the min, max, and sort functions.
1 4 7 2 5 8 3 6 9
10 11 12
Anisotropic Diffusion
Evolving the heat equation on an image is called isotropic diffusion. Isotropic diffusion is mathematically equivalent to Gaussian blurring. The problem is that the colors are allowed to flow in all directions equally, including across the edges. How can we stop the diffusion at the edges?
Anisotropic diffusion turns real images into "cartoons".
∇ ∇
= ∇⋅ u u
ut
1