Image Processing
2. Image Enhancement in Spatial
Domain
By MT
Road Map
• Introduction
• Contract Stretching
• Histogram Equalization
• Image Smoothing
• Image Sharpening
By MT
Introduction
By MT
Introduction
• Image enhancement in the spatial domain involves directly manipulating the pixels of an image to
improve its visual appearance or to extract useful information.
• This method focuses on the spatial location of pixels and the operations applied to them.
• Intensity filtering in spatial domain mostly uses the following formula:
where:
• g(x ,y) is the output intensity of the processed pixel
• T is some transformation applied on the input pixel intensity f(x, y)
By MT
Introduction
• The point (x0 , y0) shown is an arbitrary location
in the image, and the small region shown is the
neighborhood of
(x0 , y0) which is smaller in size than the image.
• For a grayscale image, the formula
can be further simplified into
By MT
Introduction
• Image enhancement in the spatial domain involves directly manipulating the pixels of an image to
improve its visual appearance or to extract useful information. This method focuses on the spatial
location of pixels and the operations applied to them.
• Here are some common techniques used in spatial domain image enhancement:
• Point Processing Operations
• These operations are applied to individual pixels independently.
• Contrast Stretching: Enhances the contrast of an image by stretching the range of
intensity values. For example, if an image has intensity values ranging from 50 to 150,
contrast stretching can expand this range to 0 to 255.
By MT
Introduction
• Here are some common techniques used in spatial domain image enhancement:
• Point Processing Operations
• Thresholding: Converts an image to a binary image by assigning all pixels below a certain
threshold to 0 (black) and all pixels above that threshold to 1 (white).
• Log Transformations: Used to expand the values of dark pixels in an image while
compressing the higher values. This is useful for enhancing details in the darker regions of
an image.
• Power-Law (Gamma) Transformations: These transformations can either enhance or
compress image details based on the value of the gamma parameter.
By MT
Introduction
• Here are some common techniques used in spatial domain image enhancement:
• Spatial Filtering
• Spatial filtering involves the use of a filter (or kernel) that is applied to the image.
• Smoothing Filters: Used to reduce noise and smooth the image.
• Sharpening Filters: Enhance the edges and fine details in an image.
By MT
Introduction
• Here are some common techniques used in spatial domain image enhancement:
• Histogram Processing
• Histogram processing techniques adjust the image's contrast based on the intensity
distribution of the pixels.
• Histogram Equalization: A method to improve the contrast of an image by spreading out
the most frequent intensity values. This technique often results in a more uniform
histogram.
• Histogram Matching (Specification): Adjusts the image's histogram to match a specified
histogram. This is useful for standardizing images from different sources.
By MT
Contrast Stretching
By MT
Contrast Stretching
• Low-contrast images can result from poor illumination, lack of dynamic range in the imaging sensor,
or even the wrong setting of a lens aperture during image acquisition.
• Contrast stretching expands the range of intensity levels in an image so that it spans the ideal full
intensity range of the recording medium or display device.
• It is a simple and effective way to improve the contrast of an image by stretching the range of
intensity values. This process makes the features of the image more distinct and easier to analyze.
• Contrast stretching involves transforming the original pixel values to span a desired range of values.
The simplest form of contrast stretching maps the minimum and maximum intensity values in the
original image to the minimum and maximum values in the new, enhanced image.
By MT
Contrast Stretching
Steps of Contrast Stretching
1. Identify Minimum and Maximum Pixel Values: Find the minimum and maximum pixel intensity
values in the original image, denoted as smin and smax, respectively.
2. Specify Desired Range: Define the desired minimum and maximum intensity values for the output
image, typically 0 and 255 for an 8-bit grayscale image.
3. Apply Transformation: Use a transformation to map the original pixel values to the desired range.
By MT
Contrast Stretching
Example
1. Original Image Values: Suppose the minimum and maximum pixel intensities in the original image
are 50 and 200, respectively.
2. Desired Range: We want the output image to have pixel values ranging from 0 to 255.
3. Transformation Formula: For any pixel value r in the original image, the new pixel value s in the
enhanced image is calculated using a transformation function.
By MT
Contrast Stretching
Example
• The figure on the right shows a typical transformation
used for contrast stretching.
• The locations of points (r1, s1) and (r2, s2) control the
shape of the transformation function.
• If r1 =s1 and r2=s2, the transformation is linear and
makes no changes to the image.
• If r1=r2, s1=0, and s2=L−1 (where L is the maximum
intensity value, typically 255 for an 8-bit image), it
results in a thresholding operation, creating a binary
image.
By MT
Contrast Stretching
By MT
Histogram Processing
By MT
Histogram Processing
• Histogram is a visual graph representation of quantitative data.
• It's a technique to improve the contrast in images. It spreads out the most frequent intensity values,
which makes the image details more visible:
• Dark areas become darker
• Bright areas become brighter
• This increases the overall contrast of the image
By MT
Histogram Processing
• This is how it works:
• Calculate how often each brightness level occurs in the image (this is the histogram).
• Create a transformation that spreads out these brightness levels more evenly.
• Apply this transformation to the original image.
• This results in:
• The output image has a more uniform distribution of brightness levels.
• This often makes details more visible that were hard to see in the original image.
By MT
Histogram Processing
• Histogram is a visual graph representation of quantitative data.
• It can be used to show the frequency of pixels with certain intensity values.
By MT
Histogram Processing
• Let rk, where r represents a raw pixel and k denotes the possible intensity range for that pixel,
generally k - 0, 1, 2, ..., L - 1
• An unnormalized histogram can be defined as:
• Where nk is the number of pixels with the intensity rk
By MT
Histogram Processing
• A normalized histogram can be defined as:
• Where p(rk) is the probability or estimate of the frequency of a certain intensity value in the
image. M and N are the number of rows and columns of an image.
• The probabilities p(rk) of all the possbile k values always add up to 1.
By MT
Histogram Processing
Histogram Equalization
By MT
Histogram Processing
Histogram Equalization
• Assuming initially continuous intensity values, let the variable r denote the intensities of an image to
be processed. We assume that r is in the range [0, L − 1], with r = 0 representing black and r = L − 1
representing white.
• For r satisfying these conditions, we focus attention on transformations (intensity mappings) of the
form that produce an output intensity value s, for a given intensity value r in the input image.
• We assume that:
a) T(r) is a monotonic increasing function in the interval 0 ≤ r ≤ L − 1; and
b) 0 ≤ T (r ) ≤ L − 1 for 0 ≤ r ≤ L − 1
By MT
Histogram Processing
Histogram Equalization
• In some formulations, inverse transformation is used:
• In which case we change condition (a) to:
(a’) T (r ) is a strictly monotonic increasing function in the interval 0 ≤ r ≤ L − 1.
• The condition in (a) that T(r) be monotonically increasing guarantees that output intensity values will
never be less than corresponding input values, thus preventing artifacts created by reversals of
intensity. Condition (b) guarantees that the range of output intensities is the same as the input.
Finally, condition (a’) guarantees that the mappings from s back to r will be one-to-one, thus
preventing ambiguities.
By MT
Histogram Processing
Histogram Equalization
• The intensity of an image may be viewed as a random variable in the interval [0, L - 1].
• The above formula on the right describes how the probability
distribution of pixel intensities changes after applying
the transformation T(r).
• ps(s) is the probability density function (PDF) of the new intensity values after transformation.
• pr(r) is the probability density function (PDF) of the original intensity values.
• |dr / ds| is the derivative of r with respect to s, measures how the original intensities r change with
respect to the new intensities s.
By MT
Histogram Processing
Histogram Equalization
• This is one of the important transformations (an integral) of image processing:
• Integral: Think of it as a way to add up a bunch of small pieces to find the total.
• pr(w): The probability density function (PDF) of the original intensity values.
• (L − 1): The maximum possible intensity value in the transformed image (for example, 255 for an 8-
bit grayscale image).
• s: The new intensity value after transformation.
• r: The original intensity value.
• w: A dummy variable representing all possible intensity values from 0 to r.
By MT
Histogram Processing
Histogram Equalization
• This is one of the important transformations (an integral) of image processing:
• The function pr(w) tells you the likelihood (or density) of each intensity value w occurring in the
image.
• The integral represents the sum of all the densities from intensity 0 to intensity r. Think of it like
adding up all the little pieces of probability from 0 to r.
• The term (L − 1) scales the result of the integral to ensure the transformed intensity values s are in
the appropriate range (e.g., 0 to 255).
By MT
Histogram Processing
Histogram Equalization
• For example, from the table on the right, if we
want to transform the original intensity r = 1:
By MT
Spatial Filtering
By MT
Spatial Flitering
Fundamentals:
• Here, “Filtering” refers to passing, modifying, or rejecting specified frequency components of an
image.
• For example, a filter that passes low frequencies is called a lowpass filter. The net effect produced by
a lowpass filter is to smooth an image by blurring it.
• Spatial filtering modifies an image by replacing the value of each pixel by a function of the values of
the pixel and its neighbors.
• If the operation performed on the image pixels is linear, then the filter is called a linear spatial filter.
Otherwise, the filter is a nonlinear spatial filter.
By MT
Spatial Flitering
Linear spatial filtering:
• A linear spatial filter performs a sum-of-products operation between an image f and a filter kernel,
w.
• The kernel is an array whose size defines the neighborhood of operation, and whose coefficients
determine the nature of the filter.
• Other terms used to refer to a spatial filter kernel are mask, template, window, or simply kernel.
By MT
Spatial Flitering
Linear spatial filtering:
• The figure on the right llustrates the
mechanics of linear spatial filtering
using a 3 3× kernel.
• At any point (x, y) in the image, the response,
g(x, y), of the filter is the sum of products of
the kernel coefficients and the image pixels
encompassed by the kernel.
By MT
Spatial Flitering
Image Smoothing:
• Smoothing (also called averaging) spatial filters are used to reduce sharp transitions in intensity.
• One of the use of image smoothing is random noise reduction. because random noise typically
consists of sharp transitions in intensity.
• Smoothing is used to reduce irrelevant detail in an image, where “irrelevant” refers to pixel regions
that are small with respect to the size of the filter kernel.
• Convolving a smoothing kernel with an image blurs the image, with the degree of blurring being
determined by the size of the kernel and the values of its coefficients.
By MT
Spatial Flitering
Image Smoothing: Box Filter Kernel
• The simplest, separable lowpass filter kernel is the box kernel, whose coefficients have the same
value (typically 1).
• The normalization constant is applied to the filter. It’s calculated as 1/mn where m and n are the
dimensions of the filter (height and width).
By MT
Spatial Flitering
Image Smoothing: Box Filter Kernel
• This normalization constant has two purposes:
• Maintaining Average Intensity: It ensures that the average value of a uniformly intense area
remains the same after filtering. In other words, if an area of the image has a constant
intensity, that intensity should not change after applying the filter.
• Avoiding Bias: It prevents the filter from altering the overall brightness of the image. The total
sum of pixel values before and after filtering should remain the same.
• This is a separable filter because you can break down the 2D filtering process into two 1D processes -
first along the rows and then along the columns, or vice versa. This reduces the computation time.
By MT
Spatial Flitering
Image Smoothing: Box Filter Kernel
• The following figure shows a test pattern image of size 1024 x 1024 pixels. These images are the
results obtained using box filters of size m x m with m = 3, 11, and 21, respectively.
• For m = 3, there’s a slight overall blurring of the image, with the image features whose sizes are
comparable to the size of the kernel being affected significantly more, including the thinner lines in
the image and the noise pixels contained in the boxes on the right side of the image.
By MT
Spatial Flitering
Image Smoothing: Box Filter Kernel
• The filtered image also has a thin gray border, the result of zero-padding the image prior to filtering.
• Zero padding extends the boundaries of an image to avoid undefined operations when parts of a
kernel lie outside the border of the image.
By MT
Spatial Flitering
Image Smoothing: Box Filter Kernel
• Using the 11 x 11 kernel resulted in more pronounced blurring throughout the image, including a
more prominent dark border.
• The result with the 21 x 21 kernel shows significant blurring of all components of the image,
including the loss of the characteristic shape of some components, including, for example, the small
square on the top left and the small character on the bottom left. The dark border resulting from
zero padding is proportionally thicker than before.
By MT
Spatial Flitering
Image Smoothing: Gussian Filter Kernel
• A Gaussian filter is a type of lowpass filter used in image processing to smooth an image and reduce
noise. It is called "Gaussian" because the filter's shape is based on the Gaussian (or normal)
distribution, which looks like a bell curve.
By MT
Spatial Flitering
Image Smoothing: Gussian Filter Kernel
• The Gaussian function describes the distribution of weights in the filter. It’s defined as:
• Here, (s, t) are coordinates, σ is the standard deviation, and Ke represents the exponential function.
The parameter σ controls the spread or width of the Gaussian curve.
By MT
Spatial Flitering
Image Smoothing: Gussian Filter Kernel
• The Gaussian filter smooths an image by averaging the pixel values, but it does so in a way that gives
more weight to pixels near the center of the kernel and less weight to pixels farther away. This is
because the Gaussian function decreases rapidly as you move away from the center.
• By smoothing, the filter reduces the high-frequency components in the image, which often
correspond to noise. This results in a blurred image where the details and sharp transitions are
softened.
• Like with the box filter, the Gaussian filter is normalized so that the sum of all the weights in the
kernel is 1. This ensures that the overall brightness of the image remains unchanged after filtering.
By MT
Spatial Flitering
Image Smoothing: Gussian Filter Kernel
• By adjusting the σ parameter, you can
control the amount of smoothing.
• A larger σ results in more blurring because
the Gaussian function becomes wider,
averaging more pixels.
• A smaller σ results in less blurring.
By MT
Spatial Flitering
Image Smoothing: Gussian Filter Kernel
By MT
Spatial Flitering
Image Smoothing: Gussian Filter Kernel
By MT
Spatial Flitering
Image Smoothing: Gussian Filter Kernel
By MT
Spatial Flitering
Image Sharpening
• Image smoothing is accomplished in the spatial domain by pixel averaging (smoothing) in a
neighborhood because averaging is analogous to integration. It’s logical to conclude that sharpening
can be accomplished by spatial differentiation.
• Image sharpening is often referred to as highpass filtering where high frequencies (which are
responsible for fine details) are passed, while low frequencies are attenuated or rejected.
• Sharpness is usually measured at the onset and end of discontinuities (step and ramp
discontinuities) in intensities.
By MT
Spatial Flitering
Image Sharpening
• To measure sharp changes in intensity, first and second ordre derivative functions can be used.
• The first derivative:
1. Must be zero in areas of constant intensity.
2. Must be nonzero at the onset of an intensity step or ramp.
3. Must be nonzero along intensity ramps.
By MT
Spatial Flitering
Image Sharpening
• To measure sharp changes in intensity, first and second ordre derivative functions can be used.
• The second derivative:
1. Must be zero in areas of constant intensity.
2. Must be nonzero at the onset and end of an intensity step or ramp.
3. Must be zero along intensity ramps.
By MT
Spatial Flitering
Image Sharpening
By MT
Spatial Flitering
Image Sharpening
• Edges in digital images often are ramp-like transitions in intensity, in which case the first derivative of
the image would result in thick edges because the derivatives nonzero along a ramp.
• On the other hand, the second derivative would produce a double edge one pixel thick, separated by
zeros. From this, we conclude that the second derivative enhances fine detail much better than the
first derivative, a property ideally suited for sharpening images.
By MT