Presentation on theme: "Lecture 4 Linear Filters and Convolution"— Presentation transcript:
1 Lecture 4 Linear Filters and Convolution Slides by:David A. ForsythClark F. OlsonSteven M. SeitzLinda G. Shapiro
2 Image noiseIn finding the interesting features (such as edges) in an image, the biggest problem is noise.Noise is:Sensor error in acquiring the imageAnything other than what you are looking forNoise is often caused by underexposure (low light, high film speed)Noisy image from Wikipedia page on image noise
3 Noise Common types of noise: Salt and pepper noise - contains random occurrences of black and white pixelsImpulse noise - contains random occurrences of white pixelsGaussian noise - variations in intensity drawn from a Gaussian (normal) distribution
4 Image noise “Simple” noise model: Independent stationary additive Gaussian noiseThe noise value at each pixel is given by an independent draw from the same normal (i.e., Gaussian) probability distributionThe scale (σ) determines how large the effect of the noise is.Result image “Perfect” image additive noisewhere z is a random number between 0 and 1.
5 Image noise Issues: Advantages: This model allows noise values that could be greater than maximum camera output or less than zero.For small standard deviations, this isn’t too much of a problem - it’s a fairly good model.Independence may not be justified (e.g., damage to lens).Noise may not be stationary (e.g., thermal gradients in the ccd).Advantages:Fairly accurateRelatively easy to determine response of filters to such noise
6 Linear filtersWe use linear filtering to reduce the effect of noise (among other things).General process:Form new image, where pixels are a weighted sum of nearby pixel values in original image, using the same set of weights at each pointProperties:Output is a linear function of the inputOutput is a shift-invariant function of the input (i.e. shift the input image two pixels to the left, the output is shifted two pixels to the left)
7 Linear filteringFiltering operations use a “kernel” or “mask” composed of weights to determine how to compute the weighted average in a neighborhood.Usually, the mask is centered on the pixel and the weights are applied by multiplying by the corresponding pixel in the image and summing.** ** ** ** **** 39 ** ** **1/9 1/9 1/93x3 MaskInput ImageOutput Image
8 Mean filteringA mean filter (as on the previous slide) averages the pixels in some neighborhood (such as the 3x3 box surrounding the pixel).For this neighborhood, every pixel in the output (except for the borders) is defined as:
9 Kernel The kernel is a 2D array or matrix or image. The kernel has an origin that represents the location that is multiplied by the pixel at the location of the output pixel.Usually at the center of the kernel, but not necessarilyKernel for mean filtering in a 3x3 neighborhood (center is bold):For smoothing or averaging, the kernel coefficients always add up to one.Larger (sometimes much larger) kernels are common.1/9 1/9 1/9
10 Image boundariesAt the image boundary, we can’t use the same process, since part of the kernel will be outside of the input image.Some methods for handling the boundary:Shrink the output image (ignore the boundaries)Consider every pixel outside of the input to be:Black (zero)The same as the nearest pixel inside the imageExtends the borders infinitelyA mirror image of the pixels inside the imageLess likely to appear as edge at boundary, but second order effects occur (second derivative may appear large)
12 Mean filteringAs the size of the kernel is increased, the noise is more smoothed, but so is the rest of the image.
13 Linear filtering Some examples of linear filtering Smoothing by averaging (mean filtering)Form the average of pixels in a neighborhoodSmoothing with a GaussianForm a weighted average of pixels in a neighborhoodFinding a derivative (approximation)
14 ConvolutionLinear filtering can be performed using a process called discrete convolution.Represent the pixel weights as an image, KK is usually called the kernel in convolutionOperation is associative (if defined correctly)Continuous convolution is common in signal processing (and other fields), but, since images are not continuous, we will use only discrete convolution
15 ConvolutionAlgorithmically, convolution corresponds to four nested loops (two over the image, two over the kernel).For each image row in output image:For each image column in output image:Set running total to zero.For each kernel row:For each kernel column:Multiply kernel value by appropriate image valueAdd result to running totalSet output image pixel to value of running total
16 Convolution Mathematically: Odd definition preserves associativity and commutativity.Subtracting u and v from the image indices implies that the kernel is flipped before applying it to the image.All linear operations can be written as a convolution with some kernel.Variables u and v range over the size of the kernel.Note that the kernel origin (0,0) is usually at the center of the kernel (but does not need to be).
17 Convolution The “center” of the kernel is at the origin. For our “mean filter” kernel, we have:-1 ≤ u ≤ 1-1 ≤ v ≤ 1Again, note the change in the sign of u and v – this is flipping the image (or, equivalently, the kernel).K(0, 0)K(-1, -1)Kernel K1/9 1/9 1/9vK(1, 0)u
18 Convolution Convolution is written in shorthand as O = K * I. The “flipping” preserves commutativity:K * I = I * Kand associativity:J * (K * I) = (J * K) * I,but only if the borders are handled correctly.Must expand the output, treating values outside the input image as zero.
19 Cross-correlationCross-correlation is the same as convolution, except that you don’t flip the kernel.How does this differ from convolution for:Mean filtering?Gaussian filtering?
20 Example: smoothing by averaging KernelInputOutputHere is the point to introduce some visual “notation”. I’ve given the kernel as an image on the top.Usually, these images are white for the largest value, and black for the smallest (which is zero inthis case). The point of this pair of images is that if you convolve the one on the left with the kernelshown above you get the one on the right, which is nothing like a smoothed version of the one on the left(there’s some fairly aggressive ringing). Explain the ringing qualitatively in terms of the boundaries ofthe kernel, and point out that the effect would disappear if there weren’t sharp edges in the kernel.20
21 Smoothing with a Gaussian Smoothing with an average actually doesn’t compare at all well with a defocused lens (e.g., in an eye).A defocused lens smoothes an image symmetrically, which is what we want.Most obvious difference is that a single point of light viewed in a defocused lens looks like a fuzzy blob, but the averaging process would give a little square.We want smoothing to be the same in all directions.I always walk through the argument on the left rather carefully; it gives some insight into thesignificance of impulse responses or point spread functions.A Gaussian gives a good model of a fuzzy blob
22 An isotropic Gaussian Plot of: The constant is necessary so that the function integrates to 1.Plot of:The image shows a smoothing kernel proportional to a Gaussian (a circularly symmetric fuzzy blob)Sigma (σ) is often referred to as the scale of the Gaussian
23 Gaussian smoothingIn practice, we must discretize the (continuous) Gaussian function:We could generate the following 3x3 kernel with σ=1:(Normally, we would use a larger kernel.)h(-1, -1) h(-1, 0) h(-1, 1)h(0, -1) h(0, 0) h(0, 1)h(1, -1) h(1, 0) h(1, 1)=
24 Gaussian smoothingUnfortunately, the sum of the values for the kernel on the previous slide is onlyWe need to normalize the kernel by dividing each value byThe sum is now 1.
25 Smoothing with a Gaussian You want to point out the absence of ringing effects here.25
27 Differentiation Recall that: This is linear and shift invariant, so it must be the result of a convolution.
28 Differentiation and convolution We can approximate this as:This is called a “finite difference.”It is definitely a convolution – what is the kernel?Often called the gradient when applied to an image.This finite difference (gradient) measures horizontal change.By itself, it’s not a very good way to do things, since it is very sensitive to noise.I tend not to prove “Now this is linear and shift invariant, so must be the result of a convolution” butleave it for people to look up in the chapter.
29 Gradient kernelsTo determine the horizontal image gradient, we could use one of the following kernels:The first has better “localization,” but shifts the image by half of a pixel.For vertical image gradients, we use one of:-1 1-11-11
30 Finite differences (horizontal) Large (bright) values for light/dark transitionsKernel:1 -1Negative (dark) values for dark/light transitionsDetects only horizontal changes.Small (grey) values for non-transitionsNow the figure on the right is signed; darker is negative, lighter is positive, and mid grey is zero.I always ask 1) which derivative (y or x) is this? 2) Have I got the sign of the kernel right? (i.e.is it d/dx or -d/dx).
31 Finite differencesFinite difference filters respond strongly to noise.Image noise results in pixels that look very different from their neighborsGenerally, the larger the noise, the stronger the response.
32 Finite differences responding to noise Low noise Medium noise High noise
33 Finite differences and noise What is to be done?Intuitively, most pixels in images look quite a lot like their neighbors.This is somewhat true even at an edge; along the edge they’re similar, across the edge they’re not.This suggests that smoothing the image should help, by forcing pixels different to their neighbors (noise pixels?) to look more like neighbors.
34 Filter responses are correlated The filter responses are correlated over scales similar to the scale of the filter.Filtered noise is sometimes useful.It looks like some natural textures, can be used to simulate fire, etc.
35 Filtered noiseIndependent stationary Gaussian noise convolved with a Gaussian kernel.The scores are correlated over the same scale as the kernel.
36 Filtered noiseIndependent stationary Gaussian noise convolved with a Gaussian kernel.The scores are correlated over the same scale as the kernel.
37 Filtered noiseIndependent stationary Gaussian noise convolved with a Gaussian kernel.The scores are correlated over the same scale as the kernel.
38 Median filteringA median filter takes the median value in the neighborhood of a pixel, rather than a weighted average.Is this a convolution?Advantage: It doesn’t smooth over region boundaries.Noise added to the images is Gaussian.
39 Median filteringMedian filtering works best with salt and pepper noise.