CIS 601 Image ENHANCEMENT in the SPATIAL DOMAIN Dr. Rolf Lakaemper
Most of these slides base on the book Digital Image Processing by Gonzales/Woods
Spatial Filtering
Spatial Filtering: Operation on the set of ‘neighborhoods’ N(x,y) of each pixel (Operator: sum)
Spatial Filtering Neighborhood of a pixel p at position x,y is a set N(p) of pixels defined relative to p. Example 1: N(p) = {(x,y): |x-x P |=1, |y-y P | = 1} P Q
Spatial Filtering More examples of neighborhoods: PPPP PP
Spatial Filtering Usually neighborhoods are used which are close to discs, since properties of the eucledian metric are often useful. The most prominent neighborhoods are the 4-Neighborhood and the 8-Neighborhood PP
Spatial Filtering We will define spatial filters on the 8-Neighborhood and their bigger relatives P P P N8 N24 N48
Spatial Filtering Index system for N8: n3n2n1 n6n5n4 n9n8n7
Spatial Filtering Motivation: what happens to P if we apply the following formula: P = i n i n3n2n1 n6n5=Pn4 n9n8n7
Spatial Filtering What happens to P if we apply this formula: P = i a i n i with a i given by: a3=1a2=1a1=1 a6=1a5=4a4=1 a9=1a8=1a7=1
Spatial Filtering Linear Image Filters Linear operations calculate the resulting value in the output image pixel f(i,j) as a linear combination of brightness in a local neighborhood of the pixel h(i,j) in the input image. This equation is called discrete convolution: Function w is called a convolution kernel or a filter mask. In our case it is a rectangle of size (2a+1)x(2b+1).
Spatial Filtering
Exercise: Compute the 2-D linear convolution of signal X with mask w. Extend the signal X with 0’s if needed (padding).
Spatial Filtering Lets have a look at different values of a i and their effects ! This MATLAB program creates an interesting output: % Description: given an image 'im', % create 12 filtered versions using % randomly designed filters for i=1:12 a=rand(7,7); % create a % random 7x7 % filter-matrix a=a*2 - 1; % range: -1 to 1 a=a/sum(a(:)); % normalize im1=conv2(im,a);% Filter subplot(4,3,i); imshow(im1/max(im1(:))); end
Spatial Filtering
Different effects of the previous slides included: Blurring / Smoothing Sharpening Edge Detection All these effects can be achieved using different coefficients.
Spatial Filtering Blurring / Smoothing (Sometimes also referred to as averaging or lowpass-filtering) Average the values of the center pixel and its neighbors: Purpose: Reduction of ‘irrelevant’ details Noise reduction Reduction of ‘false contours’ (e.g. produced by zooming)
Spatial Filtering Blurring / Smoothing * 1/9 Apply this scheme to every single pixel !
Spatial Filtering Example 2: Weighted average * 1/16
Spatial Filtering Basic idea: Weigh the center point the highest, decrease weight by distance to center. The general formula for weighted average: P = i a i n i / i a i Constant value, depending on mask, not on image !
Spatial Filtering Blurring using different radii (=size of neighborhood)
Spatial Filtering EDGE DETECTION
Spatial Filtering EDGE DETECTION Purpose: Preprocessing Sharpening
Spatial Filtering What are edges in an image? " Edges are those places in an image that correspond to object boundaries. " Edges are pixels where image brightness changes abruptly. Brightness vs. Spatial Coordinates
Spatial Filtering Motivation: Derivatives
Spatial Filtering First and second order derivative
Spatial Filtering First and second order derivative 1. 1 st order generally produces thicker edges 2. 2 nd order shows stronger response to detail 3. 1 st order generally response stronger to gray level step 4. 2 nd order produce double (pos/neg) response at step change
Spatial Filtering Definition of 1 dimensional discrete 1 st order derivative: dF/dX = f(x+1) – f(x) The 2 nd order derivative is the derivative of the 1 st order derivative…
Spatial Filtering The 2 nd order derivative is the derivative of the 1 st order derivative… F(x-1)F(x)F(x+1) F(x)-F(x-1)F(x+1)-F(x)F(x+2)-F(x+1) derive F(x+1)-F(x) – (F(x)-F(x-1)) F(x+1)-F(x) – (F(x)-F(x-1))= F(x+1) -2F(x)+F(x-1)
Spatial Filtering F(x+1)-F(x) – (F(x)-F(x-1))= F(x+1) -2F(x) +F(x-1) 1-21 One dimensional 2 nd derivative in x direction: One dimensional 2 nd derivative, direction y:
Spatial Filtering 1-21 TWO dimensional 2 nd derivative (derivatives are linear !): = This mask is called the ‘LAPLACIAN’ (remember calculus ?)
Spatial Filtering Different variants of the Laplacian
Spatial Filtering Effect of the Laplacian… (MATLAB) im = im/max(im(:)); % create laplacian L=[1 1 1;1 -8 1; 1 1 1]; % Filter ! im1=conv2(im,L); % normalize and show im1=(im1-min(im1(:))) / (max(im1(:))-min(im1(:))); imshow(im1);
Spatial Filtering
A Quick Note " Matlab’s image processing toolbox provides edge function to find edges in an image: I = imread('rice.tif'); BW1 = edge(I,'prewitt'); BW2 = edge(I,'canny'); imshow(BW1) figure, imshow(BW2) " Edge function supports six different edge- finding methods: Sobel, Prewitt, Roberts, Laplacian of Gaussian, Zero-cross, and Canny.
Spatial Filtering Edges detected by the Laplacian can be used to sharpen the image ! +
Spatial Filtering
Sharpening can be done in 1 pass: = LAPLACIANOriginal ImageSharpened Image
Spatial Filtering Sharpening in General: Unsharp Masking and High Boost Filtering
Spatial Filtering Basic Idea (unsharp masking): Subtract a BLURRED version of an image from the image itself ! F sharp = F – F blurred
Spatial Filtering Variation: emphasize original image (high-boost filtering): F sharp = a*F – F blurred, a>=1 0a = a
Spatial Filtering Different Examples of High-Boost Filters: a= a=6 1e a=1e7+1 Laplacian + Image !
Spatial Filtering
Enhancement using the First Derivative: The Gradient Definition: 2 dim. column vector, (f) = [G x ; G y ], G is 1 st order derivative MAGNITUDE of gradient: Mag( (f))= SQRT(G x 2 + G y 2 )
Spatial Filtering For computational reasons the magnitude is often approximated by: Mag ~ abs(G x ) + abs(G y )
Spatial Filtering Mag ~ abs(G x ) + abs(G y ) “Sobel Operators”
Spatial Filtering Sobel Operators are a pair of operators ! Effect of Sobel Filtering:
Spatial Filtering Sobel Operators are a pair of operators !
Spatial Filtering Remember the result some slides ago: First and second order derivative 1. 1 st order generally produces thicker edges 2. 2 nd order shows stronger response to detail 3. 1 st order generally response stronger to gray level step 4. 2 nd order produce double (pos/neg) response at step change
Spatial Filtering In practice, multiple filters are combined to enhance images
Spatial Filtering …continued
Spatial Filtering
Non – Linear Filtering: Order-Statistics Filters Median Min Max
Spatial Filtering Median: The median M of a set of values is such that half the values in the set are less than (or equal to) M, and half are greater (or equal to) M
Spatial Filtering Most important properties of the median: less sensible to noise than mean an element of the original set of values Can be computed in O(n)
Spatial Filtering Median vs. Mean
Spatial Filtering Min and Max MIN MAX
Spatial Filtering Basics of Morphological Filtering
Spatial Filtering Dilation:
Spatial Filtering Erosion:
Spatial Filtering opening:
Spatial Filtering closing:
Spatial Filtering Opening on binary images: for i=2:2:18 se = strel('disk',i); imo = imopen(im1,se); subplot(3,3,(i)/2); imshow(imo); s=sprintf('disksize: %d',i); title(s); end
Spatial Filtering Opening on gray images:
Spatial Filtering Closing on binary images: for i=2:2:18 se = strel('disk',i); imo = imclose(im1,se); subplot(3,3,(i)/2); imshow(imo); s=sprintf('disksize: %d',i); title(s); end
Spatial Filtering Closing on gray images:
Spatial Filtering Closing o Opening & Opening o Closing
Spatial Filtering Morphological Edges: Dilation - Original
Spatial Filtering (Exercise: surveillance camera)
Spatial Filtering …end of Operations in Spatial Domain. And now to something completely different ( really ?)
The FREQUENCY Domain
Some of these slides base on the textbook Digital Image Processing by Gonzales/Woods Chapter 4
Frequency Domain So far we processed the image ‘directly’, i.e. the transformation was a function of the image itself. We called this the SPATIAL domain. So what’s the FREQUENCY domain ?
Sound Let’s first forget about images, and look at SOUND. SOUND: 1 dimensional function of changing (air-)pressure in time Pressure Time t
Sound SOUND: if the function is periodic, we perceive it as sound with a certain frequency (else it’s noise). The frequency defines the pitch. Pressure Time t
Sound The AMPLITUDE of the curve defines the VOLUME
Sound The SHAPE of the curve defines the sound character Flute String Brass
Sound How can the SHAPE of the curve be defined ?
Sound Listening to an orchestra, you can distinguish between different instruments, although the sound is a SINGLE FUNCTION ! Flute String Brass
Sound If the sound produced by an orchestra is the sum of different instruments, could it be possible that there are BASIC SOUNDS, that can be combined to produce every single sound ?
Sound The answer (Charles Fourier, 1822): Any function that periodically repeats itself can be expressed as the sum of sines/cosines of different frequencies, each multiplied by a different coefficient
Sound Or differently: Since a flute produces a sine-curve like sound, a (huge) group of (outstanding) talented flautists could replace a classical orchestra. (Don’t take this remark seriously, please)
1D Functions A look at SINE / COSINE The sine-curve is defined by: Frequency (the number of oscillations between 0 and 2*PI) Amplitude (the height) Phase (the starting angle value) The constant y-offset, or DC (direct current)
1D Functions The general sine-shaped function: f(t) = A * sin( t + ) + c Amplitude Frequency Phase Constant offset (usually set to 0)
1D Functions Remember Fourier: …A function…can be expressed as the sum of sines/cosines… What happens if we add sine and cosine ?
1D Functions a * sin( t) + b * cos( t) = A * sin( t + ) (with A=sqrt(a^2+b^2) and tan = b/a) Adding sine and cosine of the same frequency yields just another sine function with different phase and amplitude, but same frequency. Or: adding a cosine simply shifts the sine function left/right and stretches it in y-direction. It does NOT change the sine-character and frequency of the curve.
1D Functions Remember Fourier, part II: Any function that periodically repeats itself… => To change the shape of the function, we must add sine-like functions with different frequencies.
1D Functions This applet shows the result: Applet: Fourier Synthesis
1D Functions What did we do ? Choose a sine curve having a certain frequency, called the base-frequency Choose sine curves having an integer multiple frequency of the base-frequency Shift each single one horizontally using the cosine-factor Choose the amplitude-ratio of each single frequency Sum them up
1D Functions This technique is called the FOURIER SYNTHESIS, the parameters needed are the sine/cosine ratios of each frequency. The parameters are called the FOURIER COEFFICIENTS
1D Functions As a formula: f(x)= a 0 /2 + k=1..n a k cos(kx) + b k sin(kx) Fourier Coefficients
Note: The set of a k, b k TOTALLY defines the CURVE synthesized ! We can therefore describe the SHAPE of the curve or the CHARACTER of the sound by the (finite ?) set of FOURIER COEFFICIENTS ! 1D Functions
Examples for curves, expressed by the sum of sines/cosines (the FOURIER SERIES): 1D Functions
SAWTOOTH Function 1D Functions f(x) = ½ - 1/pi * n 1/n *sin (n*pi*x) Freq.sincos /20 31/30 41/40
SQUARE WAVE Function 1D Functions f(x) = 4/pi * n=1,3,5 1/n *sin (n*pi*x) Freq.sincos /30 51/50 71/70
What does the set of FOURIER COEFFICIENTS tell about the character of the shape ? 1D Functions
Result: Steep slopes introduce HIGH FREQUENCIES. 1D Functions
Motivation for Image Processing: Steep slopes showed areas of high contrast… …so it would be nice to be able to get the set of FOURIER COEFFICIENTS if an arbitrary (periodically) function is given. (So far we talked about 1D functions, not images, this was just a motivation) 1D Functions
The Problem now: Given an arbitrary but periodically 1D function (e.g. a sound), can you tell the FOURIER COEFFICIENTS to construct it ? 1D Functions
The answer (Charles Fourier): YES. 1D Functions
With: e ix =cos(x)+ i sin(x) The coefficients F(u) are given by (continous): F(u)= f(x)e -i2 ux dx Discrete (M=number of samples): F(u)=1/M x f(x)e -i2 ux/M 1D Functions
Note the similarity between analysis and synthesis: Analysis : F(u)=1/M x=0 f(x)e -i2 ux/M dx (with u=0..M-1) Synthesis: f(x)= u=0..M-1 F(u)e i2 ux/M (with x=0..M-1) 1D Functions
We don’t want to explain the mathematics behind the answer here, but simply use the MATLAB Fourier Transformation Function. 1D Functions
MATLAB - function fft: Input: A vector, representing the discrete function Output: The Fourier Coefficients as vector of imaginary numbers, scaled 1D Functions
Example: 1D Functions x=0:2*pi/(2047):2*pi; s=sin(x)+cos(x) + sin(2*x) + 0.3*cos(2*x); f=fft(s); i i i Freq. 0Freq. 1Freq. 2Freq. 3 cos sin
1D Functions FrReIm FrReIm 0~00 1~1 2~0.3~ i i i Transformation: t(a) = 2*a / length(result vector)
1D Functions The fourier coefficients are given by: F=fft(function) L=length(F); %this is always = length(function) Coefficient for cosine, frequency k-times the base frequency: real(F(k+1)) * 2 / L Coefficient for sine, frequency k-times the base frequency: imag(F(k+1)) * 2 / L Since: a * sin( t) + b * cos( t) = A * sin( t + ) the Amplitude is given by: A=sqrt(a^2+b^2), The Phase by: tan = b/a
1D Functions An application using the Fourier Transform: Create an autofocus system for a digital camera We did this already, but differently ! (MATLAB DEMO)
1D Functions Second application: Shape Database using Fourier Features (revisited)
Some Results query Top 9
Some Results query Top 9
2D Functions From Sound to Images: 2D Fourier Transform
2D Functions The idea: Extend the base functions to 2 dimensions: f u (x) = sin(ux) f u,v (x,y) = sin(ux + vy)
2D Functions Some examples: The base function, direction x: u=1, v=0 y x
2D Functions The base function, direction y: u=0, v=1
2D Functions u=2, v=0
2D Functions u=0, v=2
2D Functions u=1, v=1
2D Functions u=1, v=2
2D Functions u=1, v=3
2D Functions As in 1D, the 2D image can be phase-shifted by adding a weighted cosine function: f u,v (x,y) = a k sin(ux + vy) + b k cos(ux + vy) + =
2D Functions As basic functions, we get a pair of sine/cosine functions for every pair of frequency-multiples (u,v): sin cos sin cos sin cos sin cos sin cos sin cos sin cos sin cos sin cos sin cos sin cos sin cos v u
2D Functions Every single sin/cos function gets a weight, which is the Fourier Coefficient: a b a b a b a b v u a b a b a b a b a b a b a b a b
2D Functions Summing all basic functions with their given weight gives a new function. As in 1D: Every 2D function can be synthesized using the basic functions with specific weights. As in 1D: The set of weights defines the 2D function.
2D Functions Example: Summing basic functions of different frequencies:
2D Functions Example: Summing basic functions of different frequencies:
2D Functions MATLAB Demo: Bear Reconstruction