0% found this document useful (0 votes)
9 views13 pages

B.Sc. (CS) TY Unit4 FOIP (BCS-602)

Bsc notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

B.Sc. (CS) TY Unit4 FOIP (BCS-602)

Bsc notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Unit IV

Intensity Transformation and spatial filtering techniques

Background
The term spatial domain refers to the image plane itself and the methods in this category are
based on the direct manipulation of pixels of an image.
In Intensity transformation, the processing operations are done directly on the pixels present
inside the image.

The spatial domain processes are denoted by the expression-

g(x,y)=T[f(x,y)]

Where, f(x,y) is an input image, g(x,y) is the output i.e. processed image and T is an
operator on f.

The above expression can be written in simplified form as:


s=T(r)
Where, r denotes the intensities of f and s denotes the intensities of g, both at any
corresponding point (x, y) in the images.

Following is the most commonly used intensity transformation function imadjust () function
which can also be called gamma transformation.
Basic intensity transformation using imadjust () function-
The function imadjust () is the basic IPT tool for intensity transformation of gray scale
images or intensity images. It has the basic syntax:
g=imadjust(f,[low_in high_in ],[low_out high_out ],gamma);

In above syntax, f is an input image and g is the mapped image or adjusted image.

The function imadjust() is explained as given below-

PREPARED BY: MS. SHAIKH TAYYABA M. 1 | 13


The function imadjust() maps the intensity values in image f to new values in image g,
such that values between low_in and high_in map to the values between low_out and
high_out.

The intensity values below low_in or equal to low_in will map to low_out and values above
high_in and equal to high_in map to high_out.

The input image can be of data class uint8, uint16 or double class and output image hassame
class as input.

The values of low_in,high_in, low_out, high_out should be in the range[0 1]. The imadjust
function multiplies these values by 255 for unit8, and 65535 for unit16.

Using empty matrix [ ] for [low_in high_in] or for [low_out high_out] results in the default
values [0 1].

If high_out is less than low_out the output intensity is reversed.

The parameter gamma specifies the shape of curve that maps the intensities of Input image f
to g.

The default value of gamma parameter is 1, then it is called as linear mapping. If gamma is
less than 1 then mapping is weighted towards brighter output values and if itis greater than 1
then mapping is weighted towards darker output values.

Example

>> %Intensity transformation using imadjust() function


>> f=imread('rice.png');
>> g=imadjust(f,[0.40 0.60],[0.60 0.70]);
>> %Obtaining the negative image using imadjust function
>> h=imadjust(f,[0 1],[1 0]);
>> subplot(1,3,1),imshow(f),title('Input Image')
>> subplot(1,3,2),imshow(g),title('Adjusted Image')
>> subplot(1,3,3),imshow(h),title('Negative Image using imadjust()')

PREPARED BY: MS. SHAIKH TAYYABA M. 2 | 13


Output

Histogram processing and function plotting


In digital image processing, the image histogram is used for representing digital images in
graphical form.

A histogram has two axes, the x axis and the y axis.

In an image histogram ,the horizontal axis(x axis) of the graph represents the gray level
intensities, while the vertical axis(y axis) represents the total number of pixels or frequency
of these intensities.

The histogram of a digital image with L total possible intensity levels in the range[0,G] is
defined as the discrete function.

h(rk)=nk
Where, rk is the kth intensity level in the interval [0,G] and nk is the number of pixels in the
image whose intensity level is rk.

The value of G is 255 for images of class uint8 and 65535 for images of class uint16 and 1 for
images of class double.

Normally the image histogram can be normalized by simply dividing all elements of h(rk)
with the total number of pixels in the image.

PREPARED BY: MS. SHAIKH TAYYABA M. 3 | 13


It can be denoted as :

P(rk)=h(rk)/n

=nk/n

Where p(rk) is the normalized histogram.

Computing histogram
The basic IPT tool to compute image histogram is imhist() function which has the following
basic syntax:
h=imhist(f,b)
Where, f is an input image,h is its histogram(h(rk)) and b is the number of bins(parts) used
in forming the histogram.

If b is not included as an argument, then it defaults to 256. A bin is simply a subdivision of


intensity scale of input image.

Example

%Computing histogram

>> f=imread('cameraman.tif');

>> size(f)
ans =
256 256

>> 256*256
ans =
65536

