Spatial
Spatial
Spatial domain
• Image plane
• Image processing methods based on direct manipulation of pixels
• Two principal image processing technique classifications
1. Intensity transformation methods
2. Spatial filtering methods
Background
• Spatial domain
– Aggregate pixels composing an image
– Computationally more efficient and require less processing resources for implementation
• Spatial domain processes denoted by the expression
s = T (r)
Intensity Transformation and Spatial Filtering 2
∗ Image contrast could be low due to poor illumination, lack of dynamic range in the sensor, or wrong setting of
lens aperture during image acquisition
∗ Increase the dynamic range of gray levels in the image being processed to the full intensity range of recording
medium or display device
∗ Figure 3.10
∗ Thresholding function
– Gray-level slicing
∗ Figure 3-11
∗ Two approaches
1. Display in one value (white) all the values in the range of interest, and change to black everything else
2. Brighten or darken desired range of intensities leaving all other intensity levels unchanged
3. Figure 3-12
– Bit-plane slicing
∗ Figure 3-13
∗ Most changes can be captured in significant bits
∗ You may not be able to perceive minute changes reflected in low bits
∗ Figure 3-14
· See how the border is black in some cases and white in others (pixel value 194, or 1100 0010)
∗ Decomposition useful for image compression
∗ Combine some of the bit planes
∗ Figure 3-15
– The image can be made brighter by multiplying each pixel by a small amount (and made darker by dividing each
pixel by a constant)
∗ Notice that the use of saturation arithmetic makes the operation irreversible
∗ Multiplying each pixel by 2 and then, dividing it by 2 will not yield the original pixel intensity
– Gain and bias
∗ Multiplication by a constant is known as gain (γ) while addition by a constant is adjustment of bias (β)
g(x, y) = γf (x, y) + β
∗ In older monitors, gain was used to adjust contrast while bias was used to adjust brightness or blacklevel
∗ If γ = 1, the operation reduced to just addition of a constant
∗ If β = 0, the operation reduces to just multiplication by a constant
∗ If γ = −1 and β = L − 1, the operation reduced to grayscale inversion (or image negative)
– Linear contrast stretch
Intensity Transformation and Spatial Filtering 5
∗ Maps gray levels between [g1 , g2 ] in the input image to [g10 , g20 ]
g20 − g10
g(x, y) = (f (x, y) − g1 ) + g10
g2 − g1
Histogram processing
– Second condition guarantees that gray levels are preserved in output image
– Inverse transform from s back to r is denoted
r = T −1 (s) 0≤s≤L−1
∗ T −1 may fail to be single valued unless we require T (r) to be strictly monotonically increasing in the interval
[0,L-1]
– Gray values in an image may be viewed as random variables in the interval [0, L − 1]
– Probability density function
∗ Used to describe a random variable
∗ Let pr (r) and ps (s) denote the probability density functions of random variables r and s, respectively
· pr and ps are different functions
∗ If pr (r) and T (r) are known, and T (r) is continuous and differentiable over the range of values of interest, the
PDF of the transformed variable s can be obtained using the simple formula
dr
ps (s) = pr (r)
ds
∗ A transformation function of importance in image processing is:
Z r
s = T (r) = (L − 1) pr (w)dw
0
· Substituting the result in the equation for the PDF of transformed variable, we get
dr
ps (s) = pr (r)
ds
1
= pr (r)
(L − 1)pr (r)
1
= 0≤s≤L−1
L−1
· The form of ps (s) is a uniform PDF, or performing the intensity transformation yields a random variable
s, characterized by a uniform PDF
· T (r) depends on pr (r) but the resulting ps (s) always is uniform, regardless of the form of pr (r)
· Figure 3.18
– For discrete values for an image of size n = M × N pixels, we have
nk
pr (rk ) = k = 0, 1, 2, . . . , L − 1
n
Intensity Transformation and Spatial Filtering 7
– Processed image obtained by mapping each pixel with level rk into a corresponding pixel with level sk
∗ Histogram equalization or histogram linearization
– Transform spreads the histogram of the input image so that the levels of histogram-equalized image span a fuller
range of gray scales
– Discrete form does not guarantee uniform distribution of probability density function
– Advantages
∗ More dynamic range of gray level distribution
∗ Fully automatic algorithm; no parameters need be specified
∗ Simple to compute
– Example: Consider a 3-bit image (8 intensity levels) of size 64 × 64 pixels with the intensity distribution as follows:
rk nk pr (rk ) = nk /n
0 790 0.19
1 1023 0.25
2 850 0.21
3 656 0.16
4 329 0.08
5 245 0.06
6 122 0.03
7 81 0.02
∗ Histogram in Figure 3.19a
∗ Values of histogram transformation are obtained by (last column shows the value rounded to nearest integer)
P0
s0 = T (r0 ) 7 j=0 pr (rj ) = 7pr (r0 ) 1.33 1
P1
s1 = T (r1 ) 7 j=0 pr (rj ) = 7(pr (r0 ) + pr (r1 )) 3.08 3
s2 = T (r2 ) 4.55 5
s3 = T (r3 ) 5.67 6
s4 = T (r4 ) 6.23 6
s5 = T (r5 ) 6.65 7
s6 = T (r6 ) 6.86 7
s7 = T (r7 ) 7.00 7
∗ Transformation function and equalized histogram in Figure 3.19 b and c
∗ Some intensity levels are mapped to a single intensity level, hence fewer intensity levels in the equalized
histogram
∗ Intensity levels of the equalized histogram span a wider range of intensity scale, leading to contrast enhance-
ment
∗ Perfectly flat histograms
· Not possible because of two reasons
1. Histograms are approximations to PDFs
2. We cannot create new intensity levels when we perform histogram equalization because of discrete
values
· Spreading the histogram of input image results in a wider span of intensities for equalized image
Intensity Transformation and Spatial Filtering 8
rk = T −1 (sk ) k = 0, 1, 2, . . . , L − 1
∗ Satisfies the two conditions outlined earlier only if none of the levels rk , k = 0, 1, 2, . . . , L − 1 are missing
from input image (none of the components of the equalized histogram are zero)
∗ Plays a central role in histogram matching scheme
– Figure 3.20
∗ Significant improvement in low contrast images (first three)
∗ Transformation functions (Figure 3.21)
· Transformation 4 as nearly linear shape, indicating that the inputs were mapped to nearly equal outputs
∗ Transformation function changes contrast by redistributing pixel gray levels but does not alter the number
(content given by probability) of matched gray levels
• Histogram matching/Histogram specification
– Histogram equalization gives an image with uniformly distributed gray levels automatically
– Histogram matching allows us to specify the shape of the histogram of new image
∗ Useful when you have to create two images with same contrast and brightness
– Development of method
∗ Consider continuous gray levels r and z (continuous random variables)
· r is the intensity level of input image
· z is the intensity level of processed image
∗ Let pr (r) and pz (z) denote their continuous probability density functions
∗ pr (r) is estimated from input image
∗ pz (z) is the specified probability density function desired in output image
∗ Let s be a random variable with the property
Z r
s = T (r) = (L − 1) pr (w)dw
0
Find the transformation function that will produce an image whose intensity PDF is
(
3z 2
(L−1) 3 0 ≤ z ≤ (L − 1)
pz (z) =
0 otherwise
s = T (r)
Z r
= (L − 1) pr (w) dw
Z r0
2
= w dw
L−1 0
r2
=
L−1
This gives a uniform PDF due to histogram equalization transform
∗ Since we are interested in an image with a specified histogram, we compute
Z z
G(z) = (L − 1) pz (w) dw
0
Z z
3
= w2 dw
(L − 1)2 0
z3
=
(L − 1)2
∗ Multiply every histogram-equalized pixel by (L − 1)2 and compute its cube root giving an image whose
intensities z have the PDF pz (z) = 3z 2 /(L − 1)2 in the interval [0, L − 1]
∗ Since s = r2 /(L − 1), we can generate z directly from r, the input intensity by
1/3
(L − 1)2 s
z =
1/3
r2
2
= (L − 1)
(L − 1)
2 1/3
= (L − 1)r
sk = T (rk )
k
X
= (L − 1) pr (rj )
j=0
Intensity Transformation and Spatial Filtering 10
k
X nj
= (L − 1) k = 0, 1, 2, . . . , L − 1
j=0
n
k
(L − 1) X
= nj
M N j=0
∗ Given a specific value of sk , the discrete formulation for G(z) is based on computing the transformation
function
Xq
G(zq ) = (L − 1) pz (zi )
i=0
zq = G−1 (sk )
for q = 0, 1, 2, . . . , L − 1 where pz (zi ) are the values of the specified histogram; Round the values of G to
integers in the range [0, L − 1] into an LUT
3. For every value of sk ∈ [0, L − 1], use the stored value of G from step 2 to find the corresponding value of zq
so that G(zq ) is closest to sk and store these mappings from s to z
4. Histogram equalize the input image; map every histogram-equalized value and map every equalized pixel sk
of this image to corresponding value zq in the histogram-specified image using the mappings from step 3
– Example: Using the 64 × 64 pixel image from histogram equalization example
∗ Histogram in Figure 3.23(a)
∗ Specified histogram is in column 2 below
zq Specified Actual
pz (zq ) pz (zk )
z0 =0 0.00 0.00
z1 =1 0.00 0.00
z2 =2 0.00 0.00
z3 =3 0.15 0.19
z4 =4 0.20 0.25
z5 =5 0.30 0.21
z6 =6 0.20 0.24
z7 =7 0.15 0.11
Intensity Transformation and Spatial Filtering 11
sk = {1, 3, 5, 6, 6, 7, 7, 7}
G(zi ) = {0, 0, 0, 1, 2, 5, 6, 7}
∗ May not work very well when details may get obscured due to small number of pixels or smaller neighborhoods
∗ Small number of pixels have a negligible effect on computation of global transformation
– Local methods work with neighborhood of each pixel to find a transformation
– A simple approach would be to define small rectangles on the image and process the pixels in selected rectangle
using the techniques already seen
∗ May have overlapping or non-overlapping rectangles
∗ You could also define one rectangle and move its center from pixel to pixel
∗ Compute the neighborhood and obtain a histogram equalization or histogram specification transformation
– Example – Blurring to reduce noise content
∗ Figure 3-32a – Slightly noisy 512 × 512 pixel image
∗ Figure 3.32b – Global histogram equalization
∗ Figure 3.32c – Local histogram with a 3 × 3 neighborhood
∗ Local vs global histogram equalization
· Intensity values of objects too close to the intensity of large squares, and their sizes too small to influence
global histogram equalization significantly enough to show the detail
• Use of histogram statistics for image enhancement
– Let r be the discrete random variable representing intensity values in the range [0, L − 1], and p(ri ) the normalized
histogram component corresponding to ri
– Mean: Average gray scale intensity
L−1
X
m= ri p(ri )
i=0
Obviously, µ0 = 1 and µ1 = 0
– Variance (second moment): Average contrast
L−1
X
σ 2 (r) = µ2 (r) = (ri − m)2 p(ri )
i=0
√
– Standard deviation is defined simply as σ = µ2
– Mean and variance can also be measured directly from sample values (sample mean and sample variance) as
M −1 N −1
1 X X
m= f (x, y)
M N x=0 y=0
M −1 N −1
1 X X 2
σ2 = [f (x, y) − m]
M N x=0 y=0
∗ Image histogram is given by: (6, 7, 7, 5), or after normalization, p(ri ) = (0.24, 0.28, 0.28, 0.20)
∗ The average value of intensities in the image is
3
X
m = ri p(ri )
i=0
= (0)(0.24) + (1)(0.28) + (2)(0.28) + (3)(0.20)
= 1.44
– Enhancement can be based on measuring global mean and variance over entire image and adjusting those values to
change intensity and contrast
– These quantities can also be used for local enhancement by changing local mean and variance in predefined regions
about each pixel
∗ Consider pixel at location (x, y)
∗ Let Sxy be a subimage/neighborhood of specified size, centered at (x, y)
∗ Mean value of the neighborhood is
L−1
X
mSxy = ri pSxy (ri )
i=0
∗ Local mean and local variance are a measure of average intensity and average contrast, respectively in the
neighborhood
∗ Low contrast areas are also candidates for enhancement, if σSxy ≤ k2 σG where k2 > 0 and σG is global
standard deviation
· k2 > 1.0 to enhance light areas
· k2 < 1.0 to enhance dark areas
∗ We need a lower bound on contrast so as not to enhance uniform intensity areas with standard deviation 0
· This can be achieved by requiring k1 σG ≤ σSxy , with k1 < k2
∗ A pixel meeting all the requirements can be enhanced by multiplying it by a specific constant E to in-
crease/decrease its gray level
∗ Let f (x, y) and g(x, y) be the input and enhanced values of the pixel at location (x, y)
E · f (x, y) if mSxy ≤ k0 mG && k1 σG ≤ σSxy ≤ k2 σG
g(x, y) =
f (x, y) otherwise
• Filtering
– Roots in the use of Fourier transform for signal processing in frequency-domain
– Accepting or rejecting certain frequency components
– Low-pass filter, high-pass filter, band-pass filter
– Filtering effects of frequency-based filters can be achieved by using spatial filters
– Filter, mask, kernel, or window
∗ Values in filter subimage are referred to as coefficients
– Spatial filters have a 1:1 correspondence to frequency filters but can also do nonlinear filtering in addition (not
possible in frequency based filters)
• Spatial filtering
– Spatial filter consists of
1. A neighborhood, typically a small square or rectangle
2. A predefined operation performed on the image pixels in the neighborhood
– Creates a new pixel at the coordinates of the neighborhood center as the result of filtering operation
∗ Generally, the result is written into a new image as the pixels in the neighborhood may still be needed for the
filtering of other pixels
– Convolution – Moving the filter mask over pixels
– Linear filtering
∗ Product of filter coefficients with the corresponding pixels
P
∗ R = wij f (x + i, y + j)
∗ Fig 3-34
∗ Response g(x, y) of the filter at point (x, y)
· Given by the sum of products of filter coefficients and corresponding image pixels
· For a 3 × 3 filter, we have
X1 X 1
g(x, y) = w(i, j)f (x + i, y + j)
i=−1 j=−1
Intensity Transformation and Spatial Filtering 15
· The center coefficient of the filter (0, 0) aligns with the pixel at location (x, y)
∗ Odd dimensions of the filter
· For a mask of size m × n, assume that m = 2a + 1 and n = 2b + 1, where a > 0 and b > 0
· For a filter of size m × n
X a Xb
g(x, y) = w(i, j)f (x + i, y + j)
i=−a j=−b
9 Xa Xb
w(x, y) f (x, y) = w(s, t)f (x + s, y + t)
s=−a t=−b
∗ Convolution is given by
8f (x, y) X
a b
X
w(x, y) w(s, t)f (x − s, y − t)
s=−a t=−b
– The same equation can be used for convolution by rotating the mask by 180◦
– A general 3 × 3 mask is labeled as
w1 w2 w3
w4 w5 w6
w7 w8 w9
– Again, R is given by wT z as above, where w and z are 9-dimensional vectors formed from the coefficients of the
mask and image intensities, respectively
• Generating spatial filter masks
– Generating an m × n linear spatial filter mask requires the specification of mn mask coefficients
– The coefficients are selected based on the purpose of the filter
∗ All a filter does is to compute sum of products
– Replacing the pixels by the average intensity of a 3 × 3 neighborhood centered at the pixels
∗ Average value at location (x, y) is the sum of nine intensity values in the 3 × 3 neighborhood centered on (x, y)
divided by 9
∗ With zi , i = 1, 2, . . . , 9, the average is
9
1X
R= zi
9 i=1
∗ This is the same as
9
X
R= wi zi
i=1
with wi = 1/9
– Generating a spatial mask based on a continuous function of two variables
∗ Gaussian function of two variables has the basic form
x2 +y 2
h(x, y) = e− 2σ 2
where σ is the standard deviation and the coordinates x and y are integers
∗ The mask for a 3 × 3 neighborhood can be generated as w1 = h(−1, −1), w2 = h(−1, 0), . . ., w9 = h(1, 1)
∗ 2D Gaussian has a bell shape with standard deviation controlling the tightness of the bell
– Generating a nonlinear filter requires the size of the neighborhood and the operations to be performed on the image
pixels contained in the neighborhood
∗ Max operation
· A side effect is the blurring of edges which are also characterized by sharp transitions in intensity levels
∗ The filter can also be used to reduce false contouring
– Standard average of pixels in a 3 × 3 neighborhood
1 1 1
1
9×
1 1 1
1 1 1
P1 P1
∗ Same as g(x, y) = 19 c=−1 r=−1 f (x + c, y + r)
∗ Also called box filter (all coefficients of filter are equal)
∗ The entire image can be divided by 9 at the end of filtering
– Weighted average of pixels in a 3 × 3 neighborhood
1 2 1
1
16 ×
2 4 2
1 2 1
∗ Pixels are multiplied by different coefficients, giving more importance to some pixels at the expense of others
∗ Reduces blurring in the smoothing process
∗ Optimization by shifting right by four bits instead of dividing by 16
∗ General implementation to filter an image with a weighted averaging filter of size m × n is given by
Pa Pb
s=−a t=−b w(s, t)f (x + s, y + t)
g(x, y) = Pa Pb
s=−a t=−b w(s, t)
13 15 15 30 25
17 15 15 15 22
26 28 21 10 29
27 1 23 20 5
31 7 27 24 21
∗ Application of 3 × 3 median filter yields
- - - - -
- 15 15 21 -
- 21 15 20 -
- 26 21 21 -
- - - - -
∗ Isolated clusters with area less than n2 /2 are eliminated by n × n median filter
∗ Figure 3.49
– Max filter
∗ Helps with finding the brightest points in a neighborhood
∗ Erodes dark areas adjacent to light regions
– Min filter
∗ Opposite of max filter
• Highlight fine detail in an image or enhance detail that has been blurred
– Averaging is same as integration
– Achieve sharpening by differentiation
∗ Strength of response of a derivative operator is proportional to the degree of discontinuity of image at the point
where the operator is applied
∗ Differentiation enhances edges and discontinuities (including noise) and deemphasizes slow varying gray scale
values
∗ High pass filtering
• Foundation
– One dimensional derivatives to simplify the discussion
– Behavior of derivatives at the beginning (step) and end (ramp) of discontinuities and along gray-level ramps
∗ Discontinuities used to model noise points, lines, and edges
– Derivative of a digital function
∗ Defined in terms of differences
∗ Definition for a first derivative must be
· Zero in flat segments (areas of constant gray level)
· Nonzero at the onset of a gray-level step or ramp
· Nonzero along ramps
∗ Definition of a second derivative must be
· Zero in flat areas
· Nonzero at the onset and end of a gray level step or ramp
· Zero along ramps of constant slope
∗ Keep in mind the finite nature of digital functions
· Finite values
Intensity Transformation and Spatial Filtering 19
∂f
= f (x + 1) − f (x)
∂x
∗ Second-order derivative is defined as the difference
∂2f
= f (x + 1) + f (x − 1) − 2f (x)
∂x2
· The two definitions satisfy the conditions laid out above
· Fig 3-50
· Scan line contains an intensity ramp, three sections of constant intensity, and an intensity step
· First-order derivatives are indicated by dots while the second-order derivatives are indicated by squares
· Circles indicate the onset or end of intensity transitions
· First-order derivative produces thick edges while second-order derivative produces fine edges
· For isolated point, second-order derivative gives a much stronger response than first-order derivative
· Second-order derivative can enhance fine detail much more than the first-order derivative
∗ Conclusions from above
· First-order derivatives produce thicker edges in an image
· Second-order derivatives have stronger response to fine detail
· First-order derivatives have stronger response to gray-level step
· Second-order derivatives produce a double response at step changes in gray level
∗ In most applications, second derivative is better suited than first derivative because of enhancement of fine
detail
∗ Zero-crossing property
· Sign of the second derivative changes at the onset and end of a step or ramp
· In a step transition, a line joining the two values crosses the horizontal axis midway between the two
extremes
· Useful to detect edges
• Use of second derivative for enhancement – the Laplacian
– Define a discrete formulation of second order derivative and construct a filter mask based on that formulation
– Preference for isotropic filters
∗ Response is independent of direction of discontinuities in image
∗ Rotationally invariant filters
· Rotating the image and then applying the filter gives the same result as applying the filter first and then
rotating the image
– Development of method
∗ Laplacian – simplest derivative operator
2 2
∗ Defined as ∇2 f = ∂∂xf2 + ∂∂yf2
· Since derivative of any order is a linear operation, Laplacian is a linear operator
∗ Discrete Laplacian operator
· Must satisfy the properties of second derivative
· Partial-order derivative in x direction
∂2f
= f (x + 1, y) + f (x − 1, y) − 2f (x, y)
∂x2
Intensity Transformation and Spatial Filtering 20
∂2f
= f (x, y + 1) + f (x, y − 1) − 2f (x, y)
∂y 2
· Discrete Laplacian in two dimensions is given by taking the sum of partials
· Figure 3.52
∗ Scaling Laplacian
· Add to the image its minimum value to bring the new minimum to zero and then, scale the result to the full
[0, L − 1] range
· Figure 3.53
– Simplifications
∗ Computation of Laplacian and subtraction can be done in a single pass of a single mask
– Using Laplacian
Af (x, y) − ∇2 f (x, y) if center is negative
g(x, y) =
Af (x, y) + ∇2 f (x, y) if center is positive
– High-boost filtering is standard Laplacian sharpening when A = 1
– If A is large, high-boost image is approximately equal to the original image multiplied by a constant
• Use of first derivative for enhancement – the gradient
– First derivative implemented using the magnitude of gradient
– Gradient of f at (x, y) is defined as the column vector
" #
∂f
gx ∂x
∇f = = ∂f
gy ∂y
∗ Important geometric property of the vector: it points in the direction of the greatest rate of change of f at (x, y)
– Magnitude of vector ∇f , denoted by M (x, y), is given by
M (x, y) = mag(∇f )
q
= gx2 + gy2
"
2 2 #1/2
∂f ∂f
= +
∂x ∂y
Intensity Transformation and Spatial Filtering 22
∇f ≈ |Gx | + |Gy |
gx = f (x, y + 1) − f (x, y)
gy = f (x + 1, y) − f (x, y)
gx = f (x + 1, y + 1) − f (x, y)
gy = f (x, y + 1) − f (x + 1, y)
or, pictorially as
-1 -2 -1 -1 0 1
0 0 0 -2 0 2
1 2 1 -1 0 1
· Difference in first and third row in the left mask gives partial derivative in the vertical direction
· Difference in first and third column in the right mask gives partial derivative in the horizontal direction
∗ Mask gives gradient in x and y directions, and coefficients sum to zero indicating no change in constant gray
level areas
Intensity Transformation and Spatial Filtering 23
• Use a combination of the filters to enhance the image depending on the application
• Figure 3.43
• Reading assignment