800 likes | 873 Views
This lecture covers spatial filtering principles in computer vision, including linear and nonlinear operations, correlation, convolution, and examples in MATLAB. Explore Gaussian distribution, edge detection with Sobel filter, Canny edge detection, and more.
E N D
Lecture 07 Roger S. Gaborski Introduction to Computer Vision Roger S. GaborskiRoger S. Gaborski Roger S. Gaborski 1
RECALL: Spatial Filtering • Neighborhood processing • Define center point (x, y) • Perform operations involving only pixels in the neighborhood • Result of operation is response to process at that point • Moving the pixel results in a new neighborhood • Repeat process for every point in the image Roger S. Gaborski
Linear and Nonlinear Spatial Filtering • Linear operation • Multiply each pixel in the neighborhood by the corresponding coefficient and sum the results to get the response for each point (x, y) • If neighborhood is m x n , then mn coefficients are required • Coefficients are arranged in a matrix, called • filter / filter mask / kernel / template • Mask sizes are typically odd numbers (3x3, 5x5, etc.) Roger S. Gaborski
Image origin y Kernel coefficients mask x Image region under mask Roger S. Gaborski
Correlation and Convolution • Correlation • Place mask w on the image array f as previously described • Convolution • First rotate mask w by 180 degrees • Place rotated mask on image as described previously • Convolution = 180 degree rotation + correlation Roger S. Gaborski
Example: 1D Correlation • Assume w and f are one dimensional • Origin of f is its left most point • Place w so that its right most point coincides with the origin of f • Pad f with 0s so that there are corresponding f points for each w point (also pad end with 0s) • Multiply corresponding points and sum • In this case (example on next page) result is 0 • Move w to the right one value, repeat process • Continue process for whole length of f Roger S. Gaborski
Reminder • ‘full’ is the result we obtain from the operations on the previous slide. If instead of aligning the left most element of f with the right most element of w we aligned the center element of w with the left most value of f we would obtain the ‘same’ result, same indicating the result is the same length of the original w Roger S. Gaborski
Chapter 3 www.prenhall.com/gonzalezwoodseddins Roger S. Gaborski
‘Full’ correlation Roger S. Gaborski
‘Same’ correlation etc. Roger S. Gaborski
Example - Convolution • Convolution is the same procedure, but the filter is first rotated 180 degrees. • Convolution = 180 degree rotation + correlation • If the filter is symmetric, correlation and convolution results are the same Roger S. Gaborski
Chapter 3 www.prenhall.com/gonzalezwoodseddins This can be simply extend to images Roger S. Gaborski
Linear Filtering in MATLAB g = imfilter(f, w, filtering mode, boundary, size) • filters the imput image f with the filter mask w. • f is input image. It can be of any class (logical/numeric) and dimension. • g is output image • filter mode: - 'corr' : correlation, and default mode - 'conv' : convolution Roger S. Gaborski
Parameters • g = imfilter(f, w, filtering mode, boundary, size) Boundary options - X pad boundary with value X. Default X = 0. - 'symmetric' symmetric padding - 'replicate' replicate padding - 'circular' circular padding Size options - 'same' g is the same size of f (default mode) - 'full' g is full filtered by w, so size of g is increased Roger S. Gaborski
MATLAB function for filtering: imfilter • g = imfilter(f, w, ‘replicate’) • Correlation is the default filtering mode. • If filters are pre-rotated 180 degrees, can simply use default(corr) for convolution • If filter is symmetric, doesn’t matter Roger S. Gaborski
Simple First Derivative Approximation Difference x = Smoothing Roger S. GaborskiRoger S. Gaborski
Rotate Filter Sensitive to edges at different orientations Roger S. GaborskiRoger S. Gaborski
Sobel Filter • Consider unequal weights for smoothing operation = Sy x = Roger S. GaborskiRoger S. Gaborski
Sobel Filter • Consider unequal weights for smoothing operation = Sx x = Roger S. GaborskiRoger S. Gaborski
build1.jpg Roger S. GaborskiRoger S. Gaborski
Edge image Roger S. GaborskiRoger S. Gaborski
~ Operator Roger S. GaborskiRoger S. Gaborski
Is there a better way to remove noise than the simple Sobel approach? Roger S. GaborskiRoger S. Gaborski
Canny • Image is smoothed with 2D Gaussian Function • Canny uses two thresholds • One threshold detects strong edges • Second threshold detects weak edges connected to strong edges Roger S. GaborskiRoger S. Gaborski
RECALL: 2D Gaussian Distribution • The two-dimensional Gaussian distribution is defined by: • From this distribution, can generate smoothing masks whose width depends upon the standard deviation, s: Roger S. GaborskiRoger S. Gaborski
Sigma Determines Spread of Filter Variance, s2 = .25 Variance, s2 = 4.0 Roger S. GaborskiRoger S. Gaborski
i2 + j2 i2 + j2 W(i,j) = exp (- ) W(i,j) = k * exp (- ) 2 s2 2 s2 k Creating Gaussian Kernels • The mask weights are evaluated from the Gaussian distribution: • This can be rewritten as: Roger S. GaborskiRoger S. Gaborski
j -3 -2 -1 0 1 2 3 -3 -2 -1 i 0 1 2 3 Example 2 • Choose s = 2. and n = 7, then: Roger S. GaborskiRoger S. Gaborski
7x7 Gaussian Filter Roger S. GaborskiRoger S. Gaborski
Building1 gray level image Roger S. GaborskiRoger S. Gaborski
31x31Gaussian, sigma = 3 Roger S. GaborskiRoger S. Gaborski
63x63 Gaussian, sigma = 10 Roger S. GaborskiRoger S. Gaborski
Canny Edge Detector Roger S. GaborskiRoger S. Gaborski
Implement Canny usingMATLAB edge Function EDGE Find edges in intensity image. EDGE takes an intensity or a binary image I as its input, and returns a binary image BW of the same size as I, with 1's where the function finds edges in I and 0's elsewhere. EDGE supports six different edge-finding methods: The Sobel method finds edges using the Sobel approximation to the derivative. The Prewitt method finds edges using the Prewitt approximation to the derivative. The Roberts method finds edges using the Roberts approximation to the derivative. The Laplacian of Gaussian method finds edges by looking for zero crossings after filtering I with a Laplacian of Gaussian filter. The zero-cross method finds edges by looking for zero crossings after filtering I with a filter you specify. The Canny method finds edges by looking for local maxima of the gradient of I. The gradient is calculated using the derivative of a Gaussian filter. The method uses two thresholds, to detect strong and weak edges, and includes the weak edges in the output only if they are connected to strong edges. This method is therefore less likely than the others to be "fooled" by noise, and more likely to detect true weak edges. Roger S. GaborskiRoger S. Gaborski
[g,t]=edge(im,'canny',[ ],.5) [ ] : Edge function determines the two thresholds CHANGING SIGMA OF GAUSSIAN, in this example = .5 Roger S. GaborskiRoger S. Gaborski
[g,t]=edge(im,'canny',[ ],1) CHANGING SIGMA OF GAUSSIAN Roger S. GaborskiRoger S. Gaborski
[g,t]=edge(im,'canny',[ ],2) CHANGING SIGMA OF GAUSSIAN Roger S. GaborskiRoger S. Gaborski
[g,t]=edge(im,'canny',[ ],3) CHANGING SIGMA OF GAUSSIAN Roger S. GaborskiRoger S. Gaborski
[g,t]=edge(im,'canny',[ ],5) CHANGING SIGMA OF GAUSSIAN Roger S. GaborskiRoger S. Gaborski
Second Derivative Edge Detection Methods • Second derivative is noisy • First smooth the image, then apply second derivative • Consider • Effect of smoothing filter. • Gaussian: Roger S. GaborskiRoger S. Gaborski
RECALL: Second Derivative Approximation • Discrete version of 2nd Partial Derivation of f(x,y) in x direction is found by taking the difference of Eq1 and Eq2: • 2 f(x,y) / x2 = Eq1-Eq2 = 2f(x,y)-f(x-1,y)-f(x+1,y) In y direction: • 2 f(x,y) / y2 = Eq3-Eq4 = 2f(x,y)-f(x,y-1)-f(x,y+1) Roger S. GaborskiRoger S. Gaborski
Derivative Approximation for Laplacian Roger S. GaborskiRoger S. Gaborski
Laplacian • Independent of edge orientation • Combine 2 f(x,y)/ x2 and 2 f(x,y)/ y2 =4 f(x,y) - f(x-1,y) – f(x+1,y) – f(x,y-1) – f(x,y+1) Roger S. GaborskiRoger S. Gaborski
Laplacian of the Gaussian is a circularly symmetric operator. Second derivative is linear operation Therefore: convolving an image with 2 G(x,y) is the same as first convolving first with smoothing filter (Gaussian) then computing Laplacian of result. Edge are location of zero crossings LoG Roger S. GaborskiRoger S. Gaborski
LoG Also called the Mexican hat operator. Roger S. GaborskiRoger S. Gaborski
s2 Controls of the Size of the Filter s2 = 0.5 s2 = 2.0 Roger S. GaborskiRoger S. Gaborski
Human Visual System Receptive Field Approximation 17 x 17 5x5 Roger S. GaborskiRoger S. Gaborski
LoG (7x7, sigma = 2) Roger S. GaborskiRoger S. Gaborski