>> h=imhist(f,2)
h=
26477
39059

PREPARED BY: MS. SHAIKH TAYYABA M. 4 | 13


Histogram plotting
In Digital Image Processing, histograms are used for graphical representation of pixels of
digital images. It is simply the graphical display of image data using bars of different heights.
Histograms of digital images can be plotted using bar graph, stem graph and plot graph.

1. Bar graph

Histograms are often plotted using bar graphs.

The x axis of the histogram shows the range of intensities or gray levels whereas on the y
axis, it is the frequency(count) of the intensities of each gray level.

If input image is of data class uint8, that means it has 256 different gray levels or shades of
gray in it. That’s why the range of x axis starts from 0 and end at 255.

It’s syntax is:

bar(horz,v,width)

Where, v is a vector containing the points to be plotting, horz is a vector of same size as v
that contains the increment of the horizontal scale and width is a number between 0 and 1.
If horz is omitted, the horizontal axis is divided in units from 0 to length(v).
The default value of width is 0.8.

Example

>> %Bar graph


>> f=imread('cameraman.tif');
>> h=imhist(f);
>> v=h(1:20:256);
>> horz=1:20:256;
>> bar(horz,v);
>> bar(horz,v,0.5);
>> axis( [0 300 0 2500])
>> set(gca,'xtick',0:50:300)
>> set(gca,'ytick',0:250:2500)

PREPARED BY: MS. SHAIKH TAYYABA M. 5 | 13


Output

2. Stem graph
A stem graph is similar to the bar graph. It is also used to plot the image histograms.

It’s syntax is:

stem(horz,v,’color_linestyle_marker’,’fill’)

Where v is a row vector containing the points to be plotting, horz is a vector of same
dimension as v.

The fill keyword fills the specified color into the marker if it is square, circle or diamond.

PREPARED BY: MS. SHAIKH TAYYABA M. 6 | 13


The argument ’color_linestyle_marker’ is described in the following table:

Symbol color Symbol linestyle symbol marker


k black - solid + plus
w white -- dashed * asterisk
r red -. dash dotted x cross
g green : dotted o circle
b blue ‘none’ No line s square
m magenta d diamond
y yellow

Example

>> %stem graph


>> f=imread('coins.png');
>> h=imhist(f);
>> v=h(1:30:256);
>> horz=1:30:256;
>> stem(horz,v,'g:s','fill');

Output

PREPARED BY: MS. SHAIKH TAYYABA M. 7 | 13


3. Plot graph

The plot graph plots a set of points of image histogram and link them with a straight line.

It’s syntax is:

plot(horz,v,’color_linestyle_marker’)

Where, v is a row vector containing the points to be plotting, horz is a vector of same
dimension as v and the argument ’color_linestyle_marker’ is described as above in the table
in stem graph.
Example

>> %plot graph


>> f=imread('coins.png');
>> h=imhist(f);
>> v=h(1:30:256);
>> horz=1:30:256;
>> plot(horz,v,'r-d');

Output:

PREPARED BY: MS. SHAIKH TAYYABA M. 8 | 13


Histogram equalization
An image histogram is a graphical representation of digital image. It is a graph of distribution
of image data. A histogram equalizing means normalizing the intensities of digital image.

Histogram equalization is implemented in the MATLAB toolbox by using function histeq(),


which has the syntax:

g= histeq (f, nlev)

Where f is the input image, nlev is the number of intensity levels(L) specified for the output
image.

In an ideal histogram, there should be pixels of all intensities. An equalized histogram


performs the normalization of intensities by average out all the intensities of input image.

Histogram Equalization of digital image is used to improve the quality of given input image
i.e. contrast level of image.

Example-
>> %Histogram Equalization of digital image in MATLAB
>> f=imread('tire.tif');
>> h=histeq(f);
>> subplot(2,2,1),imshow(f),title('Input Image(f)')
>> subplot(2,2,2),imhist(f),title('imhist(f)')
>> subplot(2,2,3),imshow(h),title('imshow(h)')
>> subplot(2,2,4),imhist(h),title('imhist(h)')

Output-

PREPARED BY: MS. SHAIKH TAYYABA M. 9 | 13


Fundamentals of Filtering, Neighbourhood
In order to improve the quality of an image first we need to improve the image by performing
some operations on it. “Filtering is a technique used for modifying or enhancing an image”

