Module II
Image Enhancement in the Spatial Domain: Some Basic Gray Level Transformations, Histogram
Processing, Enhancement Using Arithmetic/Logic Operations, Basics of Spatial Filtering,
Smoothing Spatial Filters, Sharpening Spatial Filters, Combining Spatial Enhancement Methods.
Some Basic Gray Level Transformations
    Image enhancement is the most fundamental and simple process of digital image processing.
         In this process, the intensity level of an image is manipulated to get a better output image.
    For this purpose, we will use the mathematical operation of gray level transformation, also
         known as intensity transformation.
    Image enhancement in the spatial domain is based on the following expression:
                                           g(x. y) = T[f(x, y)]
    where, f (x, y) is the input image, g(x, y) is the output image and T is an operator on f
         defined over the neighborhood of point (x, y). The neighborhood of an arbitrary point (x, y)
    The simplest and the smallest possible of all the neighborhoods of a point is 1 X 1.
    When the mapping is defined over the 1 X 1 neighborhood of points, then the mapping is
         said to be Gray level (or Intensity) Transformation (or mapping).
    The output image, g depends on the value of f at a single point (x, y) and T alone.
    The gray level transformation is given by the expression:
                                                 s = T(r)
where, s and r is the intensity value of g and f value of the point (x, y)
Linear Transformation and Image Enhancement
    Gray level transformation can be linear or non-linear operation. Linear transformation
         possesses the property of homogeneity and additivity.
    Linear gray level transformation techniques which are used in image enhancement.
Basic Intensity Transformation Function
    Gray level transformation is one of the simplest image processing techniques. In digital
         image processing, the transformation functions are stored in a table.
    The mappings are implemented via table lookups. For an n-bit image, a lookup table will
         have 2n entries.
    There are three basic types of intensity functions used in gray level transformation:
         Negative Image
         Log Transformation
         Gamma Transformation
Negative Image
    The negative of a image having intensity range [0, L - 1] can be found using the following
      transformation:
                                         s = T(r) = L - r - 1
    This technique is used to enhance white or light color detail in dark background, as dark
      color in light background is easily visible than light color in dark background.
Log Transformation
                               The log transformation is of the form:
                                       s = T(r) = c log (1 + r)
                                   where, c is constant and r ≥ 0.
Log Transformation
    In log transformation, the higher range of intensity level is mapped to a lesser range of
      intensity level at the brighter side, whereas the lesser range of intensity level is mapped to
      higher range of intensity level at the darker side.
    The log transformation expands the dark pixels. Log transformation is applied when the
      intensity levels are very large, for example, 0 to 10^6. This is because transformation
      compresses the intensity levels of input level.
      The inverse logarithmic transformation is called exponential transformation. It has the
         opposite behaviour that of logarithmic transformation.
          The higher range of intensity level is mapped to higher range of intensity level at the
         brighter side, whereas the lesser range of intensity level is mapped to a lesser range of
         intensity level at the darker side.
  Power Law (Gamma) Transformation
  The power law (or gamma) transformation is of the form:
                                               s = T(r) = c * r ^ γ
  where, c, γ are constants; c, γ > 0
      For various values of γ, there exists a different power law transformation.
      At γ < 1, the higher range of intensity level is mapped to a lower range of intensity level at
         the brighter side, while the lower range of intensity level is mapped to higher range of
         intensity level at the darker side.
      At γ > 1, the behaviour is opposite that of γ < 1. Here, the higher range of intensity level is
         mapped to a higher range of intensity level at the brighter side, while the lower range of
         intensity level is mapped to lower range of intensity level at the darker side. If γ = 1, then
         the transformation becomes identity transformation.
Histogram Processing:
    In digital image processing, the histogram is used for graphical representation of a digital image.
      A graph is a plot by the number of pixels for each tonal value.
    Nowadays, image histogram is present in digital cameras. Photographers use them to see the
      distribution of tones captured.
    In a graph, the horizontal axis of the graph is used to represent tonal variations whereas the
      vertical axis is used to represent the number of pixels in that particular pixel.
    Black and dark areas are represented in the left side of the horizontal axis, medium grey color is
      represented in the middle, and the vertical axis represents the size of the area.
Applications of Histograms
       1. In digital image processing, histograms are used for simple calculations in software.
       2. It is used to analyze an image. Properties of an image can be predicted by the detailed study
           of the histogram.
       3. The brightness of the image can be adjusted by having the details of its histogram.
       4. The contrast of the image can be adjusted according to the need by having details of the x-
           axis of a histogram.
       5. It is used for image equalization. Gray level intensities are expanded along the x-axis to
           produce a high contrast image.
       6. Histograms are used in thresholding as it improves the appearance of the image.
Histogram Processing Techniques
Histogram Sliding
    In Histogram sliding, the complete histogram is shifted towards rightwards or leftwards.
    When a histogram is shifted towards the right or left, clear changes are seen in the brightness of
      the image.
    The brightness of the image is defined by the intensity of light which is emitted by a particular
      light source.
Histogram Stretching
    In histogram stretching, contrast of an image is increased.
    The contrast of an image is defined between the maximum and minimum value of pixel
      intensity.
    To increase the contrast of an image, histogram of that image will be fully stretched and covered
      the dynamic range of the histogram.
    From histogram of an image, we can check that the image has low or high contrast.
Histogram Equalization
    Histogram equalization is used for equalizing all the pixel values of an image. Transformation is
      done in such a way that uniform flattened histogram is produced.
    Histogram equalization increases the dynamic range of pixel values and makes an equal count of
      pixels at each level which produces a flat histogram with high contrast image.
    While stretching histogram, the shape of histogram remains the same whereas in Histogram
      equalization, the shape of histogram changes and it generates only one image.
    Consider for a moment continuous intensity values and let the variable r denote the intensities of
      an image to be processed.
      that produce an output intensity level s for every pixel in the input image having intensity r.
 (a) Monotonically increasing function, showing how multiple values can map to
                                           a single value.
   (b) Strictly monotonically increasing function. This is a one-to-one mapping,
                                             both ways.
 Monotonic transformation function performs a one-to-one or many-to one mapping.
 This is perfectly fine when mapping from r to s. The intensity levels in an image may be viewed
   as random variables in the interval [0, L-1].
 A fundamental descriptor of a random variable is its probability density function (PDF).
 Let pr(r) and ps(s) denote the PDFs of r and s, respectively, where the subscripts on p are used to
   indicate that pr and ps are different functions in general.
 A fundamental result from basic probability theory is that if p r(r) and T(r) are known, T(r) is
   continuous and differentiable over the range of values of interest, then the PDF of the
   transformed (mapped) variable s can be obtained using the simple formula:
 The right side of this equation is recognized as the cumulative distribution function (CDF) of
   random variable r.
 Because PDFs always are positive and recalling that the integral of a function is the area under
   the function. To find ps(s) the corresponding to the transformation:
Local Histogram Processing
    The histogram processing methods are global, in the sense that pixels are modified by a
      transformation function based on the intensity distribution of an entire image.
    Although this global approach is suitable for overall enhancement, there are cases in which it is
      necessary to enhance details over small areas in an image.
    The number of pixels in these areas may have negligible influence on the computation of a
      global transformation whose shape does not necessarily guarantee the desired local
      enhancement.
    The solution is to devise transformation functions based on the intensity distribution in a
      neighborhood of every pixel in the image.
    The histogram processing techniques previously described are easily adapted to local
      enhancement.
    The procedure is to define a neighborhood and move its center from pixel to pixel. At each
      location, the histogram of the points in the neighborhood is computed and either a histogram
      equalization or histogram specification transformation function is obtained.
    This function is then used to map the intensity of the pixel centered in the neighborhood.
Enhancement Using Arithmetic/Logic Operations
     Arithmetic operations between images play a crucial role in image processing.
    These operations are carried out on corresponding pixel pairs and involve addition, subtraction,
      multiplication, and division.
    Arithmetic operations in image processing involve manipulating pixel values by performing
      mathematical operations on corresponding pixels of two images.
    The four basic arithmetic operations are addition, subtraction, multiplication, and division.
    These operations can be used to enhance images, reduce noise, and perform other
      transformation techniques.
 Addition of Images
    The addition of images is a simple process where corresponding pixel values of two images are
      added together.
    However, there are certain rules for handling the result. If the result is a floating-point number,
      it needs to be rounded off.
    If the result exceeds the pixel range (0 to 255 in an 8-bit grayscale image), the maximum range
      value is selected.
    If the result is below the pixel range, the minimum range value is chosen. Addition of images is
      commonly used for noise reduction and image averaging.
Subtraction of Images
    Subtraction of images involves subtracting corresponding pixel values from one another.
    Similar to addition, the result needs to be handled Based on certain rules. If the result is below
      zero, it is set to zero.
    Subtraction is commonly used for image enhancement, detecting differences between images,
      and in medical imaging for mask mode radiography.
Multiplication of Images
    Multiplication of images is a process where corresponding pixel values are multiplied together.
    Similar to addition and subtraction, the result needs to be managed. If the result exceeds the
      pixel range, it is set to the maximum range value (255).
    Multiplication is used for shading correction and masking or region of interest operations.
Division of Images
    Division of images involves dividing corresponding pixel values. As with the other operations,
      certain rules.
    If the result is undefined or infinity, it is set to zero. Division is primarily used for shading
      correction.
Applications of Arithmetic Operations
    Arithmetic operations have several applications in image processing.
    Addition of images is used for noise reduction and image averaging, while subtraction is used
       for image enhancement and detecting differences between images.
    Multiplication is employed for shading correction and masking, and division is primarily used
       for shading correction.
Logical Operations on Images
    In addition to arithmetic operations, logical operations such as AND, OR, and NOT can also be
       applied to images.
    These operations are performed pixel by pixel and are commonly used in digital logic and
       design.
    The AND operation outputs a pixel value of 1 only when both inputs are 1, the OR operation
       outputs a pixel value of 1 if any of the inputs are 1, and the NOT operation produces the
       inverse of the input pixel value.
AND Operation
    The AND operation is applied to corresponding pixel pairs of two images.
    If both pixels are 1, the output is 1; otherwise, the output is 0. The AND operation is useful for
       extracting common features from multiple images.
OR Operation
    The OR operation is also performed on corresponding pixel pairs. If any of the two pixels are
       1, the output is 1; otherwise, it is 0.
    The OR operation is useful for combining images with different features or detecting specific
       objects.
NOT Operation
    The NOT operation is performed on a single image, inverting the pixel values. If the input
       pixel is 0, the output will be 1, and vice versa.
    The NOT operation can be employed for image manipulation and conversion.
Basics of Spatial Filtering
   Spatial filtering is one of the principal tool is used in this field for a broad spectrum of
     applications, so it is highly advisable that you develop a solid understanding of these concepts.
Mechanics of Spatial Filtering
 Filtering creates a new pixel with coordinates equal to the coordinates of the center of the
   neighborhood, and whose value is the result of the filtering operation.
 A processed (filtered) image is generated as the center of the filter visits each pixel in the input
   image.
 If the operation performed on the image pixels is linear, then the filter is called a linear spatial
   filter.
 The mechanics of linear spatial filtering using a 3*3 filter mask. The form chosen to denote the
   coordinates of the filter mask coefficients simplifies writing expressions for linear filtering.
 The mechanics of linear spatial filtering using a 3*3 neighborhood. At any point (x, y) in the
   image, the response, g (x, y) , of the filter is the sum of products of the filter coefficients and the
   image pixels encompassed by the filter:
Spatial Correlation and Convolution
    There are two closely related concepts that must be understood clearly when performing linear
       spatial filtering. One is correlation and the other is convolution.
    Correlation is the process of moving a filter mask over the image and computing the sum of
       products at each location.
    The mechanics of convolution are the same, except that the filter is first rotated by 180°.
    Spatial correlation refers to the relationship between pixel values at different spatial locations in
       an image. It essentially measures how the intensity values of one part of the image are related to
       the intensity values of another part. In image processing, this is often used to detect patterns,
       similarities, or redundancies in an image.
    Mathematically, the spatial correlation is expressed as the product of the pixel values in one
       region and a filter or mask (often called a kernel) applied to another region.
      This operation is performed across the entire image, typically to find features like textures or
       edges.
    The filter "slides" over the image, and the result is a new image showing the correlated areas.
 Convolution
    Convolution is a more specific operation that is closely related to correlation. In convolution, an
       image is processed with a kernel or filter to extract certain features, such as edges, blurs, or
       sharpened details. The key difference between convolution and correlation is that in convolution,
       the kernel is flipped both horizontally and vertically before being applied to the image.
    The process of convolution involves the following steps:
       1. The kernel (or filter) slides over the image.
       2. At each position, the pixel values in the image are multiplied by the corresponding values in
       the kernel.
       3. The results of the multiplications are summed up to produce a single output pixel.
       4. This process is repeated for all pixels in the image.
    Convolution is widely used for operations like edge detection, blurring and sharpening .
Vector Representation of Linear Filtering
   When interest lies in the characteristic response, R, of a mask either for correlation or
     convolution, it is convenient sometimes to write the sum of products as
   where the s are the coefficients of an m*n filter and the zs are the corresponding image
     intensities encompassed by the filter
Generating Spatial Filter Masks
   Generating an m*n linear spatial filter requires that we specify mn mask coefficients.
   In turn, these coefficients are selected based on what the filter is supposed to do, keeping in
     mind that all we can do with linear filtering is to implement a sum of products.
   For example, suppose that we want to replace the pixels in an image by the average intensity of a
     3*3 neighborhood centered on those pixels. The average value at any location (x, y) in the
     imageis the sum of the nine intensity values in the 3*3 neighborhood centered on (x, y) divided
     by 9.
   Letting zi,i=1,2,3,…9, denote these intensities, the average is
   Generating a nonlinear filter requires that we specify the size of a neighborhood and the
     operation(s) to be performed on the image pixels contained in the neighborhood.
Smoothing Spatial Filters
   Smoothing filters are used for blurring and for noise reduction.
   Blurring is used in preprocessing tasks, such as removal of small details from an image prior to
     (large) object extraction, and bridging of small gaps in lines or curves.
   Noise reduction can be accomplished by blurring with a linear filter and also by nonlinear
     filtering.
Smoothing Linear Filters
    The output (response) of a smoothing, linear spatial filter is simply the average of the pixels
       contained in the neighborhood of the filter mask.
    These filters sometimes are called averaging filters. The idea behind smoothing filters is
       straightforward.
    By replacing the value of every pixel in an image by the average of the intensity levels in the
       neighborhood defined by the filter mask, this process results in an image with reduced “sharp”
       transitions in intensities.
    Because random noise typically consists of sharp transitions in intensity levels, the most
       obvious application of smoothing is noise reduction.
    However, edges (which almost always are desirable features of an image) also are
       characterized by sharp intensity transitions, so averaging filters have the undesirable side effect
       that they blur edges.
    Another application of this type of process includes the smoothing of false contours that result
       from using an insufficient number of intensity levels.
Two 3*3 smoothing (averaging) filter masks
    The idea here is that it is computationally more efficient to have coefficients valued 1. At the
          end of the filtering process the entire image is divided by 9.
    An m*n mask would have a normalizing constant equal to1/ mn.
    A spatial averaging filter in which all coefficients are equal sometimes is called a box filter. The
          second mask is a little more interesting.
    This mask yields a so called weighted average, terminology used to indicate that pixels are
          multiplied by different coefficients, thus giving more importance (weight) to some pixels at the
          expense of others.
Order-Statistic (Nonlinear) Filters
  Order-statistic filters are nonlinear spatial filters whose response is based on ordering (ranking) the
  pixels contained in the image area encompassed by the filter and then replacing the value of the
  center pixel with the value determined by the ranking result.
  The best-known filter in this category is the median filter, which, as its name implies, replaces the
  value of a pixel by the median of the intensity values in the neighborhood of that pixel (the original
  value of the pixel is included in the computation of the median).
  Median filters are quite popular because, for certain types of random noise, they provide excellent
  noise-reduction capabilities, with considerably less blurring than linear smoothing filters of similar
  size.
  Median filters are particularly effective in the presence of impulse noise, also called salt-and-
  pepper noise because of its appearance as white and black dots superimposed on an image.
Sharpening Spatial Filters
 The principal objective of sharpening is to highlight transitions in intensity.
 Uses of image sharpening vary and include applications ranging from electronic printing and
    medical imaging to industrial inspection and autonomous guidance in military systems.
 Image blurring could be accomplished in the spatial domain by pixel averaging in a neighborhood.
    Because averaging is analogous to integration, it is logical to conclude that sharpening can be
    accomplished by spatial differentiation.
 Fundamentally, the strength of the response of a derivative operator is proportional to the degree of
    intensity discontinuity of the image at the point at which the operator is applied.
  Thus, image differentiation enhances edges and other discontinuities (such as noise) and
     deemphasizes areas with slowly varying intensities.
Foundation
   The derivatives of a digital function are defined in terms of differences.
   There are various ways to define these differences.
   However, we require that any definition we use for a first derivative (1) must be zero in areas of
      constant intensity; (2) must be nonzero at the onset of an intensity step or ramp; and (3) must be
      non zero along ramps.
   Similarly, any definition of a second derivative (1) must be zero in constant areas; (2) must be
      nonzero at the onset and end of an intensity step or ramp; and (3) must be zero along ramps of
      constant slope.
   Because we are dealing with digital quantities whose values are finite, the maximum possible
      intensity change also is finite, and the shortest distance over which that change can occur is
      between adjacent pixels.
   A basic definition of the first-order derivative of a one-dimensional function f(x) is the difference
second-order derivative of f(x) as the difference
    When computing the first derivative at a location x, we subtract the value of the function at that
       location from the next point. So this is a “look-ahead” operation.
    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 derivative is nonzero along a
       ramp. On the other hand, the second derivative would produce a double edge one pixel thick,
       separated by zeros.
    From this, that the second derivative enhances fine detail much better than the first derivative, a
       property that is ideally suited for sharpening images.
Using the Second Derivative for Image Sharpening—The Laplacian
    Isotropic filters are rotation invariant, in the sense that rotating the image and then applying the
       filter gives the same result as applying the filter to the image first and then rotating the result.
    It follows from the preceding three equations that the discrete Laplacian of two variables is
    This equation can be implemented using the filter mask in which gives an isotropic result for
      rotations in increments of 90°.
    Because the Laplacian is a derivative operator, its use highlights intensity discontinuities in an
      image and deemphasizes regions with slowly varying intensity levels.
    This will tend to produce images that have grayish edge lines and other discontinuities, all
      superimposed on a dark, featureless background.
 (a) Filter mask used to implement(b) Mask used to implement an extension of this equation that
     includes the diagonal terms. (c) and (d) Two other implementations of the Laplacian found
                                          frequently in practice.
Unsharp Masking and High boost Filtering
    A process that has been used for many years by the printing and publishing industry to sharpen
       images consists of subtracting an unsharp (smoothed) version of an image from the original
       image. This process, called unsharp masking, consists of the following steps:
1. Blur the original image.
2. Subtract the blurred image from the original (the resulting difference is called the mask.
3. Add the mask to the original.
    Letting f (x, y) denote the blurred image, unsharp masking is expressed in equation form as
       follows. First we obtain the mask:
    Then we add a weighted portion of the mask back to the original image:
       1-D illustration of the mechanics of unsharp masking. (a) Original signal. (b) Blurred
        signal with original shown dashed for reference. (c) Unsharp mask. (d) Sharpened
                                   signal, obtained by adding (c) to (a).
    The points at which a change of slope in the intensity occurs in the signal are now emphasized
       (sharpened).
    Observe that negative values were added to the original.
    Thus, it is possible for the final result to have negative intensities if the original image has any
       zero values or if the value of k is chosen large enough to emphasize the peaks of the mask to a
       level larger than the minimum value in the original.
    Negative values would cause a dark halo around edges, which, if k is large enough, can produce
       objectionable results.
Using First-Order Derivatives for (Nonlinear) Image Sharpening—The Gradient
    First derivatives in image processing are implemented using the magnitude of the gradient.
    For a function f(x,y) , the gradient of f at coordinates (x, y) is defined as the two-dimensional
       column vector.
      This vector has the important geometrical property that it points in the direction of the greatest
        rate of change of f at location (x, y).
      The magnitude (length) of vector
      Because the components of the gradient vector are derivatives, they are linear operators.
      However, the magnitude of this vector is not because of the squaring and square root operations.
      The computations of gx and gy are linear operation because they involve derivatives and,
        therefore, can be implemented as a sum of products using the spatial masks.
      The nonlinear aspect of sharpening with the gradient is the computation of M(x, y) involving
        squaring and square roots, or the use of absolute values, all of which are nonlinear operations.
      These operations are performed after the linear process that yields gx and gy.
Combining Spatial Enhancement Methods
      Our objective is to enhance this image by sharpening it and by bringing out more of the skeletal
        detail.
      The narrow dynamic range of the intensity levels and high noise content make this image
        difficult to enhance.
      The strategy we will follow is to utilize the Laplacian to highlight fine detail, and the gradient to
        enhance prominent edges.
      Combining spatial enhancement methods in digital image processing refers to the practice of
        applying multiple spatial filtering techniques (like edge detection, sharpening, contrast
        adjustment) to an image sequentially, often by combining the results of each filter to achieve a
        more comprehensive image enhancement that addresses different aspects of the image quality,
        such as highlighting edges while preserving overall detail and contrast.
Combining spatial enhancement methods:
    Addressing different image features:
       Different spatial filters are designed to emphasize specific features in an image, like edges,
          textures, or smooth regions.
       By combining them, it can target multiple aspects of the image simultaneously.
   Sequential application:
       Typically, you apply one spatial filter to the image, then apply another filter to the output of the
          first, and so on, allowing the results of each filter to influence subsequent operations.
       Examples of combinations:
             Smoothing followed by sharpening: Blurring an image slightly with a low-pass filter can
              reduce noise before applying a sharpening filter to enhance edges.
             Edge detection with contrast enhancement: Applying an edge detection filter to identify
              edges, then boosting the contrast in those edge areas to make them more prominent.
             Histogram equalization with local enhancement: Adjusting the overall image intensity
              distribution using histogram equalization, then applying a local enhancement technique to
              specific areas of interest.
Combine spatial enhancement methods:
    Direct addition:
       Simply add the outputs of different filters together after applying them to the image.
    Weighted addition:
       Assign different weights to the outputs of different filters depending on their importance for the
          desired enhancement outcome.
    Masking:
       Create a mask that defines the regions where specific filters should be applied more strongly.
Important considerations:
    Filter selection:
       Choosing appropriate filters based on the image characteristics and desired enhancement goals is
          crucial.
    Parameter tuning:
       Adjusting filter parameters (like kernel size, sigma value) can significantly impact the results.
    Evaluation:
       Assessing the quality of the enhanced image subjectively (visual inspection) or objectively
          (using image quality metrics) is important to determine if the combination of filters is effective.