You are on page 1of 28

Bakhtiyarpur College of

Engineering

Digital Image Processing Lab File

Submitted By: Submitted To:


Mamta Kumari Prof. Alok Kumar
RegNo-16105126016
Roll No- 16cs49
Table of Content

1. Distance and connectivity

2. Image Arithmetic

3. Affine Transformation

4. Point Operations

5. Neighbourhood Operations

6. Image histogram
Experiment-1
Distance and Connectivity
Objective
The objective of this experiment is to understand the notion of connectivity and
neighbourhood defined for a point in an image. These concepts are useful in
finding if two points are neighbours in some sense and quantify the distance
between them.

Introduction
In most image processing tasks, basic operations typically involve modification
of a pixel’s value based on the value of its neighbours. A digital image is
defined on a discrete grid of points. Hence there are different ways in which
one can define the spatial relation between pixels. For instance, we could
consider two pixels as ‘neighbours’ only if they share one side/ edge.
Alternately, two pixels could be neighbours if they share a vertex. These two
leads to different definitions of neighbourhood for a pixel. The extent of a
neighbourhood is also of interest as it is useful in finding not only which pixels
belong to an object in an image but also the extent of the object.

The notions of connectivity and distance between two pixels are useful in
defining the neighbourhood for a pixel. Connectivity tells us which pixels are
`next’ to which. It helps answer questions like “Can we reach pixel p0 from
pixel p1?” The concept of connectivity also helps simplify the definition of
region or boundary in an image. Once we know we can reach p1 from p0, the
cost to reach or distance is of interest. There are different distance metrics and
we will study them in this experiment.
Theory
Connectivity of pixels:

Pixel connectivity describes a relation between two or more pixels. For two
pixels to be connected they must fulfill certain conditions on the pixel value
and spatial adjacency.

4 - neighbours

For a pixel p (x, y) shown in green circle, N4(p) is


the set of 4-neigbors which share a face (or edge of
the pixel) with p. They are four in number and are
shown by orange circles. Two pixels p and q are 4-
connected if q is in N4(p) and both p, q є C

8 - neighbours

For a pixel p (x, y) shown in green circle, N8(p) is


the set of neighbours which share a face or a
vertex/corner. There are 8 such neighbours and
they are shown by orange circles. Two pixels p and
q are 8-connected if q is in N8(p) and both p, q є C

m – neighbours (mixed neighbours):

For a pixel p (x, y) shown in green circle, Nd(p) is


the set of diagonal neighbours shown by red
circles. Two pixels p and q with values from C
are m-connected if:

q is in N4(p), or

q is in Nd(p), and the set N4(p) N4(q) has no pixels


whose values are from C.
The pixel brightness condition:

Their pixel values must be from the same set of values C. For a grayscale
image, C might be any range of possible Gray-level values 0 to 255, e.g. C= {12,
13, …. 40}, for a binary image we simply have

C = {1}.

To understand the spatial adjacency condition, we first explain the notion of


neighbourhood. For a pixel p with the coordinates (x, y), the set of
neighbourhood pixels Nd (p) can be defined as follows

Properties of connectivity:

If we consider a pixel to be connected to itself (trivial connectivity) then


connectivity is reflexive. A pixel and its neighbour are mutually connected and
hence connectivity is also symmetric. 4-connectivity and 8-connectivity are
also transitive: if pixel A is connected to pixel B, and pixel B is connected to
pixel C, then there exists a connected path between pixels A and C.

Pixel connectivity is an equivalence relation as it is reflexive, symmetric, and


transitive.

Simplification of the definition of region and boundary in an image:

Let S represent a subset of pixels in an image. Two pixels are said to be


connected in S if there exists a path between them consisting entirely of pixels
in S. For a pixel p  S, the set of pixels pi  S that are connected to it is called
connected component.

In the event there is only one connected component, the set S is said to be a
connected set and S is called a region in the given image.

The boundary of a region R is the set of pixels in the region that have at least
one neighbour that is not in R.

Distance Metrics:

On a discrete grid, the distance between two points a: (xa, ya) and b: (xb, yb)
can be defined in a variety of ways.

Euclidean distance:
In vector form, this is referred to as L2 norm.

City-block distance: This is the 4-connected distance and sometime called


Manhattan distance.

In vector form, it is referred to as L1 norm.

Chess-board distance: This is the 8-connected distance D8, also called as chess-
board distance.

In vector form, it is referred as L norm.

Using these distance metrics, for a given pixel p, one can find pixels at equal
distance from this pixel. These pixels form the iso-distance pixel set for the
point p.

Properties of a true distance metric

Let a, b and c be three pixels with coordinates (xa, ya), (xa, yb) and (xc, yc)
respectively. A distance function D is a metric if it has the following properties:

D(a, b) ≥ 0 and D(a, b) = 0 iff a = b

D(a, b) = D(a, b)

D(a, c) ≤ D(a, b) + D(b, c)

Procedure
The experiment is designed to understand the concept of distance and
connectivity in digital images. This experiment consists of three
experiments:

Compute distance between two points in an image

Obtain iso-distance points from a given point

Covert binary image into gray-scale image by taking distance transform


with respect to a given point
These experiments can be performed under three basic distance metrics
settings accessible using “select type of distance” and two basic
connectivity types accessible using “connectivity” tab.

Steps to run

Experiment-1:

Select two points on the image grid by doing mouse click on red and
green squares which highlight your select location.

Select distance metric from using “select type of distance” option

Select “run” option to see the distance between two selected points w.r.t
to the given distance metrics

Select different distance and connectivity type to observe change in the


computed distance.

Experiment-2:

Select a point on the image grid

Select distance value using distance scroll bar appeared next to it.

Select “run” option to see iso-distance points w.r.t to the input point

Select different distance and connectivity type to observe change in


result.

Experiment-3:

Left image panel shows a binary image and small red point on it.

Move location of red point by clicking at the respective location

Select distance metric from using “select type of distance” option

Select “run” option to conversion of binary image to gray scale image


based on the distance transform computed w.r.t to the input location

Select different distance and connectivity type to observe variation in the


gray scale output.
Experiment-2
Image Arithmetic
Objectives
To learn to use arithmetic operations to combine images.

To study the effect of these operations on the dynamic range of the output image.

To study methods to enforce closure - force the output image to also be an 8 bit
image.

Introduction
In image arithmetic, we consider operations such as I(x,y) = A (x,y) o B(x,y) where
o is an arithmetic operation such as addition, subtraction, multiplication or division.
Here, A and B could be derived from different sources. Such operations are
particularly used in modelling image acquisition as a result of a perfect image
corrupted with (additive or multiplicative) noise. The noise is often introduced either
by a transmission medium or the camera.

Theory
A (x,y) o B(x,y) = I(x,y)

The important requirement in image arithmetic is that all (input and output) the
images are of the same size MxM.

Arithmetic operations are done pixelwise. Let p = A(x,y) and q = B(x,y) be the pixel
values to be operated on and r =I(x,y) be the result of the operation.

Addition

I(x,y) = A(x,y) + B(x,y) → r = p + q

Subtraction

I(x,y) = A(x,y) - B(x,y) → r = p - q


Difference : I(x,y) = |A(x,y) - B(x,y)| → r = |p - q|

Multiplication:

I(x,y) = A(x,y) X B(x,y) → r = p x q

Division:

I(x,y) = A(x,y) / B(x,y) → r = p / q

Implementation issues:

Digital images are stored as b - bit images. Hence, the range of values a pixel can
take is restricted to the range [ 0, 1,... (2b -1)]. With b= 8 this range is [0,1,...255].
The closed interval poses a problem when performing arithmetic operations in
practice, as the results are not guaranteed to be within this interval. For an 8-bit
image the intervals for the output pixel for each operation are:

Addition: r ∈ [0, (2x255=510)]

Subtraction: r ∈ [-255, 255]

Difference: r ∈ [0, 255]

Multiplication: r ∈ [0, (2552 = 65025)]

Division: r ∈ [0,∞]

Since we need r to be in [0,255], we will have an underflow or overflow. A final


processing step is generally required to handle this problem.

There are two options:

Clipping- Map all negative pixel values ( r < 0) to zero and all values above 255 to
255.

Auto scaling - This operation remaps the range of r to fit to be in [0, 255] as follows.
Let ra be the autoscaled value.

ra = 255 x (r - rmin)/(rmax-rmin)

Where, rmax and rmin are the maximum and minimum values of an arithmetic
operation.

Procedure
The experiment is design to understand and learn the image arithmetic concepts.
This experiment consists five parts:

Image addition
Image Subtraction

Image Difference

Image Multiplication

Image Division

Steps to run the experiments

Select image from the mosaic using 'select imag' option

Select region of the image to load it in the input image panel

Select secondary image by choosing either of the 'dull' , 'bright' or 'gradient' images

Select one option from 'addition', 'subtraction', 'difference', 'multiplication' and


'division'

Select the one option from 'clipping' and 'auto-scaling'

Select run option to perform the operations

Output result will be displayed in the output panel

Along with intermediate results

Interesting Observations

Do the Subtraction and Difference operations, and compare the results.

Do the Multiplication operation first with clipping and then with auto-scaling, and
compare the results.
Experiment-3

Affine Transformation
Objectives
To learn basic image transformation
Translation
Rotation
Scaling
To learn the role of interpolation operation
Bi-linear
Bi-cubic
Nearest neighbor
To learn the effect of multiple transformations
Significance of order in which one carried out

Introduction
In cricket, to decide if a batsman has been run out, the standard practice is to select
the part of the image around the stumps and zoom on it so that the third umpire can
take a decision. The region of interest is the crease. If one were to automate this
process, several steps are required. This will include identifying the stump region
and rotating the image so that the crease is at the right orientation and scale to
enable an accurate judgement.

The set of transformations that will possibly be required are translation (to place the
crease within the view window), rotation (to orient the crease such that the bat is
seen to the left or right of it) and finally scaling so the bat position relative to the
crease can be determined accurately.

The set of transformations namely, translation, rotation, scale and shear (stretching
in one direction) forms the affine class of transformations. In this experiment, we
will study how the first three types of transformations are effected on a given image.
Theory
An affine transformation is a geometrical transformation which moves points in
space such that points which were lying on a line remain so after the transformation.

Given a point which is a point location [x, y]T , transformed location after
translation, rotation and scaling can be as follows.

Translation

Rotation (θ)

Scaling

To geometrically transform an image, the result we seek is an image with


transformed coordinates. The result of applying the transformation function is
generally not an integer particularly in case of rotation and scaling. This poses a
problem since the image is defined on a grid (coordnates have to be integer valued).
The solution is interpolation.

n implementation, image rotation is done as follows. Given I[m,n] to find a


transformed image X[m., n.] where [m., n.]= A x [m, n] + B according to the above
given equation:

Create a dummy array X[m., n.].

Find the locations [m, n] in I that correspond to locations [., n.] in the image X by
applying the inverse transformation. I.e. A-1 ([m., n.]-B) = [m, n]. This can result in
non-integer locations.

Find the pixel value at these source locations. If it is non-integer valued locations,
use an interpolation function.

Example: Consider A to be rotation by 30 degrees. Let us take the location [12, 13]
in X and find its corresponding location in I. This will be [3.89 17.26]. To determine
the value of I at this location, we can use the known values closest to this location
Option 1: Round off the [m, n] and take the pixel value at that location. This is
known as the nearest neighbour interpolation method.

Option 2: Take the weighted average of the 4 neighbouring pixels. The weights are
the distance between the desired location and that of the neighbours. The common
form is the bilinear form which is a second order approximation and equivalent to
fitting a straight line pairs of neighbouring pixels.

Option 3: Take a nonlinear combination of the 16 neighbouring pixels. The standard


is to use a third order approximation and fit two piecewise cubic polynomials on to
the neighbouring pixels. Hence the interpolation function is called the bicubic
interpolation method.

Procedure

The experiments are designed in two parts:

Single transformation among three is allowed

Multiple transformations are allowed

Single Transformation

Select image from the mosaic using .select image. option

Selected region of the image to load it in the input image panel

Select appropriate parameters to see the effect

Select .single choice. option

Only one transformation is available at a time

Select one choice among three choices: scaling, rotation and translation

For rotation and scaling, select interpolation function to see the difference in the
output. The default choice for interpolation function is nearest neighbor.
Select .run. option to see the output under the selected parameters

To view the full sized output image, select .view full size image.

Panel below input and output image panel shows the intermediate results obtained
during operation. Click on the thumbnails to view images in full size.

Multiple Transformations

Select .multiple choice. option

Order selection panel will be pop out. Select the order in which you want to perform
the operation. The order sequence is displayed in front of each transformation
choice. During operation, edit option is provided below in the left panel to change
the selected order of operation.

All three transformations are allowed under this choice

The interpolation function is kept fixed i.e. nearest neighbor for this operation

Select appropriate parameters

In case you do not want any particular transformation. Do not change parameter of
the respective choice

Select the .run. option to perform operations

Output panel and intermediate results panel will display the obtained output.
Experiment-4

Point Operations
Objective
To learn image enhancement through point transformation

Linear transformation

Non-linear transformation

Clipping (piecewise linear)

Gray level windowing

Introduction
The most popular application of image processing is to change the visual appearance
of the image such as to improve the contrast, reduce the dynamic range of
brightness, reverse the 'polarity' such as dark to white and vice versa. There are
many image editing tools which enable one to do such alteration in the appearance
of the image. Besides such everyday uses, these operations play an important role
in many domains such as medical imaging, remote sensing, to overcome the
inherent limitations in imaging acquisition process, adapting the image to different
displays and so on.

In this experiment, we will study an effective, yet simple, technique that


manipulates the pixel values in the given image using a mapping function. You will
observe that the effects of processing, greatly depends on the nature of the chosen
mapping function: linear or non-linear.
Theory
Point operations are simple image enhancement techniques. Here, the operations
are directly performed on the pixel values. A given image f(x,y) is transformed into
an output image g(x,y) using some transformation T. Let f(x,y) = p and g(x,y) = q;
p and q are also known as pixel values in the corresponding images at location (x,y).
Thus,

q = T(p)

where, T is a transformation that maps p into q. The results of this transformation


are mapped into the grey scale range as we are dealing here only with grey scale
digital images. So, the results are mapped back into the range [0, 255].

There are four commonly used and basic types of functions (transformation) are
described here:

Linear function

Non-linear function

Clipping

Window function

Linear function: The output pixel value is linearly related to the input pixel value.
This can be described using a line equation as follows:

q= s * p + o;

where, s is the slope of the line and o is the offset from the origin. This type of
transformation is particularly useful for enhancing white of gray details embedded in
the dark regions of an image.

Non-linear function: The output pixel value is not linearly related to the input pixel
value. Here, we consider example of log-transformation function to explain the
process. In this function is:

q= c * log(1+p)

where c is a constant variable. This function converts a narrow range of low gray-
level values of p in to a wider range of q values or vice versa. This type of
transformation is useful in expanding values of dark pixels while compressing the
higher values.

Clipping: The value of output pixel is related to the input pixel value in piece-wise
linear fashion. Though such functions are complex and require user input but useful
in many practical situations. This function is defined as
where, a and b are constant variables which define input pixel value range in which
linear function with a slope s and offset o is applied. In gray level image, value of L
is 255. In our experiment section, we kept value of offset fixed to zero.

Window: This function is similar to the clipper. In this, only a desired range of gray
levels is preserved in the output image. It is defined as:

Procedure
The experiments are designed to understand and learn the effect of different gray-
level transformation functions. This experiment consists of four experimentations:

Linear

Non-linear

Clipping

Windowing

Steps to run the experiments

Select image from the mosaic using 'select image' option

Select region of the image to load it in the input image panel

Select appropriate parameters to see the effect

Select one option from 'linear', 'non-linear', 'clipping' and 'window'

For linear select the value of slope and offset

For non-linear select the value of c .


For clipping and window option, select values for paraemeters a (start), b (end) and
slope

With the change in the parameters, the corresponding transformation function will
be display in the left below panel

Select run option to perform the operations

Output result will be displayed in the output panel

Along with intermediate results


Experiment-5

Neighbourhood Operations
Objective
To learn about neighbourhood operations and use them for

Linear filtering

Non-linear filtering

To study the effect of the size of neighbourhood on the result of processing

Introduction
Neighbourhood operations are a generalization of the point operations. A pixel in the
processed image now depends not only on the corresponding pixel in the input
image but also its neighbouring pixels. This generalization also allows for defining
linear as well nonlinear filtering operations. An example of linear filtering in image
editing is to produce a soft focus effect by smoothing away the fine details. Image
denoising is another application where neighbourhood operations are very useful.
During transmission, an image may get corrupted with salt and pepper noise, where
isolated pixels turn black or white and stand out from their background. Performing
a nonlinear filtering such as replacing a pixel by the median value of its
neighbourhood values effectively removes the noise.

In this experiment, you will study the linear and non-linear operations. Specifically,
you will study the effect of changing the size of the neighbourhood and the rule for
combining the pixel values from the neighbourhood, on the result of processing.
Theory
Given an input image f(x,y) an output image g(x,y) is computed by applying some
operation on a local neighbourhood N of each pixel in the image f. This can be
visualized as follows: a window or mask is placed at every pixel location in f(x,y)
and some operation is performed on the pixels within the window. The window is
moved to the next pixel location nd the process is repeated. Thus,

Where HN is the neighborhood operator of size N and g is the output image.

Linear operations :

Linear operations can be represented as a convolution operation between f(x,y) and


a window function w(x,y) as follows.

w is a window function which in practice is in the form of a matrix of


size s x t; a=(s-1)/2 and b=(t-1)/2 .

An example of 3x3 w(x,y) is shown below.

w(-1,-1) w(-1,0) w(-1,1)

w(0,-1) w(0,0) w(0,1)

w(1,-1) w(1,0) w(1,1)

An example of convolution operation is shown below:


Note that w(0,0) is the coefficient or weight of the window function at the centre
which is the position of the current pixel in f(x,y). Varying the weights w(i,j), results
in different type of linear neighborhood operations. The above operations are
generally termed as filtering operations and w is referred to as a filter. A popular
filter is one which performs local averaging or smoothing of the image. This is a low
pass filter. The key characteristic of this filter is that w(i,j) > 0 for every (i,j). An
additional constraint is generally imposed on the weights to sum to 1. Below are
some examples.

Straight averaging filter: All the filter coefficients are equal.

Triangular filter:All the coefficients are arranged to form a pyramid (with triangular
faces) as shown below
Gaussian filter: The filter coefficients form a bell shaped surface as shown below.

Nonlinear Operations:

HN can also be a nonlinear operator. An example is one which performs a ranking


operation. Such operations cannot be represented as a convolution between f(x,y)
and a window function as in the case of linear operations.

Median filter: The operator HN computes the median of the set of grey values in the
neighborhood of the current pixel in f(x,y). This is found by performing a sorting
operation at every location (x,y) in f(x,y).

Unsharp Masking filter: This is another example of nonlinear filtering. Here, the aim
is to highlight the details. This operation is given by

Where is the result of applying an averaging filter


(ex. Gaussian) to the input f(x,y) and  < 1 is a constant. Note that if  =1 then we
have an operation that will retain only the details in the image.

Implementation Issues

When a pixel lies in the border of the input image, some elements of its
neighbourhood is not defined. Hence the mask cannot be applied. To circumvent this
issue, one of the following two approaches can be used :
Zero Padding : The undefined pixels are assumed to be zero and the mask can be
applied. For example: An input image of size MXN and filter size mXn, the output
image size will be (M+(m-1)/2) X (N+(n-1)/2)

Repetition : The border pixels in input image are repeated, so that the
neighbourhood is always defined and mask can be applied.

An example demonstrating Zero-Padding and repitition of border pixels is shown


below:

Procedure
The experiment is designed to understand and learn the Neighborhood operations in
images. This experiment consists of two experiments

Linear filtering

Non-linear filtering

Steps to run the experiments

Select image from the mosaic using 'select image' option

Select region of the image to load it in the input image panel


Select one option from 'Linear filtering’ and ‘Non-linear filtering’

For ‘linear filtering’ select one opetion

Straight average

Triangular average

Gaussian average

For ‘Non-linear filtering’ select one option

Median filter

Unsharp filter

Select one option from ‘Neighborhood size’

Select run option to perform the operations

Output result will be displayed in the output panel

Interesting Observations

Try any filtering option with different neighborhood sizes and compare the results

Try straight average and Gaussian average on same image and same neighborhood
size and compare the results.
Experiment-6
Image Histogram
Objectives
To understand how frequency distribution can be used to represent an image.

To study the correlation between the visual quality of an image with its histogram.

Introduction
A histogram of an image represents the distribution of pixel intensities in the image.
Histograms provide statistical information about the image. These are of use in
applications such as image enhancement, compression and segmentation. For
instance, based on the information present in histogram, it is possible to develop a
transformation function which improves the quality or visual appearance of an
image.

Theory
Given an image A, its histogram H(k) is derived by counting the number of pixels at
every grey level k.

H(k) = Nk k = 0,1,2.....K-1.

where Nk is the count of pixels at gray level k. The total number of bins in this
histogram is K . Theoretically, the maximum value for K is determined by the pixel
depth M of the image. For instance, for an M=8-bit greyscale image, we can have up
to 2M = 256 = K bins and for a binary image (1-bit) we can have just 2 bins.

Sometimes, the value of K is chosen to be different from2M. This will alter the
appearance of the histogram. The example below illustrates this effect.
The histogram of an image is a good indicator of the contrast and brightness of a
given image.

Image appearance Histogram

Dark confined to low gray levels

Bright confined to high gray levels

Poor contrast narrow

High contrast wide with good spread

This property of a histogram is used in content-based retrieval applications to


roughly categorise images. Other uses for image histograms are to quantitatively
describe an image via its pixel statistics: mean value (intensity), median value,
standard deviation and the number of modes.

These information in turn are useful for processing a given image to enhance or
analyse the content of the image which is described in detail next.

Histogram Processing:

The contrast of an image can be modified by manipulating its histogram. A popular


method is via Histogram equalization. Here, the given histogram is manipulated such
that the distribution of pixel values is evenly spread over the entire range 0 to K-1.

Histogram equalization can be done at a global or local level. In the global level the
histogram of the entire image is processed whereas at the local level, the given
image is subdivided and the histograms of the subdivisions (or subimages) are
manipulated individually. When histogram equalization is applied locally, the
procedure is called Adaptive Histogram Equalization.
Procedure
The experiment is designed to understand and learn the image histogram concepts.

Steps to run the experiments

(A) Histogram:

1. Select image from the mosaic using 'select image' option

a) Select region of the image to load it in the input image panel

2. Select one option from 'Full Image Histogram' and 'Divided Histogram'

3. Select run option to perform the operations

a) Output result will be displayed in the output panel

(B) Processing:

1. Select image from the mosaic using 'select image' option

a. Select region of the image to load it in the input image panel

2. Select one option from Global histogram equalization, Local histogram


equalization and Filtering on histogram

3. Select the size of filter from Filtering on histogram option


Interesting Observations
1. Try Global histogram equalization and local histogram equalization on same image
and observe the result.

2. Select different size of filters and observe the change in histograms.

End

You might also like