Image processing operations are implemented with filtering include smoothing, sharpening and
edge enhancement.

In Image processing, we can perform two types of processing on the images such as spatial
domain processing and frequency domain processing.

The operations performed in spatial domain of an image again further classified into intensity
transformation and Spatial filtering.

Spatial filtering methods comes under neighbourhood processing.

In neighbourhood processing, instead of performing operations on single pixel, we consider the


neighbourhood of each pixel in an image.

Then perform operations on these surrounded pixels i.e. neighbourhood pixels and assign the
resultant intensity value is to the centered pixel by repeating this process for every pixel in the
image.

The spatial filtering can be of two types:

1) Linear Spatial Filtering


2) Non-linear spatial filtering

1) Linear Spatial Filtering


In this filtering, the computations are performed on pixels of neighbourhood linearly. So it is
called linear spatial filtering.
It consist of multiplying each pixel in the neighbourhood by a corresponding mask
coefficients and summing the results to obtain the response at that point f(x,y).
The coefficients are arranged as a matrix called a filter, mask, filter-mask, kernel, window or
template etc.

The function used to perform linear spatial filtering is imfilter() whose syntax is:
g=imfilter(f,w,’filter_mode’,’boundry_option’,’size_option’);

PREPARED BY: MS. SHAIKH TAYYABA M. 10 | 13


Where f is an input image, w is the filter mask and g is filtered output and other parameters are
described as below:
3) ‘filter_mode’
This parameter consists of two values which are ‘corr’ and ‘conv’. The corr is the default
filter mode option and the use of conv means that filter mask is rotated by 180° prior to
applying it on an image.
4) ‘boundry_option’
It has following values which are explained as below-

a) p
The boundry of the input image is extended by padding with a value p. This is a default
buoundry option with value zero.

b) ‘replicate’
The boundry of the input image is extended by replicating the values in it’s outer border.
c) ‘symmetric’
The boundry of the input image is extended by mirror reflecting the values accross it’s
border.
5) ‘size_option’
It has following values which are explained as below-
a) ‘Same’
The size of the output image is same as an input image.

b) ‘full’
The size of the output image is same as an extended image.
Example

>> %Linear Spatial Filtering


>> f=imread('rice.png');
>> w=[0.2 0 1;0 0.1 0.2;0.2 0.2 0.1];
>> g=imfilter(f,w,'conv',5);
>> subplot(1,2,1),imshow(f),title('Input Image');
>> subplot(1,2,2),imshow(g),title('OutputImage');

PREPARED BY: MS. SHAIKH TAYYABA M. 11 | 13


Output

2) Non-linear spatial filtering


The non-linear spatial filtering is performed in a non-linear base. It is based on
neighbourhood processing and the mechanics of defining MXN neighbourhood by sliding
the center point through an image.
Another basic difference between linear and non-linear is that the concept of mask is not as
prevalent in non-linear processing. It contains an empty filter mask.
The MATLAB toolbox provides a colfilt() function for performing non-linear spatial filtering.

Non-linear spatial filtering using colfilt() function-


The colfilt () function is commonly used for organizing the data in the form of columns
and require more memory.
However, it executes faster than other functions of non-linear spatial filtering. So, it is a
preferred for performing non-linear spatial filtering.

PREPARED BY: MS. SHAIKH TAYYABA M. 12 | 13


The syntax of colfit() function is:

g=colfilt(f,[m n],’sliding’,@function,parametrs)
Where, f is an input image, [m n] is a dimension of filter region,’sliding’ indicates that
the process is one of sliding the MXN region from pixel to pixel in the input image f.
The @ symbol is called a function handle which is any valid matlab function.
The parameters indicate parameters required by the function.
Example
>> %Non-Linear Spatial Filtering
>> f=imread('rice.png');
>> g=colfilt(f,[3 3],'sliding',@min);
>> h=colfilt(f,[3 3],'sliding',@max);
>> i=colfilt(f,[3 3],'sliding',@median);
>> subplot(2,2,1),imshow(f),title('Input Image');
>> subplot(2,2,2),imshow(g),title('Low pass filtering');
>> subplot(2,2,3),imshow(h),title('High pass filtering');
>> subplot(2,2,4),imshow(h),title('Median filtering');

Output:

PREPARED BY: MS. SHAIKH TAYYABA M. 13 | 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy