## Chapter 4

## Image Enhancement

##### CHAPTER OBJECTIVES

- Learn about various spatial filtering techniques.
- To understand the popular and widely used approaches in spatial and frequency domain enhancement techniques.
- To illustrate how to apply pseudo color image processing for image enhancement.
- Learn to enhance the total image appearance by considering bit plane slicing technique.

##### 4.1 INTRODUCTION

In the first chapter, we have described the fundamental steps involved in digital image processing. The various steps are image acquisition, preprocessing, segmentation, representation and description, recognition, and interpretation. Before we proceed with the segmentation process it is necessary to condition the image. The conditioning of the image can be carried out by preprocessing. One of the preprocessing techniques is image enhancement.

Image enhancement technique is defined as a process of an image processing such that the result is much more suitable than the original image for a ‘specific’ application. The word specific is important because the method that is more useful for an application (say for X-ray images) may not be suitable for another application (say pictures of Mars transmitted by space probe).

The image enhancement approaches can be put into two broad categories and they are

- Spatial domain approach
- Frequency domain approach

In the spatial domain approach the pixels of an image are manipulated directly. The frequency domain approach is mainly based on modifying the Fourier transform of an image. The enhancement techniques based on various combinations of methods are given in this chapter. Section 4.2 describes the basic ideas of spatial domain and frequency domain methods. Section 4.3 deals with enhancement techniques under the point processing categories. Section 4.4 deals with the enhancement methods based on mask processing. In Section 4.5 we discuss the enhancement techniques using Fourier transform. The concluding Section 4.6 discusses the enhancement techniques for color images. Tree diagram representation of image enhancement techniques is given in Figure 4.1.

##### 4.2 SPATIAL DOMAIN AND FREQUENCY DOMAIN APPROACHES

In the spatial domain method, the pixel composing of image details are considered and the various procedures are directly applied on these pixels. The image processing functions in the spatial domain may be expressed as

where *f*(*x, y*) is the input image, *g*(*x, y*) is the processed output image and *T* represents an operation on ‘ *f ’* defined over some neighborhood of (*x, y*). Sometimes *T* can also be used to operate on a set of input images. Consider an image representation shown in Figure 4.2.

**FIGURE 4.1** Tree diagram for image enhancement techniques

In Figure 4.2, a subimage of size (3 × 3) about a point (*x, y*) is given. The center of the subimage is moved from pixel to pixel starting at the top left corner and applying the operator at each location (*x, y*) to give the output image *‘g’* at that location. The subimage considered may be circle, square, or rectangular arrays.

If we consider the simplest case, where the neighborhood is(1 * 1), the output image *g* depends only on the value of *f*at (*x, y*) and *T* is called a *gray level transformation function.* The gray level transformation function will then be given in the form

where, *r* and *s* are variables denoting the gray level of *f*(*x, y*) and *g*(*x, y*) at any point (*x, y*).

**FIGURE 4.2** Digital image processing

If *T*(*r*) is of the form shown in Figure 4.3(a) the effect of this transformation is to produce an image of higher contrast compared to the original image by darkening the levels below *m* and brightening the levels above *m* in the original image. This approach is called contrast stretching, because the values of *r,* below *m* are compressed, and the opposite effect takes place for the values above *m.*

**Contrast Stretching:** An enhancement technique used to increase the dynamic range of gray levels in the image being processed.

Figure 4.3(b) shows a typical case of transfer function *T*(*r*) that produces a binary image. In general, the methods just described use a transfer function to produce the gray level in output image at the location (*x, y*) that depends only on the gray level of the input image at that location. These techniques are often referred to as point processing techniques. The larger neighborhoods allow a variety of processing function that go beyond just image enhancement. The neighborhood of pixels considered about the center pixel (*x, y*) is called as mask, template, or window. A mask is a small two-dimensional array (say, (3 × 3)) shown in Figure 4.2. The enhancement techniques based on this type are often called as mask processing or filtering.

**FIGURE 4.3** A typical case of transfer function

#### 4.2.1 Frequency Domain Techniques

The convolution theorem is the basis for the frequency domain approaches. Let *G*(*x, y*) be an image formed by the convolution of the image *f*(*x, y*) and a linear position invariant operator *H*(*x, y*) and is given by,

where * represents the convolution operation.

Then, from the convolution theorem, the equation 4.3 can be written in the frequency domain as,

In the linear system theory, the transform *H*(*u, v*) is called the *Transfer function.* The various image enhancement problems can be expressed in the form of equation (4.3). In a typical image enhancement application, the image *f*(*x, y*) is given and the objective after the computation of *F*(*u, v*) is to select *H*(*u, v*) so that the desired image can be given by the equation

This equation shows some highlighted feature of the original image *f*(*x*, *y*). For example, the edges in *f*(*x, y*) can be highlighted using a function *H*(*u, v*) that emphasize the high frequency component of *F*(*u, v*).

Figure 4.4 illustrates the various steps involved in the enhancement approach based on frequency domain.

**FIGURE 4.4** Enhancement steps in frequency domain approaches

##### 4.3 SPATIAL DOMAIN TECHNIQUES

In this section, the image enhancement techniques based only on the intensity of single pixel is considered. The single point processes are the simplest among the various image enhancement techniques.

Let us consider *‘r’* denotes the intensity of pixel in the given original image and *‘s’* represents the intensity of the pixels in the enhanced image.

#### 4.3.1 Negative of an Image

There are a number of applications in which negative of the digital images are quiet useful. For example, displaying of medical images and photographing a screen with monochrome positive film with the idea of using the resulting negatives as normal slides. The negative of the digital image is obtained by using the transformation function *s* = *T* (*r*) and it is shown in Figure 4.5(a), where *L* is the number of gray levels. The idea is to reverse the order from black to white so that the intensity of the output image decreases as the intensity of the input increases. The transformation function given in Figure 4.5(a) is applied to the original Lena image shown in Figure 4.5(b). The resulting negative of the original image is shown in Figure 4.5(c).

**FIGURE 4.5** (a) Gray level transformation function (b) Original image (c) Negative of the original image

**//program to display negative of an image**

#include "stdafx.h"

#include "ImgProc.h"

#include "fstream.h"

#include "ImgProcDoc.h"

#include "ImgProcView.h"

#pragma pack(1)

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

BYTE ImgType=1, graymin=50,graymax=100,bits=0;

DWORD w=0,h=0;

typedef BYTE PIXEL;

PIXEL org[1024][1024];

BYTE r[256],g[256],b[256];

DWORD pal[256];

BOOL CImgProcView::PreCreateWindow(CREATESTRUCT & cs)

{

ifstream fin;

fin.open("lena.bmp",ios::in|ios::binary);

/* The above statement opens lena image */

fin.seekg(18,ios::beg); /* Move to the 18^{th} offset */

fin.read((char*)&w,sizeof(w)); /* Read image width */

fin.read((char*)&h,sizeof(h)); /* Read image height */

fin.seekg(28,ios::beg);

fin.read(&bits,1);

fin.seekg(54,ios::beg);/* Move to beginning of pixels */

for(int i=0;i<256;i++) /*Read all pixels and store in pal[i]*/

{

pal[i]=(DWORD)(fin.get())<<16|(WORD)(fin.get())<<8

fin.getfin.get();

}

for(DWORD m=0;m<h;m++)

{

for(DWORD n=0;n<w;n++)

{

org[n][h-m] = fin.get();}

if(fin.fail()) PostQuitMessage(0);

}

if(w%4)

for(int dif=4–w%4;dif>0;dif––)

fin.get();

}

fin.close();

return CView::PreCreateWindow(cs);

}

void CImgProcView::OnDraw(CDC*pDC)/*Function to draw in VC++*/

{

switch(ImgType)

{

// **Original image**

case 1: /* Code to display original image */

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC → SetPixel(i,j,pal[org[i][j]]);

break;

}

/ / **Negative of an image**

case 2:/* Code to display negative of input image */

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

/* Original gray value is subtracted from 255

to get negative pixel */

pDC → SetPixel(i,j,pal[255-org[i][j]]);

break;

}

}

}

/* Function to display original image */

void CImgProcView::OnOriginal()

}

ImgType=1;

this → RedrawWindow();

}

Function to display negative image */

void CImgProcView::OnTransformNegative()

{

ImgType=2;

this → RedrawWindow();

}

#### 4.3.2 Contrast Stretching

Sometimes during image acquisition low contrast images may result due to one of the following reasons:

- poor illumination
- lack of dynamic range in the image sensor and
- wrong setting of the lens aperture.

The idea behind the contrast stretching is to increase the dynamic range of gray levels in the image being processed. Figure 4.6(a) shows a typical transformation function used for contrast stretching.

The location of the points (*r*_{1}, *s*_{1}) and (*r*_{2}, *s*_{2}) control the shape of the transformation function. If *r*_{1} = *r*_{2}, *s*_{1} = 0, and *s*_{2} = *L* − 1, the transformation becomes the thresholding function and creates a binary image. Intermediate values of *r*_{1}, *s*_{1}, and *r*_{2}, *s*_{2} produces various degree of spread in the gray levels at the output image, thus affecting its contrast.

Figure 4.6(b) shows an 8-bit image with low contrast and 4.6(c) shows the result of contrast stretching. Figure 4.6(d) shows the result of thresholding the original image. The thresholding level is *r* = 128, with the output set at 255 for any gray level in the input image of 128 or higher and at 0, for all other values.

#### 4.3.3 Gray Level Slicing

There are numerous applications in which highlighting a specific range of gray levels in an image is often required. For example, enhancing the flaws in X-ray images and enhancing features such as masses of water in satellite imagery. There are two basic approaches for doing gray level slicing.

**Gray Level Slicing:** An enhancement technique in which all the gray levels in the range of interest are displayed using high values and all other gray levels are displayed using low gray levels.

In the first approach, all the gray levels in the range of interest are displayed using a high value and all other gray values are displayed using low values. The corresponding transformation function used is shown in Figure 4.7(a) and this results in a binary image.

**FIGURE 4.6** (a) A form of transformation function (b) Low-cost image (c) Image after contrast stretching (d) Thresholded image

The second approach is based on transformation function shown in Figure 4.7(b). This transfer function brightens the desired range of gray levels but preserves the background and the gray level tonalities in the image. Figure 4.7(c) shows the original image and 4.7(d) shows the image resulted after applying the gray level slicing using the transformation function shown in Figure 4.7(a).

**FIGURE 4.7** Transformation function. (a) For a range of pixels of interest (b) For a range of pixels of interest and preserving background gray levels (c) Original image (d) Resulting image after applying transformation function shown in Figure 4.7(a)

**/ / program for transformation of an image and gray level slicing**

#include "stdafx.h"

#include "ImgProc.h"

#include "fstream.h"

#include "ImgProcDoc.h"

#include "ImgProcView.h"

#pragma pack(1)

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[]= __FILE__;

#endif

BYTE ImgType=1,graymin=50,graymax=100,bits=0;

DWORD w=0,h=0;

typedef BYTE PIxEL;

PIxEL org[1024][1024];

BYTE r[256],g[256],b[256];

DWORD pal[256];

BOOL CImgProcView::PreCreateWindow(CREATESTRUCT& cs)

{

ifstream fin;

**/* Code to open lena image */**

fin.open("lena.bmp",ios::in|ios::binary);

fin.seekg(18,ios::beg);

**/* Code to read the width of lena image */**

fin.read((char*)&w,sizeof(w));

**/* Code to read the height of lena image */**

fin.read((char*)&h,sizeof(h));

fin.seekg(28,ios::beg);

fin.read(&bits,1);

fin.seekg(54,ios::beg);

for(int i=0;i<256;i++)

{

pal[i]=(DWORD)(fin.get())<<16 | (WORD)(fin.get())<<8 |fin.get();

fin.get();

}

for(DWORD m=0;m<h;m++)

{

for(DWORD n=0;n<w;n++)

{

org[n][h-m]=fin.get();

if(fin.fail()) PostQuitMessage(0);

}

if(w%4)

for(int dif=4–w%4;dif>0;dif––)

fin.get();

}

fin.close();

// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);

}

switch(ImgType)

{

case 1: /* Code to display original image */

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC → SetPixel(i,j,pal[org[i][j]]);break;

}

// **Transformation of an image**

case 2:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC → SetPixel(i,j,org[i][j]>128?pal[255]:pal[0]);

break;

}

//Intensity slicing

case 10:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC → SetPixel(i,j,(org[i][j]>=graymin &&

org[i][j]<=graymax)?pal[200]:pal[20]); break;

}

case 11

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC → SetPixel(i,j,(org[i][j]>=graymin &

org[i][j]<=graymax)?pal[200]:pal[org[i][j]]);break;

}

}

}

**/* Functions to draw image according to user choice */**

void CImgProcView::OnOriginal()

{

ImgType=1;

this → RedrawWindow();

}

void CImgProcView::OnTransformThresholding()

{

// TODO: Add your command handler code here

ImgType=13;

this → RedrawWindow();

}

void CImgProcView::OnGrayscalesliceMethod1()

{

ImgType=10;

this → RedrawWindow();

}

void CImgProcView::OnGrayscalesliceMethod2()

{

ImgType=11;

this → RedrawWindow();

}

#### 4.3.4 Bit Plane Slicing

Instead of highlighting the intensity ranges sometimes it is desired to highlight the contribution made to total image appearance by considering the specific bits of the image. We know that each pixel in the image is represented by 8 bits. Imagine that the image is composed of eight 1-bit planes corresponding to the 8 bits ranging from plane 0 for the least significant bit and plane 7 for the most significant bit. Plane 0 contains all the lower order bits in the bytes comprising the pixels in the image and plane 7 contains all the higher order bits. This idea is illustrated in the Figure 4.8. Figure 4.9 shows the original image and the Figure 4.10(a)–(h) shows the various bit planes of the image shown in Figure 4.9.

**FIGURE 4.8** Bit-plane representation of an 8-bit image

**FIGURE 4.9** Original image (*Source:* Signal and Image Processing Institute, University of California)

Note that only the five highest order bits contain visually significant data. The other bit planes contribute to only less details in the image. It is also worth mentioning that the plane 7 shown in Figure 4.10(a) can also be obtained by thresholding the image of the gray level 128. The other plane images shown in Figures (g), (f), (e), (d), (c), (b) and (a) are obtained by thresholding the original image at gray levels 64, 32, 16, 8, 4, 2 and 1, respectively.

**FIGURE 4.10(a–h) Bit planes for image shown in Figure 4.9**

/ / **program for bit plane slicing**

#include "stdafx.h"

#include "ImgProc.h"

#include "fstream.h"

#include "ImgProcDoc.h"

#include "ImgProcView.h"

#pragma pack(1)

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

BYTE ImgType=1,graymin=50,graymax=100,bits=0;

DWORD w=0,h=0;

/* ImgType

1 original 2 bit1 3 bit2 4 bit3 5 bit4 6 bit5

7 bit6 8 bit7 9 bit8 10 gray1 11 gray2 */

typedef BYTE PIXEL;

PIXEL org[1024][1024];

BYTE r[256],g[256],b[256];

DWORD pal[256];

BOOL CImgProcView::PreCreateWindow(CREATESTRUCT& cs)

{

ifstream fin;

**/* code to open lena image */**

fin.open("lena.bmp",ios::in|ios::binary);

fin.seekg(18,ios::beg);

fin.read((char*)&w,sizeof(w));

fin.read((char*)&h,sizeof(h));

fin.seekg(28,ios::beg);

fin.read(&bits,1);

fin.seekg(54,ios::beg);

for(int i=0;i<256;i++)

{

pal[i]=(DWORD)(fin.get())<<16 | (WORD)(fin.get())<<8

|fin.get(); |fin.get();

}

for(DWORD m=0;m<h;m++)

{

for(DWORD n=0;n<w;n++)

{

org[n][h-m]=fin.get();

if(fin.fail()) PostQuitMessage(0);

}

if(w%4)

for(int dif=4-w%4;dif>0;dif--)

fin.get();

}

fin.close();

// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);

}** /* Function to draw output screen */**

void CImgProcView::OnDraw(CDC* pDC)

{

CImgProcDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

///////////IMAGE PROCESSING AND DISPLAY///////////

switch(ImgType)

{

// **Original image**

case 1:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC→SetPixel(i,j,pal[org[i][j]]);break;

}

// **Bit plane slicing 1-8**

case 2:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC→SetPixel(i,j,pal[org[i][j]&0x1?255:0]);break;

}

case 3:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC→SetPixel(i,j,pal[org[i][j]&0x2?255:0]);

break;

}

case 4:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC→SetPixel(i,j,pal[org[i][j]&4?255:0]);break;

}

case 5:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC→SetPixel(i,j,pal[org[i][j]&8?255:0]);break;

}

case 6:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC→SetPixel(i,j,pal[org[i][j]&16?255:0]);break;

}

case 7:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC→SetPixel(i,j,pal[org[i][j]&32?255:0]);break;

}

case 8:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC→SetPixel(i,j,pal[org[i][j]&64?255:0]);break;

}

case 9:

{

for(DWORD i=0;i<h;i++)

for(DWORD j=0;j<w;j++)

pDC→SetPixel(i,j,pal[org[i][j]&128?255:0]);break;

}

}

**/* Functions to be based on user’s choice of bit plane */**

void CImgProcView::OnBitplaneslicesPlane2()

{

ImgType=3; **/* Bit plane 2 */**

this →RedrawWindow();

}

void CImgProcView::OnBitplaneslicesPlane3()

{

ImgType=4; **/* Bit plane 3 */**

this →RedrawWindow();

}

void CImgProcView::OnBitplaneslicesPlane4()

{

ImgType=5; **/* Bit plane 4 */**

this →RedrawWindow();

}

void CImgProcView::OnBitplaneslicesPlane5()

{

ImgType=6; **/* Bit plane 5 */**

this →RedrawWindow();

}

void CImgProcView::OnBitplaneslicesPlane6()

{

ImgType=7; **/* Bit plane 6 */**

this →RedrawWindow();

}

void CImgProcView::OnBitplaneslicesPlane7()

{

ImgType=8; **/* Bit plane 7 */**

this →RedrawWindow();

}

void CImgProcView::OnBitplaneslicesPlane8()

{

ImgType=9; **/* Bit plane 8 */**

this →RedrawWindow();

}

#### 4.3.5 Histogram and Histogram Equalization

**Histogram** The histogram of a digital image is the probability of occurrence associated with the gray levels in the range 0 to 255. It can be expressed using a discrete function

where *r _{k}* is the

*k*th gray level,

*n*is the number of pixels in the image with that gray level,

_{k}*n*is the total number of pixels in the image and

*k =*0, 1, 2,…, 255. In general,

*P (r*gives an estimate of the probability of occurrence of gray level

_{k})*r*. The plot of

_{k}*P(r*for all values of

_{k})*k*is called histogram of the image and it gives a global description of the appearance of an image. The histograms for four different types of images are shown in Figure 4.11.

**Histogram:** A plot between the probability associated with each gray level versus gray levels in the image. From this one can infer whether the given image is

- a dark image or
- bright image or
- low contrast or
- high contrast image.

The histogram shown in Figure 4.11(a) shows that the gray levels are concentrated towards the dark end of the gray scale range. Thus this histogram corresponds to an image with overall dark characteristics. Figure 4.11(b) shows the histogram for a bright image. Figures 4.11(c) and (d) are the histograms for low-contrast and high-contrast images, respectively. Thus the shape of the histogram gives useful information about the possibility for contrast enhancement. The following discussion is for image enhancement based on histogram manipulation.

**FIGURE 4.11** Histograms for four different types of images. (a) Dark image (b) Bright image (c) Low contrast image (d) High contrast image

**Histogram equalization** Let *r* be the variable representing the gray levels in the image to be enhanced. Assume that the gray levels in this image after normalization range from 0 to 1. For any value of *r* in the original image in the interval (0, 1) the transformation in the form

produces a gray level *s*. It is assumed that equation (4.7) satisfies the following two conditions:

*T(r)*is single–valued and monotonically increasing in the interval 0 ≤*r*≤ 1- 0 ≤
*T(r)*≤ 1 for 0 ≤*r*≤ 1.

The first condition preserves the order from black to white in the gray scale, whereas the second condition guarantees a mapping that is consistent with the allowed range of pixel values.

An example transfer function given in Figure 4.13 satisfies these conditions. The inverse transfer function from *s* back to *r* is given as

where *T*^{−1}(s) also satisfies the conditions (1) and (2) with respect to the variable *s*. The gray levels in an image may be viewed as random quantities in the interval (0,1). The original and transformed gray levels can be characterized by their probability density functions *P _{r}(r)* and

*P*respectively. If

_{s}(S),*P*and

_{r}(r)*T(r)*are known and

*T*satisfies condition (1), the probability density function of the transformed image gray levels is then given by

^{−1}(s)

We now discuss the approach which is based on modifying the appearance of an image by controlling the probability density function of its gray levels using the transformation function *T(r)* as shown in Figure 4.12.

Consider the transformation function,

The right side of equation (4.10) is called as cumulative distribution function (CDF) of *r*. The CDF satisfies the conditions (1) and (2) already stated. Differentiating equation (4.10) with respect to *r* results,

**FIGURE 4.12** Gray level transformation function

Substituting d*r*/d*s* into equation (4.9) yields

which is a uniform density in the interval [0,1] for the variable *s*. From this we infer that using the CDF as transformation function results in an image whose gray levels have a uniform density. In terms of enhancement this implies an increase in the dynamic range of pixels which can have a considerable effect on the appearance of the image.

The concepts discussed earlier can be illustrated using a simple example.

#### Example 1

Assume that the levels *r* have the probability density function shown in Figure 4.13(a).

From Figure 4.13(a) the equation for the function can be given as

**FIGURE 4.13** (a) Probability density *P _{r}(r)* function of the image (b) Transformation function (c) Resulting uniform density

From equation (4.14), *S = r*^{2}, differentiating the above equation

then,

which is a uniform density function in the desired range. Figure 4.13(b) shows the transformation function *T(r) = r*^{2} and Figure 4.13(c) shows *P _{s}(s) =* 1.

The concepts discussed so far must be formulated in discrete form so that it will be useful for digital image processing. For the gray levels in the discrete values and the probabilities associated with them can be given as

where 0 ≤ *r _{k} ≤* 1 and

*k =*0,1,2, 3,…,

*L −*1.

*L* is the number of levels, *P _{r}(r_{k})* is the probability of the

*k*th gray level,

*n*is the number of times this level appears in the image and

_{k}*n*is the total number of pixels.

The plot of *P _{r}(r_{k})* versus

*r*is called a histogram, and the technique used for obtaining the uniform histogram is known as

_{k}*histogram equalization*or

*histogram linearization.*

Then the discrete form of transformation is given by

The inverse transformation is denoted as

where both *T(r _{k})* and

*T*

^{−1}(

*s*) are assumed to satisfy the conditions (1) and (2), mentioned previously.

_{k}The histogram equivalization technique is applied to the original Lena image shown in Figure 4.14(a) (The colored figure is available here). The Figure 4.14(b) shows the histogram of the Lena image. Figure 4.14(c) is the enhanced Lena image obtained using histogram equivalization technique. The histogram of the enhanced Lena image is given in Figure 4.14(d).

#### 4.3.6 Histogram Specifications

For interactive image enhancement applications, the histogram equalization method is not suitable. The reason is that the histogram equalization method is capable of generating only one result, that is, approximation to uniform histogram.

In practical applications, it is desirable to specify a particular histogram shape capable of highlighting certain gray level ranges in an image.

**FIGURE 4.14** (a) Original Lena image (b) Histogram of original image (c) Histogram equalized Lena image (d) Histogram of equalized Lena image

To illustrate this concept let us consider *P _{r}(r)* and

*P*as the original and desired probability density function, respectively.

_{z}(z)Suppose, the histogram equalization is applied to the original image, that is,

if the desired image was also available, its gray levels would also be equalized by using equation (4.18).

Then the inverse process *z = G*^{−1}(*V*) gives back the gray levels *z,* of the desired image. This formulation is hypothetical, because the gray levels *‘z’* are precisely what is being required. However, *P _{s}(s)* and

*P*would be identical uniform densities because of the final result of the transformation.

_{v}(v)Hence we can write

which is independent of the density inside the integral. Thus instead of using *V* in the inverse process one can use the uniform levels *s* obtained from the original image, resulting in the levels *z = G*^{−1}(*s*) and this will have the desired probability density function. This procedure can be summarized as follows.

- Equalize the original image using the equation s =
*T*(*r*) = . - Specify the desired density function and obtain the transformation function
- Apply the inverse transformation function
*Z = G*^{−1}(*s*) to the level obtained in step 1 (since*G*(*z*) =*v = s = T*(*r*)).

This procedure gives a modified version of the original image with the new gray levels characterized by the desired density *P _{z}*(

*z*).

The histogram specification technique just described involves two transformation functions *T*(*r*) followed by *G ^{−1} (s)*. These two steps can be combined into a single step, so that the desired gray levels starting with the original pixels can be obtained. We know that,

*Z =*

*G*

^{−1}(

*s*) and substituting equation

*s*=

*T*(

*r*) = in equation 4.19(b), results in the combined transformation function as

where *r* relates to *z.*

The implication of equation (4.20) is simply that, an image need not be histogram equalized explicitly. All that is required is that *T*(*r*) be determined and combined with the inverse transformation function *G*^{−1}.

#### 4.3.7 Local Enhancement Technique

The two approaches discussed earlier are global techniques because all the pixels present in an image are modified using a transformation function. These global approaches are not suitable to enhance the details over small areas. The reason for this is that the pixels in these small areas have negligible influence on the computation of a global transformation. So it is necessary to develop an approach that will produce the desired local enhancement. The solution is to use transformation functions, which are based on gray level distribution or other properties in the neighborhood of every pixel in the image.

In the local enhancement technique the square or rectangular neighborhood is considered and we move the center of this area 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 finally used to map the gray level of the pixel centered in the image. The center of the neighborhood is then moved to the adjacent pixel location and the procedure is repeated. This procedure is called *local enhancement technique.*

Figure 4.15(a) shows an image consisting of five dark squares. Figure 4.15(b) shows the result of global histogram equalization. From this we understand that no new details or structures are resulted. Figure 4.15(c) is the result of local processing using 5 × 5 neighborhood pixels and it revealed the presence of small squares inside the large darker squares. The small squares are too close in the gray levels and the influence of global histogram equalization is negligible.

The local enhancement can also be achieved using the properties of the pixel, such as intensities in the neighborhood instead of using histograms (Figure 4.15). We know that the mean denotes the average brightness and the variance denotes the contrast. So the intensity mean and variance are two properties that describe the appearance of the image.

**FIGURE 4.15** Local enhancement. (a) Original Image (b) Result of global histogram equalization (c) Image after local enhancement

The typical local transformations that uses these two properties to transform the input image *f*(*x, y*) into the new image *g*(*x, y*) at each pixel location (*x, y*) is explained later.

The equation for such a transformation can be given as

where

In the equations (4.21) and (4.22) *m*(*x, y*) and *σ*(*x, y*) are the gray level mean and standard deviation calculated in a neighborhood region centered at (*x, y*), *M* is the global mean of *f*(*x, y*) and *k* is a constant. The values of the variables *A, m*, and *σ* are dependent on a predefined neighborhood of (*x, y*). From equation (4.22), *A*(*x, y*) is inversely proportional to the standard deviation of density and hence it offers high gain to low contrast regions and vice-versa.

#### 4.3.8 Image Subtraction

Image subtraction plays a vital role in medical applications. One of the important applications is in mask mode radiography. The primary use of image subtraction includes background removal and illumination equalization. The image difference between two images *f*(*x, y*) and *g*(*x, y*) can be expressed as

The image subtraction is used to enhance the medical images. It can be used to detect the malfunctioning of the human organ and blocking in the blood-carrying arteries. In order to detect blockage in the arteries, usually iodine dye is injected into the blood stream. Using the camera the image of the blood stream is taken before and after injecting the dye. Figure 4.16(a) shows the image before injection of the dye. Figure 4.16(b) shows the image after injecting the dye and Figure 4.16(c) is the result of subtracting (b) from (a). This image is an enhanced version in which the arterial path is quite bright compared to the other two images. By analyzing the image obtained by subtraction, the doctor is in a position to decide the actual location of blood blockage.

**FIGURE 4.16** Enhancement by image subtraction. (a) The mask image of a major blood vessel (b) Image after injecting dye in the blood stream (c) The result of subtraction (a) from (b)

#### 4.3.9 Image Average

Consider a noise image *Z*(*x, y*) obtained by adding the noise term *η*(*x, y*) to the original image *f*(*x, y*) and it is given as

The noise term *η*(*x, y*) is considered as a random phenomenon and it is uncorrelated; hence the average value of the noise results in a zero value. If our intension is to reduce the noise from the noisy image *Z*(*x, y*), one can apply the image averaging technique. If the noise satisfies zero average value then the problem of reducing or eliminating the noise from an image is a simple matter. Let us assume that there are ‘*m*’ number of noisy images available and it is denoted as *Z*_{1}(*x, y*), *Z*_{2}(*x, y*),…, *Z _{m}*(

*x, y*). Then the average of these images is represented as

It can then be proved that

As per Papoulis theory, it can be proved that

and

where *E*{ (*x,y*)} is the expected value of and *σ*^{2}_{η(x,y)} are the variances of *Z* and *η*, for all coordinates (*x, y*). The standard deviation at any point in the average image is

From this equation, we infer that as ‘*m*’ increases, the variance of the pixel values at each location decreases. This means that *Z*(*x, y*) approaches to *f*(*x, y*) as the number of noisy images used in the averaging process increases. Figure 4.17(a) shows the original printed circuit board image and (b) is its noisy version. Figures 4.17(c)−(g) shows the result of averaging 2, 4, 8, 16 and 32 such noisy images. The image obtained by averaging 32 noisy image is free from noise and suitable for all practical purposes.

**FIGURE 4.17** Noise reduction by averaging technique. (a) The original printed circuit board image (b) The noisy image of (a), (c)–(g) the results of averaging 2, 4, 8, 16 and 32 noisy images

##### 4.4 SPATIAL FILTERING

The spatial filtering concept has been introduced using spatial masks. The spatial filtering approaches are useful in image processing. Sometimes the masks used for implementing the filters are called as spatial filters. The widely used filter categories are

- Low-pass filters and
- High-pass filters.

Any image can be viewed as consisting of gray level details corresponding to low frequencies and high frequencies. For example, high-frequency components correspond to edges and other sharp details in an image. So when we employ a high-pass filter to process an image, the details corresponding to edges and other sharp details are highlighted and the low-frequency details are attenuated. Hence the high-pass filter can be used to obtain the boundary of the objects and other sharp details available in an image.

Similarly, the low-frequency details correspond to slowly varying components of an image. So when we employ a low-pass filter to process an image it allows only slowly varying image details and attenuate heavily the details corresponding to edges and sharp transitions and results in a blurred image.

The frequency responses of low-pass filter and high-pass filters are shown in Figure 4.18(a) and (b) and the corresponding spatial domain responses are shown in Figure 4.18(c) and (d). The responses shown in Figure 4.18(c) and (d) provides an idea for specifying the linear spatial filter mask.

In general, in all the spatial domain filters, a mask with its coefficient are used to find the sum of the products between the mask coefficients and the intensity of the image pixels under the mask at specific location in the image. A mask of size 3 × 3 is shown in Figure 4.19, where *C*_{1}, *C*_{2},…, *C*_{9} are the coefficient of mask.

Assume that the gray levels of the pixel under the mask are *L*_{1}, *L*_{2},…, *L*_{9}, then the average response of the linear mask is given by the equation,

Then the center pixel value at the location (*x, y*) is replaced by the response *R _{a}* obtained above. The mask is then moved to the next position and the response is obtained using the equation (4.30) and the pixel value in the image at the center of the mask is replaced by the response. This procedure is repeated for the remaining pixel in the image.

#### 4.4.1 Low-pass Spatial Filters

The low-pass spatial filters are used to reduce the noise such as bridging of small gaps in the lines or curves in a given image. So the low-pass filter is also called as *smoothing filters.*

**Low-pass Spatial Filter:** This smoothes the data and makes the image appear less granular, thus suppressing image noise.

**FIGURE 4.18** (a) Low-pass filter and (b) High-pass filter (c) Cross-section of low pass filter (a) (d) Cross-section of high pass filter (b)

**FIGURE 4.19** A sample mask of coefficients

The shape of the spatial low-pass filter is shown in Figure 4.18(c). From this curve we infer that all the coefficients of the mask corresponding to the spatial low-pass filter must have all positive values. For a 3 × 3 spatial filter the easiest arrangement is to have a mask in which all the coefficients have a value of 1. The 3 × 3 mask with coefficients 1 is shown in Figure 4.20. The mask can also be larger than 3 × 3.

**FIGURE 4.20** Low-pass filter

As the size of the mask increases the smoothing effect (blurring effect) also increases.

Figure 4.21(a) shows the original image (The colored figure is available here). Figure 4.21(b) and (c) is the result after applying the spatial low-pass filter of size 3 × 3 and 7 × 7, respectively.

**FIGURE 4.21** (a) Original image (b) Result of spatial HFF (c) Result of spatial LPF of size 3 × 3

**//program to implement low-pass, median-pass, and high-pass spatial filters**

#include "stdafx.h"

#include "filter.h"

#include "filterDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

#define blk 6

int i,j,x,y,heit,width,sumr,sumg,sumb;

struct pixel **/* Structure definition for red, green and blue pixels */**

{

BYTE r,g,b;

}; pixel (*bitmap)[300]=new pixel[300][300];

void read_img()**/* Function to read image */**

{

FILE *f=fopen("c:\\2.bmp","rb");

fseek(f,18,0);

heit=fgetc(f)|fgetc(f)<<8|fgetc(f)<<16|fgetc(f)<<24;

with=fgetc(f)|fgetc(f)<<8|fgetc(f)<<16|fgetc(f)<<24;

sumr=sumg=sumb=0;

fseek(f,54,0);

for(i=0;i<heit;i++)

for(j=0;j<width;j++)

{

bitmap[j][heit-i].b=fgetc(f);

bitmap[j][heit-i].g=fgetc(f);

bitmap[j][heit-i].r=fgetc(f);

}

fclose(f);

}

class CAboutDlg : public CDialog

{

public:

CAboutDlg();

enum { IDD = IDD_ABOUTBOX };

protected:

};

void CFilterDlg::OnPaint() **/* Function to draw screen */**

{

if (IsIconic())

{

SendMessage(WM_ICONERASEBKGND, (WPARAM)

dc.GetSafeHdc(), 0);

int cxIcon = GetSystemMetrics(SM_CxICON);

int cyIcon = GetSystemMetrics(SM_CYICON);

CRect rect;

GetClientRect(&rect);

int x = (rect.Width() - cxIcon + 1) / 2;

int y = (rect.Height() - cyIcon + 1) / 2;

dc.DrawIcon(x, y, m_hIcon);

}

else

{

CDialog::OnPaint();

}

}

void CFilterDlg::Onlowpass() **/* Code for low pass */**

{

read_img();

CDC *pdc=GetDC();

for(i=0;i<heit-blk;i++)

for(j=0;j<width-blk;j++)

{

for(x=0;x<blk;x++)

for(y=0;y<blk;y++)

{

sumr+=bitmap[i+x][j+y].r;

sumg+=bitmap[i+x][j+y].g;

sumb+=bitmap[i+x][j+y].b;

}

bitmap[i][j].r=sumr/(blk*blk);

bitmap[i][j].g=sumg/(blk*blk);

bitmap[i][j].b=sumb/(blk*blk);

sumr=sumg=sumb=0;

}

for(i=0;i<heit;i++)

for(j=0;j<width;j++)

pdc→SetPixel(i,j,RGB(bitmap[i][j].r,bitmap[i][j].g,

bitmap[i][j].b));

}

void CFilterDlg::Onmedianpass() **/* Code for median pass*/**

{

read_img();

CDC *pdc=GetDC();

pixel a[10];

int pix[10];

for(i=0;i<heit-blk;i++)

for(j=0;j<width-blk;j++)

{

for(x=0;x<blk;x++)

for(y=0;y<blk;y++)

{

a[x*blk+y].r=bitmap[i+x][j+y].r;

a[x*blk+y].g=bitmap[i+x][j+y].g;

a[x*blk+y].b=bitmap[i+x][j+y].b;

pix[x*blk+y]=RGB(bitmap[i+x][j+y].r,

bitmap[i+x][j+y].g,bitmap[i+x][j+y].b);

}

}

for(int u=0;u<8;u++)

for(int v=0;v<9;v++)

if(pix[u]>pix[v])

{

a[u].r^=a[v].r^=a[u].r^=a[v].r;

a[u].g^=a[v].g^=a[u].g^=a[v].g;

a[u].b^=a[v].b^=a[u].b^=a[v].b

}

bitmap[i][j].r=a[blk*blk/2].r;

bitmap[i][j].g=a[blk*blk/2].g;

bitmap[i][j].b=a[blk*blk/2].b;

for(i=0;i<heit;i++)

for(j=0;j<width;j++)

pdc→SetPixel(i,j,RGB(bitmap[i][j].r,bitmap[i][j].g,

bitmap[i][j].b));

}

void CFilterDlg::Onhighpass() **/* code for high pass */**

{

read_img();

CDC *pdc=GetDC();

for(i=0;i<heit;i++)

for(j=0;j<width;j++)

{

for(x=0;x<blk;x++)

for(y=0;y<blk;y++)

{

if(x==blk/2&&y==blk/2)

{

sumr+=8*bitmap[i+x][j+y].r;

sumg+=8*bitmap[i+x][j+y].g;

sumb+=8*bitmap[i+x][j+y].b;

}

else

{

sumr-=bitmap[i+x][j+y].r;

sumg-=bitmap[i+x][j+y].g;

sumb-=bitmap[i+x][j+y].b;

}

}

bitmap[i+x-blk][j+y-blk].r=(sumr)/(blk*blk);

bitmap[i+x-blk][j+y-blk].g=(sumg)/(blk*blk);

bitmap[i+x-blk][j+y-blk].b=(sumb)/(blk*blk);

sumr=sumg=sumb=0;

}

for(i=0;i<heit;i++) for(j=0;j<width;j++)

pdc→SetPixel(i,j,RGB(bitmap[i][j].r,bitmap[i][j].g,

bitmap[i][j].b));

}

void CFilterDlg::OnExit()

{

OnOK();

}

#### 4.4.2 Median Filtering

The low-pass spatial filter smooths out the edges and sharp details. The low-pass filter is not suitable for reducing the noise patterns consisting of strong spike-like components. For such applications the median filter is best suited.

In order to perform the median filtering the mask of size say 3 × 3 can be considered [Figure 4.22(b)]. The coefficients of this mask are all equal to 1. Place the mask in the top left corner and read the pixel values below this mask. Arrange these pixel values in the ascending order. For example, the values of the pixels below the mask 3 × 3 are 30, 15, 3, 7, 40, 28, 18, 65, 4 as shown in Figure 4.22(a). Then the median is found from the sorted values 3, 4, 7, 15, 18, 28, 30, 40, 65. For this example, the median value is ‘18’, the middle value of the sorted pixel values. So the center pixel of the mask is replaced by median thus computed. The procedure is repeated for moving the mask one position after another until the last pixel in the image is processed. The Figure 4.23(a) is an image and to this noise is added and the resulting image is shown in Figure 4.23(b). The noisy image shown in Figure 4.23(b) is given as input to both low-pass and median filters and their output responses are shown in figures (c) and (d), respectively. From the figure it is understood that the median filter performs better than low-pass filter under noisy environment.

**FIGURE 4.22** (a) Pixel values under mask of 3 × 3 (b) Centre value ‘40’ In Figure 4.23(a) is replaced by median value ‘18’

**FIGURE 4.23** (a) The original rose image (b) The noisy image (c) The image after applying LPF (d) Image after applying median filtering

#### 4.4.3 High-pass Spatial Filters

The high-pass filters attenuates low-frequency components heavily and pass the high-frequency components. This results in an mage with sharp details such as edges and high contrast. Additionally, high-pass filters can provide more visible details that are obscured, hazy, and of poor focus in the original image.

**High-pass Spatial Filter:** These attenuates low-frequency components and pass the high-frequency components resulting in an image with details on edges and high contrast.

From the spatial high-pass filter response shown in Figure 4.19, we have to construct a mask such that the center of the mask has a positive value and all its neighbor coefficients are of negative value. Such a mask is shown in Figure 4.24.

**FIGURE 4.24** A mask for high-pass filter

When we operate this mask to an image starting from top left corner and slide one portion to the right till it reaches the last position in an image, it results in an image which emphasizes sharp details (Figure 4.25). The logic behind this high-pass filter is explained as follows.

Let the mask be at a location where the pixels beneath have equal values corresponding to the background of an image. Let us assume that all the pixels have the gray level values say 10. Then the response of the mask is given by

**FIGURE 4.25** (a) The original image (b) The high-pass filtered image

So the center pixel is replaced by 0. This means that the background pixels correspond to low frequencies and they are attenuated. On the other hand, when we place the center of the mask corresponding to pixel whose gray level value is 150 and all its neighbors have gray level 10, then the corresponding response of the mask is given as

So the center pixel is replaced by 124. This means that the pixels which correspond to sharp details are passed without much attenuation. Hence it is clear that the mask we have constructed emphasizes the details corresponding to edges and sharp details.

#### 4.4.4 High-boost Filter

The high-pass filter in general results in a background which is darker than the original image. To overcome this difficulty, a high-boost filter can be employed which restores the original background details and at the same time enhances the sharpness of the image. The high-boost filter effect can be illustrated as follows.

**High-boost Filter:** Restores the original background details and enhances the sharpness of the image.

We know that high pass filter response = original − low pass. Then the high-boost filter can be given as

The mask for high boost filter is shown in Figure 4.26.

As *‘A’* increases, the background details of the high-boost filtered image becomes brighter and brighter (Figure 4.27). When we increase the value beyond 1.2, the resulting image becomes unacceptable. Hence we should be careful in choosing the value of ‘*A*’ so that an acceptable background is obtained.

**FIGURE 4.26** Mask for high-boost filtering

**FIGURE 4.27** High-boost filter. (a) Original Lena image (b) Image after applying high-boost filter with *A* = 1.1

#### 4.4.5 Derivative Filters

The low-pass filter approach used for image enhancement can be realized by using a mask whose coefficients are equal to 1. This means when we operate this mask over an image the resulting response corresponds to the average value in that location. So the averaging increases the smoothness of the image. We can also view averaging as corresponding to integration. Similarly, the high-pass filter is viewed as opposite to low-pass filter, that is, equivalent to doing differentiation. So we can say integration results in smoothing or blurring and differentiation results in sharpening of an image. Hence, the high-pass filter can be realized by using the differentiation concept. The effect of differentiation can be implemented using the gradient operator. The gradient operation can be illustrated as in equation (4.32). Consider an image *f*(*x, y*). The gradient of the image at the coordinates (*x, y*) is given by the vector

Then, the magnitude of the vector is given as

Equation (4.33) is the basis of image differentiation. The differentiation can be effected in many ways. Roberts proposed a technique and it is illustrated as follows.

Consider a part of an image of size 3 × 3 the gray levels of which are denoted as *r*_{1}, *r*_{2},…, *r*_{9} as is shown in Figure 4.28.

**FIGURE 4.28** Subimage of size 3 × 3

Equation (4.33) can be approximated at the point *r*_{5} and it is given as

where *r*_{5} − *r*_{8} is the difference in the *x*-direction and (*r*_{5} − *r*_{6}) is the difference in the *y*-direction.

Equation (4.34) can also be simplified further and given as

According to Roberts technique the other definition is given in equation (4.35).

Equation (4.34a) can be represented in the form of masks for *x*-direction and *y*-direction and they are given as in Figure 4.29. These masks are also known as Roberts’ cross-gradient operators.

Another way of representing mask proposed by the scientist Prewitt using 3 × 3 mask is given in equation (4.36).

**FIGURE 4.29** Roberts‘ mask

The mask for equation (4.36) is shown in Figure 4.30. The equation (4.36) has two components and they correspond to *x* and *y* components. The first component is the difference between the third and first row of 3 × 3 region which approximates the derivative in the *x*-direction and the second component is the difference between the third and first column which approximates the derivative in the *y*-direction. The masks are also known as *Prewitt operators.* There is yet another pair of mask proposed by Sobel which is given in Figure 4.31.

The three different masks described here are applied to the original Lena image shown in Figure 4.32(a) (The colored figure is available here). Figures 4.32(b), (c), and (d) are the result of Roberts’, Prewitt, and Sobel operators, respectively. A VC++ program to implement the Roberts’, Prewitt, and Sobel operators is also given for the readers reference.

**FIGURE 4.30** Prewitt mask

**FIGURE 4.31** Sobel mask

**FIGURE 4.32** Output of the program given below. (a) Original image (b) Result of gradient approach using Roberts‘ approach (c) Result of gradient approach using Perwitt approach (d) Result of gradient approach using Sobel approach

**//program for Robert, Prewitt, Sobel operators**

#include "stdafx.h"

#include "Filter.h"

#include "FilterDoc.h"

#include "FilterView.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

#pragma pack(1)

#include "fstream.h"

BYTE type = 1;

struct PIxEL

{

BYTE r,g,b;

};

PIXEL org[800][600];

void CFilterView::OnDraw(CDC* pDC)

{

LONG w,h,xbyte,size;

WORD bits;

int avr,avg,avb,avr1,avg1,avb1,px;

/* Initialization of various masks */

/* Roberts mask */

int robert[2][2]={ 1, 0, 0, -1 };

int robert1[2][2]={ 0, -1, 1, 0 }

/* Prewitt mask */

int prewitt[3][3]={-1, -1, -1, 0, 0, 0, 1, 1, 1 };

int prewitt1[3][3]={-1, 0, 1, -1, 0, 1, -1, 0, 1 };**/* Sobel mask */**

int sobel[3][3]={ -1, -2, -1, 0, 0, 0, 1, 2, 1 };

int sobel1[3][3]={ -1, 0, 1, -2, 0, 2, -1, 0, 1 };

ifstream fin;

fin.open("bitmap.bmp",ios::in|ios::binary);

fin.seekg(18,ios::beg);

fin.read((char *)&w,sizeof(w));

fin.read((char *)&h,sizeof(h));

fin.seekg(28,ios::beg);

fin.read((char *)&bits,sizeof(bits));

fin.seekg(34,ios::beg);

fin.read((char *)&size,sizeof(bits));

xbyte =w*bits/8;

fin.seekg(54,ios::beg);

if(bits != 24)

{

MessageBox("Error.Only 24 bit image possible ");

PostQuitMessage(0);

}

for(WORD j=0;j<h;j++)

{

for(WORD i=0;i<w;i++)

{ if(fin.eof())

{

MessageBox("Read Error");

goto out2;

}

org[i][h-j].b = fin.get();

org[i][h-j].g = fin.get();

org[i][h-j].r = fin.get();

}

for(int diff=size/h-xbyte;diff>0;diff--)

fin.get();

}

out2:fin.close();

for(WORD x=0;x<w;x++)

for(WORD y=0;y<h;y++)

switch(type)

{

case 1: pDC→SetPixel(x,y,RGB(org[x][y].r,org[x][y].g,org[x][y].b));

break;

// **implementing roberts’ mask**

case 2: avr=avg=avb=avr1=avb1=avg1=0;

for( px=x;px<=x+1;px++)

for(int py=y;py<=y+1;py++)

{

avr+= robert[px-x][py-y]*org[px][py].r;

avg+= robert[px-x][py-y]*org[px][py].g;

avb+= robert[px-x][py-y]*org[px][py].b;

avr1+= robert1[px-x][py-y]*org[px][py].r;

avg1+= robert1[px-x][py-y]*org[px][py].g;

avb1+= robert1[px-x][py-y]*org[px][py].b;

}

pDC→SetPixel(x,y,RGB(abs(avr)+abs(avr1),abs(avg)+

abs(avg1),abs(avb)+abs(avb1)));

break;

// **implementing prewitt’s mask**

case 3:avr=avg=avb=avr1=avb1=avg1=0;

for( px=x;px<=x+2;px++)

for(int py=y;py<=y+2;py++)

{

avr += prewitt[px-x][py-y]*org[px-1][py-1].r;

avg += prewitt[px-x][py-y]*org[px-1][py-1].g;

avb += prewitt[px-x][py-y]*org[px-1][py-1].b;

avr1+= prewitt1[px-x][py-y]*org[px-1][py-1].r;

avg1+= prewitt1[px-x][py-y]*org[px-1][py-1].g;

avb1+= prewitt1[px-x][py-y]*org[px-1][py-1].b;

}

pDC→SetPixel(x,y,RGB(abs(avr)+abs(avr1),abs(avg)+

abs(avg1),abs(avb)+abs(avb1))); break;

// **implementing sobel mask**

case 4:avr=avg=avb=avr1=avb1=avg1=0;

for( px=x;px<=x+2;px++)

for(int py=y;py<<=y+2;py++)

{

avr+= sobel[px-x][py-y]*org[px-1][py-1].r;

avg+= sobel[px-x][py-y]*org[px-1][py-1].g;

avb+= sobel[px-x][py-y]*org[px-1][py-1].b;

avr1+= sobel1[px-x][py-y]*org[px-1][py-1].r;

avg1+= sobel1[px-x][py-y]*org[px-1][py-1].g;

avb1+= sobel1[px-x][py-y]*org[px-1][py-1].b;

}

pDC→SetPixel(x,y,RGB(abs(avr)+abs(avr1),abs(avg)+

abs(avg1),abs(avb)+abs(avb1)));

break;

}

}

void CFilterView::OnOriginal()**/* Function to draw original image */**

{

//MessageBox("Original Image");

type=1; this →RedrawWindow();

}

void CFilterView::OnLowpass() **/* Function to draw lowpassfilter */**

output */

{

//MessageBox("LOWPASS FILTER");

type=2; this →RedrawWindow();

}

void CFilterView::OnMedian() **/* Function to draw medianfilter */**

{

//MessageBox("MEDIAN FILTER");

type=3; this →RedrawWindow();

}

void CFilterView::OnHighpass()**/* Function to draw highpassfilter */**

{

//MessageBox("HIGrobert1SS FILTER");

type=4; this →RedrawWindow();

}

##### 4.5 FREQUENCY DOMAIN

The spatial domain filters used for image enhancement have been discussed earlier. The spatial technique methods are simple and easy to implement and also the speed of operation is high. In spite of these advantages, there are certain situations in which the spatial domain filters are not easily addressable. Under such circumstances it is more appealing and intuitive to use the frequency domain filtering approach. All the frequency domain filters are based on computing the Fourier transform of an image to be enhanced. Then the result is multiplied with a filter transfer function and the inverse Fourier transform is applied to the product so that it results in the enhanced image.

The low-pass filter is used to smooth the image and remove the high-frequency components related to noise. Smoothing effect is achieved in the frequency domain by attenuating a specified range of high-frequency components in the transformed image.

The low-pass filter equation can be represented as

where *F*(*u, v*) is the Fourier transform of the image to be enhanced, *H*(*u, v*) is the filter transfer function, and *Z(u, v)* is the enhanced image in the frequency domain. In order to get the enhanced image in the spatial domain the inverse transform is applied and the corresponding equation is given by

In equation (4.37) we have considered the transfer function *H*(*u, v*) that gives *Z*(*u, v*) by attenuating the high-frequency components of *F*(*u, v*). Now our difficulty lies in selecting an appropriate filter transfer function. In general, most of the filter transfer functions affect the real and imaginary parts of *F*(*u, v*) in the same manner. These filters are called as zero phase shift filters because they do not change the phase of the transform. In the following section, selection of the filter transfer function is discussed.

#### 4.5.1 Ideal Low-pass Filter

The two-dimensional ideal low-pass filter’s transfer function can be given by the relation

where *L*_{0} is a specified positive quantity and *L*(*u, v*) is the distance from the point (*u, v*) to the origin of the frequency plane, that is,

The frequency response of *H*(*u, v*) as a function of *u* and *v* is shown in Figure 4.33(a). The cross-sectional view of Figure 4.33(a) is also shown in Figure 4.33(b).

**FIGURE 4.33** (a) Perspective plot of a low-pass filter transfer function (b) 2D filter transfer function

From Figure 4.33(a) it can be seen that the filter passes all the frequencies inside the circle of radius *L*_{0} where it attenuates all the frequencies outside this circle. Hence this filter is called *ideal low-pass filter.*

#### 4.5.2 Butterworth Low-pass Filter

The response of the Butterworth low-pass filter of order *n* is defined by the equation

where *L*(*u, v*) is the distance from the point (*u, v*) to the origin and is given by (*u*^{2} + *v*^{2})^{}. The three-dimensional and cross-sectional views of the Butterworth low-pass filter responses are shown in Figure 4.34.

When *L*(*u, v*) = *L*_{0}, *H*(*u, v*) = 0.5, this indicates that at cut-off frequency the response is half of its maximum value (50%).

In most of the cases, at cut-off frequency, the response will be equal to times the maximum value of *H*(*u, v*). To have this effect, the equation should be modified as given in equations (4.42) and (4.43).

**FIGURE 4.34** The three-dimensional and cross-sectional views of Butterworth low-pass filter

Figure 4.35(a) shows the original image (The colored figure is available here). Figure 4.35(b) shows the result of applying low-pass Butterworth filter of order *n* = 1 for different radii. From this example it can be understood that the low-pass filtering process reduces the spurious effect.

**FIGURE 4.35** (a) The original image (b) The result of applying low-pass Butterworth filter of order 1

#### 4.5.3 High-pass Filter

In the previous sections we have discussed the ideal low-pass and Butterworth low-pass filters in detail. The low-pass filter results in the smoothing effect by attenuating the high-frequency components. The opposite effect, that is, the sharpening of the details in an image can be obtained using high-pass filter. The high-pass filter passes the high frequency components and it attenuates low-frequency components corresponding to slow-varying details of the image.

The ideal high-pass filter which has sharp or abrupt transition is given by the equation

where *L*_{0} is the cut-off frequency which is measured from the origin and *L*(*u, v*) is the distance from the origin and is given by

The filter response is opposite to the ideal low-pass filter discussed earlier. The ideal low-pass filter which has the abrupt transition at the cut-off frequency cannot be realized using the electronic circuit components. However, it can be realized with smooth transition frequency and such filters are called *Butterworth filters.*

The transfer function of the high-pass Butterworth filter of order *n* with a cut-off frequency *L*_{0} from the origin is given by the equation

#### 4.5.4 Homomorphic Filtering

**Homomorphic Filtering:** This filter controls both high-frequency and low-frequency components.

We have already discussed that an image function can be represented using illumination and reflectance components. The illumination component of an image generally represents the slow-varying details of the image, while the reflected component represents sharp details and are abruptly varying junctions of dissimilar object. Hence, we can interpret that the illumination corresponds to low-frequency components and the reflectance corresponds to high-frequency components in the frequency domain, respectively. It is possible to develop a filter which will control both high-frequency and low-frequency components. The filter which controls both high-frequency and low-frequency components are sometimes called as *Homomorphic filter.* The equation for Homomorphic filter can be derived from the illumination reflectance model given by the equation

The Fourier transform of the product of two functions is not separable, that is,

To overcome this difficulty, we rewrite equation (4.46) in the logarithmic form as

Then taking the Fourier transform of equation (4.47) results in

where *I*(*u, v*) and *R*(*u, v*) are the Fourier transform of ln *r*(*x, y*) and ln *i*(*x, y*).

Let *H*(*u, v*) be the Homomorphic filter function. The response of the *H*(*u, v*) on the function *G*(*u, v*) can be given by the relation

The inverse Fourier transform of equation (4.51)

where

As *g*(*x, y*) is formed by taking a logarithm of original image *f*(*x, y*), the inverse operation gives the desired enhanced image *Z*(*x, y*), that is,

where *g*(*x, y*) is the enhanced image in the spatial domain. The procedure adopted can be given by a schematic diagram shown in Figure 4.35(a). Figure 4.36(b) is used as an input to the homomorphic filter and its response is shown in Figure 4.36(c).

**FIGURE 4.36** (a) Homomorphic filtering approach for image enhancement (b) The tire image (*Source*: MathWorks Inc., USA (MATLab)) (c) The response of the homomorphic filter

#### 4.5.5 Pseudo Color Image

The assigning of color to monochrome images based on certain properties of the gray level content is called *pseudo color image processing.* This gives an illusion to a common person as if the image under consideration is a color image.

**Pseudo Color:** A tricolor image formed using red, green, and blue primary colors and it gives an illusion to the common man as if the image under consideration is a color image.

The intensity slicing and color coding is one of the simplest examples of pseudo color image processing. If an image is viewed as a two-dimensional intensity function then a parallel plane can be placed at each coordinate corresponding to the plane which will slice the area of intersection. Figure 4.37 shows an example of slicing the image at a height *h*_{1} = *f*(*x, y*) to slice into two parts. In general, many planes are located at different heights *h*_{1}, *h*_{2},…, *h _{m}* and the gray levels ranging from

*l*

_{0}to

*L*, where

*l*corresponds to dark and L to white. So the gray levels ranging from 0

_{0}*< m< L,*have

*m*planes to partition the image into

*m +*1 regions. The regions which have the same gray levels are denoted as

*r*

_{1},

*r*

_{2},…,

*r*

_{5}. Then the color assigned to each of the region can be given by the relation

where *C _{k}* is color associated with

*k*th region.

An example for intensity slicing is shown in the Figure 4.37 (The colored figure is available here) where the different gray level regions are highlighted with different colors.

**FIGURE 4.37** Slicing the image at height *h _{1}*

##### 4.6 GRAY LEVEL TO COLOR TRANSFORMATION

The pseudo color enhancement for the monochrome image can be achieved using three independent transformations on the gray levels of a given input image. The results of the three transformations are then separately sent to the red, green, and blue guns of a color television monitor. This approach produces a composite image color content of which is modulated by the nature of the transfer function used.

#### 4.6.1 Filter Approach for Color Coding

In this approach the Fourier transform of the monochrome image to be enhanced is first obtained. The Fourier transform of the image is then subjected to three different filters. The first filter may allow the low-frequency components of the image. In order to design the filter, a cut-off frequency corresponding to the low-frequency must be selected. The second filter will pass the frequency components corresponding to the high frequency in the images. The third filter may pass the band of frequencies in the image.

The output of these filters is subjected to the inverse Fourier transform followed by proper preprocessing techniques. Then the final output from these three different streams is fed to a tricolor picture tube so that the final output will be highlighted with red, blue, and green colors corresponding to low-frequency components, a desired band of frequencies, and high-frequency components of the image.

#### Summary

Image enhancement techniques are used to improve the image quality or appearance for human interpretation. This chapter introduces spatial domain and frequency domain enhancement techniques. It lays stress on the importance and necessity of image enhancement processes in various applications. It gives a detailed description of the steps involved in spatial and frequency domain techniques. Most of the techniques covered are explained with sample programs implemented in VC++. The images obtained as output for these programs are also given. Students can make use of these programs to have a better practical insight of these techniques.

The spatial domain technique such as histogram equalization is a fundamental tool, not only in image enhancement, but also in satellite and medical imaging applications. This chapter also provides a fair description of the spatial masks and filters. The neighborhood pixel processing techniques discussed in Chapter 1 are used in spatial masking techniques. Various filtering techniques are also discussed with examples. The examples for various filtering applications are self-explanatory.

The chapter concludes with the application of pseudo color image processing for image enhancement. It also gives a brief description about the filter approach for color coding. The fact that these tools were introduced in the context of image enhancement is likely to aid in the understanding of how they operate on digital images. The students can improve their understanding capability and logical reasoning by answering the questions and solving the problems given at the end of the chapter.

#### Review Questions

**Short Type Questions**

- What is point processing and contrast stretching?
- Draw the histograms of four basic image types.
- tate the salient features of median filter.
- Write the Prewitt and Sobel operator mask.
- What is the filter cut-off frequency?
- Draw the RGB model.
- When will you use the HIS model?
- Which of the devices require CMY data input?
- What is histogram?
- When will you use local enhancement technique?
- When will you call a filter as a low phase shift filter?
- Distinguish between full color and pseudo color.
- Mention three important characteristics used to distinguish one color from another color.
- What is the advantage of using homomorphic filtering?
- What is high boost filter? Where will you use this filter?

**Descriptive Type Questions**

- Explain in detail the histogram equalization technique for image enhancement.
- Write notes on
- Contrast stretching
- Gray level slicing
- Bit plane slicing

- Explain the procedure involved in enhancing the image using histogram specification.
- Explain the various spatial domain filter approaches for image enhancement.
- Compare and contrast various filters available under frequency domain category.
- Give the mathematical analysis and procedure to implement homomorphic filter approach.
- Give the block diagram of the filter approach for pseudo color image processing.
- Explain the underlying concepts used for full color image processing.
- An image has the gray level probability density function
*P*(_{r}*r*) as shown in Figure 4.38. It is desired to transform the gray levels of this image using the specified probability density function*P*(_{z}*z*) as shown in Figure 4.38(b). Find the transformation in terms of*r*and*z*that will transform the image.**FIGURE 4.38**