SAOE
BEII/DIP
E&TC
Assignment No.:1 

Title: Reading a BMP File and displaying its Image Informatics 

Date of performance : 

Date of submission : 

Name of Student: Prassanjeet Singh Dhanjal 

Class : BE [E & TC] 
Division : A 
Roll Number : 52 
Signature of the Teacher: 

Remarks: 
SAOE
BEII/DIP
E&TC
Title:
Assignment No.1
Reading a BMP File and displaying its Informatics.
Objective:
To read a Bitmap file.
To display its image informatics.
Theory: Bitmap File Format (BMP): The Microsoft Windows Bitmap (BMP) file format
is a basic file format for digital images in the Microsoft Windows world. BMP files have:
A file header
A bitmap header
A color table
Image data
The file header occupies the first 14 bytes of all BMP files.
The structure is shown in Table 1
BMP File Header
Bitmap Information
Colour Table
Pixel Data
Table 1 BMP file format
SAOE
BEII/DIP
E&TC
The bitmap header begins with the size of the header, which is then followed by the width and the height of the image data. If the height is a negative number, the image is stored from bottom to up. Usually the number of colour planes is one. The bits per pixel gives the number of bits required to represent the particular pixel. The next two fields deal with image compression. The compression field is 0 for no compression and 1 for runlength encoding compression. The next two fields deal with image compression. The compression field is 0 for no compression and 1 for runlength encoding compression. The next two fields deal with the resolution of the image data and the final two fields deal with the colours or grey shades in the image. The horizontal and vertical resolutions are expressed in pixels per meter. The colour field gives the number of colours or grey shades in the image. A colour table is a lookup table that assigns a grey shade or colour to a number given in the image data. The BMP colour table has 4 bytes in each colour table entry. The bytes are for the blue, green and red colour values. The final part of the BMP file is the image data. The data is stored row by row with padding at the end of each row. It has following advantages:
1. Widely used on most platforms and applications.
2. BMP supports 1bit through 24bit colour depth.
3. It is good to store small image, because the compression is lossless, the detail of the raster image will
be also stored.
Disadvantage of BMP format is that it does not support compression, which results in very large files.
Comparison of various file formats.
Attribute 
JPEG 
GIF 
PNG 
BMP 
Compression 
Lossy 
Lossless 
Lossless 
Lossless 
Bits 
8 bits 
24 bits 
8 or 24 bits 
1,4,8,16,24,32,4 
8, or 64 bits 

Color 
Not Transparent 
Transparent 
Better Transparent 
Not Transparent 
SAOE
BEII/DIP
E&TC
Extraction of BMP header
As we now know that there are following elements in a BMP file, the detailed explanation is given below.
1. File Header
2. Image Information Header
3. Colour Table
4. Pixel Data
The file header:
The file header has exactly 14 bytes in it. The first two bytes must be ASCII codes for the characters ‘B’ and ‘M’. The software should check these values to confirm that the file it is reading is most probably a Windows BMP file. The second field is a four byte integer that contains the size of the file in bytes. The third and fourth fields are each two bytes long and are reserved for future extensions to the format definition. The present definition requires both of these fields be zero. The final four bytes field is an integer that gives the offset of the start of the Pixel data section relative to the start of the file.
Field Name 
Size in Bytes 
Description 
bfType 
2 
The characters "BM" 
bfSize 
4 
The size of the file in bytes 
bfReserved1 
2 
Unused  must be zero 
bfReserved2 
2 
Unused  must be zero 
bfOffBits 
4 
Offset to start of Pixel Data 
Table 2BMP file header
SAOE
BEII/DIP
E&TC
The Image Information Header:
A BMP information header specifies the dimensions, compression type and colour format for the bitmap. There are actually two distinct options for the image information header – one that was developed for the OS/2 BMP format of 12 bytes long and the one that is for the Windows BMP format of 40 bytes long. The first four bytes of each format is the length of the header in bytes and therefore a simple examination of this value tells you which format is used.
Size in 

Field Name 
Bytes 
Description 
biSize 
4 
Header Size  Must be at least 40 
biWidth 
4 
Image width in pixels 
biHeight 
4 
Image height in pixels 
Biplanes 
2 
Must be 1 
biBitCount 
2 
Bits per pixel  1, 2, 4, 8, 16, 24, or 32 
biCompression 
4 
Compression type (0 = uncompressed) 
biSizeImage 
4 
Image Size  may be zero for uncompressed images 
biXPelsPerMeter 
4 
Preferred resolution in pixels per meter 
biYPelsPerMeter 
4 
Preferred resolution in pixels per meter 
biClrUsed 
4 
Number of Colour Map entries that are actually used 
biClrImportant 
4 
Number of significant colours. If 0 then all colours are important. 
Table 3 BMP image information header
SAOE
BEII/DIP
E&TC
The Colour Table:
A colour table is defined as an array of RGB Quad structures, contains as many elements as there
are colours in the bitmap. There is no colour table for 24 bit BMP images as each pixel is represented by 24bit redgreenblue (RGB) values in the actual bitmap data area.
For an 8 bit bitmap image the colour table consists of 256 entries with each entry consisting of four bytes of data. The first three bytes are the blue, green and the red colours respectively. The fourth byte is unused and must be equal to zero.
Field Name 
Size(bytes) 
Description 
RgbBlue 
1 
specifies the blue part of the colour 
rgbGreen 
1 
specifies the green part of the colour. 
rgbRed 
1 
specifies the red part of the colour. 
rgbReserved 
1 
must always be set to zero. 
The Pixel Data
Table 4 BMP Colour Table
In the 8bit format each pixel is represented by a single byte of data, that byte is an index into the
colour table. In 24bit image, each pixel is represented by three consecutive bytes of data that specifies
the blue, green, and red component values respectively. In 24bit BMP file the colour table is absent.
The pixel data is actual image data represented by consecutive rows, or scan lines. The number of bytes in one row must always be adjusted to fit into the border of multiple of four. You simply append zero bytes until the number of bytes in a row reaches of multiple of four.
Pixels are stored "upsidedown" with respect to normal order, starting in the lower left corner, going from left to right, and then row by row from the bottom to the top of the image
SAOE
BEII/DIP
E&TC
The following figure illustrates the pixel data stored in a BMP image. Image is stored from bottom left corner as indicated by the file pointer and then moving upwards row by row.
Figure 1
SAOE
BEII/DIP
E&TC
Program:
#include<stdio.h> #include<conio.h> typedef struct header
{
unsigned short type; unsigned long size; unsigned short reserved1; unsigned short reserved2; unsigned long offbits; unsigned long structsize; unsigned long width; unsigned long height; unsigned long planes; unsigned short bitcount,compression; long xpelparameter,ypelparameter; unsigned long imagesize,cirused,cirimportant; }header;
void main()
{
header bmpheader; FILE *fp; char image[30];
//clrscr(); printf("\n Enter the image file you want to enter "); gets(image); fp=fopen(image,"r"); if (fp==NULL)
{ 

printf("\n Error in opening file "); 

} 

else 

{ 

printf("\n BMP header attributes:> "); 

fread(&bmpheader,sizeof(bmpheader),1,fp); 

printf("\n TYPE : %d ",bmpheader.type); printf("\n SIZE : %d ",bmpheader.size); printf("\n reserved1 : %d ",bmpheader.reserved1); printf("\n reserved2 : %d ",bmpheader.reserved2); 

printf("\n Offbit 
: %d ",bmpheader.offbits); 
printf("\n Structsize : %d ",bmpheader.structsize); printf("\n width : %d ",bmpheader.width); printf("\n height: %d ",bmpheader.height); printf("\n planes : %d ",bmpheader.planes); printf("\n bitcount : %d ",bmpheader.bitcount); 

printf("\n compression : %d ",bmpheader.compression); 

printf("\n xpelparameter : %d ",bmpheader.xpelparameter); printf("\n ypelparameter : %d ",bmpheader.ypelparameter); 

printf("\n imagesize : %d ",bmpheader.imagesize); printf("\n cirused: %d ",bmpheader.cirused); printf("\n cirimportant : %d ",bmpheader.cirimportant); 

} 

getch(); 
}
SAOE
BEII/DIP
E&TC
fig.:Untitled.bmp
SAOE
BEII/DIP
E&TC
Result:
Enter the image file you want to enter C:\TC\BIN\Untitled.bmp
BMP header attributes:> TYPE : 19778
SIZE
reserved1 : 0
reserved2 : 1078
Offbit
Structsize : 53673984
width : 30146560 height: 65536 planes : 8 bitcount : 0
compression
xpelparameter : 5 ypelparameter : 0
imagesize : 0 cirused: 0 cirimportant : 0
: 5
: 2621440
: 49520
SAOE
BEII/DIP
E&TC
Conclusion:
SAOE
BEII/DIP
E&TC
Assignment No.:2 

Title: Estimation of Statistical Parameters of an Image and Histogram Plotting 

Date of performance : 

Date of submission : 

Name of Student: Prassanjeet Singh Dhanjal 

Class : BE [E & TC] 
Division : A 
Roll Number : 52 
Signature of the Teacher: 

Remarks: 
SAOE
BEII/DIP
E&TC
Assignment No.2
Title: Estimation of statistical parameters of an image and histogram plotting.
Objective:
To find the statistical parameters of an image.
To plot the histogram of an image.
Theory:
Moments are applicable to many different aspects of image processing, ranging from invariant pattern recognition and image encoding to pose estimation. When applied to images, they describe the image content (or distribution) with respect to its axes. They are designed to capture both global and detailed geometric information about the image. Here we are using them to characterize a grey level image so as to extract properties that have
analogies in statistics or mechanics. The images are read, and the headers are removed, the core data can be used to calculate the statistical properties of an image.
Mean:
The mean and variance are the first two statistical moments which provide information on the shape of the distribution. The mean of a data set is simply the arithmetic average of the values in the set, obtained by summing the values and dividing by the number of values
∑
∑
SAOE
BEII/DIP
E&TC
Variance and Standard Deviation:
The variance of a data set is the arithmetic average of the squared differences between the values and the mean.
_{∑}
(
)
_{∑}
(
)
The standard deviation is the square root of the variance:
Histogram:
√
An image histogram is a type of histogram that acts as a graphical representation of the tonal distribution in a digital image. It plots the number of pixels for each tonal value. The horizontal axis of the graph represents the tonal variations, while the vertical axis represents the number of pixels in that particular tone. The left side of the horizontal axis represents the black and dark areas, the middle represents medium grey and the right hand side represents light and pure white areas.
A histogram uses a bar graph to profile the occurrences of each grey level present
in an image. The horizontal axis is the greylevel values. It begins at zero and goes to the
number of grey levels (256 in this example). Each vertical bar represents the number of times the corresponding grey level occurred in the image.
In statistics, a histogram is a graphical display of tabulated frequencies. It shows what proportion of cases fall into each of several categories. A histogram differs from a bar chart in that it is the area of the bar that denotes the value, not the height, a crucial distinction when the categories are not of uniform width.
In a more general mathematical sense, a histogram is a mapping m _{i} that counts the
number of observations that fall into various disjoint categories (known as bins), whereas the graph of a histogram is merely one way to represent a histogram. Thus, if we let n be the total number of observations and k be the total number of bins, the histogram m _{i} meets the following conditions:
∑
SAOE
BEII/DIP
E&TC
QUANTITY OF GRAY LEVEL==>
A cumulative histogram is a mapping that counts the cumulative number of observations in all of the bins up to the specified bin. That is, the cumulative histogram Mi of a histogram m _{i} is defined as:
∑
Image histograms can be useful tools for thresholding. Because the information contained in the graph is a representation of pixel distribution as a function of tonal variation, image histograms can be analysed for peaks and/or valleys which can then be used to determine a threshold value.
HISTOGRAM OF IMAGE
1800 




1600 

1400 

1200 

1000 

800 

600 

400 

200 




0 
0
50
100
150
200
250
300
GRAY LEVEL VALUES==>
Sample histogram of cameraman.tif image
SAOE
BEII/DIP
E&TC
Algorithm:
SAOE
BEII/DIP
E&TC
Programs:
%% user defined mean of a matrix function y=u_mean(x) x=double(x);
[r c d]=size(x);
z=0;
for k=1:d for i=1:r for j=1:c
end
end
end
y=z/(r*c*d);
z=z+x(i,j,k);
%% user defined function for variance of a matrix function y=u_var(x) m=u_mean(x);
[r c d]=size(x);
x=double(x);
z=0;
for k=1:d for i=1:r for j=1:c
z=z+(x(i,j,k)m).^2;
end
end
end
y=z/(r*c*d1);
%subtracted 1 from the denominator in %order to calculate variance for %unbiased estimator
%% user defined function for standard deviation of the matrix function y=u_std(x) z=u_var(x); y=sqrt(z);
%% user defined prog. for histogram calculation and plotting function y=u_hist(x) %x=imread('cameraman.tif');
[r c]=size(x);
m=max(max(x));
h=zeros(1,m);
%
z=0;
for i=1:r
for j=1:c
SAOE
BEII/DIP
E&TC
if x(i,j)==0
z=z+1;
else
h(1,x(i,j))=h(1,x(i,j))+1;
end
end
end out=[z,h]; figure,stem(out); figure,bar(out); title('Histogram');
set(gca,'XTick',0:25:m);
y=out;
Program1:
%Practicle #2a %Name : Prassanjeet Singh %(Statistical)To find the Mean, Variance and Standard Deviation of an image.
clc 

clear all 

close all 

%a=[1 2 
3 
; 
4 
5 
6 
; 
7 8 9]; 
a=imread('cameraman.tif');
%imshow(a); b=double(a); [r c]=size(a); l=r*c;
%converting 2D matrix to 1D
z=zeros(1,r*c);
k=0;
for x=1:1:r for y=1:1:c
k=k+1;
z(1,k)=a(x,y);
end
end
%using inbuilt functions
mean1=mean(z)
var1=var(z)
std_deviation=std(z)
%user defined
mean2=u_mean(a)
var2=u_var(a)
std_deviation_u=u_std(a)
%reading the image into a new %variable a
%getting # of rows and columns
%predefining z matrix %initializing a counter
SAOE
BEII/DIP
E&TC
Result:
mean1 = 118.7245
var1 = 3.8865e+003
std_deviation = 62.3417
mean2 = 118.7245
var2 = 3.8865e+003
std_deviation_u =
62.3417
Program2:
%Practical: 2b %Histogram using inbuilt as well as user defined fn
clc
close all
clear all
a=imread('cameraman.tif');
imhist(a);
hist2=u_hist(a);
Result:
SAOE
BEII/DIP
E&TC
Histogram using user defined function:
Fig. Histogram shown using stem function
Fig. Histogram shown using bar function
SAOE
BEII/DIP
E&TC
Conclusion:
SAOE
BEII/DIP
E&TC
Assignment No.: 03 

Title: 
Image enhancement using histogram modelling. 

Date of performance : 

Date of submission : 

Name of Student: Prassanjeet Singh Dhanjal 

Class : 
BE [E & TC] 
Division : A 
Roll Number : 52 
Signature of the Teacher: 

Remarks: 
SAOE
BEII/DIP
E&TC
Assignment No. 3
Title: Image enhancement using histogram modelling.
Objective: Image Enhancement using:
a) Contrast stretching
b) Histogram Equalization
Theory:
Image Enhancement:
Cosmetics, to make the image look good. Image enhancement involves accentuation, or sharpening, of image feature such as edge, boundaries, gray level and contrast manipulation, noise reduction, filtering, interpolation and magnification, pseudo colouring and so on.
The enhancement process does not increase the inherent information content in the image. But it increases the dynamic range of chosen feature so that they can be detected easily.
1. Contrast Stretching
Contrast stretching (often called normalization) is a simple image enhancement technique that attempts to improve the contrast in an image by `stretching' the range of intensity values it contains to span a desired range of values, e.g. the the full range of pixel values that the image type concerned allows. It differs from the more sophisticated histogram equalization in that it can only apply a linear scaling function to the image pixel values. As a result the `enhancement' is less harsh. (Most implementations accept a graylevel image as input and produce another graylevel image as output)
(
(
)
)
SAOE
BEII/DIP
E&TC
Algorithm:
SAOE
BEII/DIP
E&TC
2. Histogram Equalization:
Histogram equalization automatically determines a transformation function seeking to produce an output image with a uniform histogram.
Linear stretching is good, but it does not change the shape of the histogram. In applications where we need a flat histogram, linear stretching fails. To get a flat histogram, we go for histogram equalization.
Transfer function should satisfy the following conditions:
1) T(r) should be single valued and it should be monotonically increasing. 2) 0<=T(r)<=1 for 0<=r<=1
If
(r) = Probability density function of input gray level.
(s) = Probability density function of output gray level.
As per probability theory,
CDF of an image
CDF = _{∫}
S = _{∫}
(s) =
(r)dr
(r)dr
(r)
Taking differentiation on both sides,
=
(r)
Substitute in eq. (1)
eq. (1)
(s) =
(r).
_{(}
_{)}
= 1
SAOE
BEII/DIP
E&TC
Steps for solving numerical on Histogram Equalization
1) For given r and (from image), calculate n. 2) Calculate probability density function of input gray level { ( )} 3) Calculate cumulative distribution function of input gray level {S= T{r} =
∑
(
)}
4) Calculate (L1)S and round off the value. 5) Map old gray level value to new equalized gray level. 6) Plot equalized histogram.
Algorithm for histogram equalization:
SAOE
BEII/DIP
E&TC
Program:
%% user defined prog. for histogram calculation and plotting function y=u_hist(x) %x=imread('cameraman.tif');
[r c]=size(x);
m=max(max(x));
h=zeros(1,m);
%
z=0;
for i=1:r for j=1:c if x(i,j)==0
z=z+1;
else
h(1,x(i,j))=h(1,x(i,j))+1;
end
end
end out=[z,h];
% figure,stem(out);
% figure,bar(out);
% title('Histogram');
% set(gca,'XTick',0:25:m);
y=out;
%% Practical 3: Contrast Stretching and Histogram Equilization
clc
close all
clear all
I=imread('pout.tif');
img=double(I);
imhist(I);
[r c]=size(I);
%% contrast Stretching
p=input('Enter the lower limit for contrast stretching 
'); 
q=input('Enter the upper limit for contrast stretching close all out=zeros(r,c); for i=1:r for j=1:c if (I(i,j)<=p) 
'); 
out(i,j)=0.5*img(i,j);
elseif (I(i,j)>p && I(i,j)<=q)
out(i,j)=2*(img(i,j)p)+0.5*p;
else
out(i,j)=0.5*(img(i,j)q)+0.5*p+2*(qp);
end
end
end
out=uint8(out);
figure
subplot(421)
imshow(I);
SAOE
BEII/DIP
E&TC
title('Original Image>');
subplot(422)
imhist(I); title('Histogram of Original Image>');
subplot(423)
imshow(out); title('Contrast Stretched Image>');
subplot(424)
imhist(out); title('Histogram of Contrast Stretched Image>');
%% Histogram equilization using inbuilt function
subplot(425)
out2=histeq(I);
imshow(out2);
title('Histogram Equalized Image>');
subplot(426)
imhist(out2);
title('Histogram of Histogram Equalized Image>');
%% Histogram Equilization using user defined function
hin1=u_hist(I);
[lr lc]=size(hin1);
maxquanta=2.^(ceil(log2(lc)))1;
tp=r*c;
pdf=hin1/tp;
cdf=zeros(lr,lc);
cdf(1)=pdf(1);
for i=2:lc
cdf(i)=cdf(i1)+pdf(i);
end eqtable=round(cdf*maxquanta);
out2=zeros(r,c);
temp=0;
for i=1:r for j=1:c
out2(i,j)=eqtable(1,I(i,j)+1);
end
end
out2=uint8(out2);
subplot(427)
imshow(out2);
title('Histogram Equalized Image(user defined)>');
subplot(428)
imhist(out2);
title('Histogram of Histogram Equalized Image(user defined)>');
SAOE
BEII/DIP
E&TC
Result:
Enter the lower limit for contrast stretching Enter the upper limit for contrast stretching
100
SAOE
BEII/DIP
E&TC
Conclusion:
SAOE
BEII/DIP
E&TC
Assignment No.:4 

Title :Grey level transformations on an image 

Date of performance: 

Date of submission : 

Name of Student : Prassanjeet Singh Dhanjal 

Class :BE [E & TC] 
Division : A 
Roll Number : 52 
Signature of the Teacher: 

Remarks: 
SAOE
BEII/DIP
E&TC
Assignment No.4
Title: Grey level transformations on an image
Objective: To perform following point processing algorithms on an image
Negative transformation
Log transformation
Power law transformation (Antilog transformation)
Bit plane slicing
Grey level slicing
Theory:
Image enhancement is a very basic image processing task that defines us to have a better subjective judgement over the images. And Image Enhancement in spatial domain (that is, performing operations directly on pixel values) is the very simplistic approach. Enhanced images provide better contrast of the details that images contain. Image enhancement is applied in every field where images are ought to be understood and analysed. For example, medical image analysis, Analysis of images from satellites, etc. Here we discuss some preliminary image enhancement techniques that are applicable for grey scale images.
Image enhancement simply means, transforming an image f into image g using T. Where T is the transformation. The values of pixels in images f and g are denoted by r and s, respectively. As said, the pixel values r and s are related by the expression,
s = T(r)
where T is a transformation that maps a pixel value r into a pixel value s. 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, L1], where L=2 ^{k} , k being the number of bits in the image being considered. So, for instance, for an 8bit image the range of pixel values will be [0, 255]. There are three basic types of functions (transformations) that are used frequently in image enhancement.
SAOE
BEII/DIP
E&TC
They are,
Linear,
Logarithmic,
PowerLaw.
The transformation map plot shown below depicts various curves that fall into the above three types of enhancement techniques.
The Identity and Negative curves fall under the category of linear functions. Identity curve simply indicates that input image is equal to the output image. The Log and InverseLog curves fall under the category of Logarithmic functions and nth root and nth power transformations fall under the category of PowerLaw functions.
Image Negation:
The negative of an image with grey levels in the range [0, L1] is obtained by the negative transformation shown in figure above, which is given by the expression,
s = L  1  r
This expression results in reversing of the grey level intensities of the image thereby producing a negative like image. The output of this function can be directly mapped into the grey scale lookup table consisting values from 0 to L1.
Log Transformations:
The log transformation curve shown in fig. A, is given by the expression,
s = c log (1 + r)
where c is a constant and it is assumed that r≥0. The shape of the log curve in fig. A tells that this transformation maps a narrow range of lowlevel grey scale intensities into a wider range of output values. And similarly maps the wide range of highlevel grey scale intensities into a narrow range of high level output values. The opposite of this applies for inverselog transform. This transform is used to expand values of dark pixels and compress values of bright pixels.
SAOE
BEII/DIP
E&TC
Figure A: Plot of various transformation functions
Power Law Transformation:
Basic form
where C and r are positive constants.
As in log transformation, power law curves with γ<1,
SAOE
BEII/DIP
E&TC
For γ—> maps a narrow range of dark i/p values into a wider range of o/p values and with the opposite being true for higher values of i/p. By varying γ we obtain a family of possible transformation. For curves generated with values γ <1 effect is opposite. Finally the transformation (1) reduces to identity transformation for C= γ=1.
A variety of devices for image capture, printing, and display respond according to a power law. The exponent in power law equation is referred to as gamma Þ process used to correct this power law response phenomena is called gamma correction. E.g. CRT devices have intensity. Vs voltage response as a power functions with γ varying from 1.8 to 2.5. With γ=2.5, the CRT would produce images darker than intended. E.g. i/p is a simple grey scale linear wedge .To reproduce colours accurately also requires knowledge of gamma correction as changing the value of gamma changes not only the brightness but also the colour ratios of R:G:B. Gamma correction is extremely important as use of digital images for commercial purposes over the internet has increased.
SAOE
BEII/DIP
E&TC
This transformation function is also called as gamma correction. For various values of γ different levels of enhancements can be obtained. This technique is quite commonly called as Gamma Correction. If you notice, different display monitors display images at different intensities and clarity. That means, every monitor has builtin gamma correction in it with certain gamma ranges and so a good monitor automatically corrects all the images displayed on it for the best contrast to give user the best experience.
SAOE
BEII/DIP
E&TC
The difference between the logtransformation function and the powerlaw functions is that using the powerlaw function a family of possible transformation curves can be obtained just by varying the λ.
These are the three basic image enhancement functions for grey scale images that can be applied easily for any type of image for better contrast and highlighting. Using the image negation formula given above, it is not necessary for the results to be mapped into the grey scale range [0, L1]. Output of L1r automatically falls in the range of [0, L1]. But for the Log and PowerLaw transformations resulting values are often quite distinctive, depending upon control parameters like λ and logarithmic scales. So the results of these values should be mapped back to the grey scale range to get a meaningful output image. For example, Log function s = c log (1 + r) results in 0 and 2.41 for r varying between 0 and 255, keeping c=1. So, the range [0, 2.41] should be mapped to [0, L1] for getting a meaningful image.
Bit Plane Slicing:
Instead of highlighting grey level images, highlighting the contribution made to total image appearance by specific bits might be desired. Suppose that each pixel in an image is represented by 8 bits. Imagine the image is composed of 8, 1bit planes ranging from bit plane10 (LSB) to bit plane 7 (MSB).In terms of 8bits bytes, plane 0 contains all lowest order bits in the bytes comprising the pixels in the image and plane 7 contains all high order bits.
Separating a digital image into its bit planes is useful for analysing the relative importance played by each bit of the image, implying, it determines the adequacy of numbers of bits used to quantize each pixel, useful for image compression. In terms of bitplane extraction for a 8bit image, it is seen that binary image for bit plane 7 is obtained by proceeding the input image with a thresholding greylevel transformation function that maps all levels between 0 and 127 to one level (e.g. 0)and maps all levels from 129 to 253 to another (e.g. 255).
SAOE
BEII/DIP
E&TC
Grey level slicing:
Highlighting a specific range of graylevels in an image is often desired. Applications include enhancing features such as masses of water, crop regions, or certain elevation area in satellite imagery. Another application is enhancing flaws in xray. There are two main different approaches:
Highlight a range of intensities while diminishing all others to a constant low level.
Highlight a range of intensities but preserve all others.
The fig. illustrates the intensity level slicing process. The left figures show a transformation function that highlights a range [A, B] while diminishing all the others. The right figures highlights a range [A, B] but preserves all the others.
SAOE
BEII/DIP
E&TC
SAOE
BEII/DIP
E&TC
Algorithm:
SAOE
BEII/DIP
E&TC
Programs:
%% User Defined function to convert greyscale image to binary sliced image % 1st plane refers to the LSB plane function y=u_grey2binslice(x) %x=[255 230 40;29 140 239; 89 90 100]; %x=imread('cameraman.tif');
[r c]=size(x);
x=double(x); m=max(max(x));
l=ceil(log2(double(m)));
img=zeros(r,c,l); for i=1:r for j=1:c temp=x(i,j); for k=1:l
temp_r=rem(temp,2);
temp=floor(temp/2);
img(i,j,k)=temp_r;
end
end
y=img;
end
%% User defined function to convert binary sliced image to greyscale
function y=u_binslice2grey(x)
[r c l]=size(x);
img=zeros(r,c);
for i=1:r for j=1:c
temp=0;
for k=1:l
temp=temp+x(i,j,k)*2.^(k1);
end
img(i,j)=temp;
end
end
y=img;
%% Practical4: Grey Level Transformations on an Image %(a)Negative Transformation (b)Thresholding %(c)Greyscale Slicing (d)Log Transf. %(e)Power Law Transformation (f)Bit Plane Slicing clc close all clear all
a=imread('cameraman.tif');
%a=imread('moon.tif');
%a=imread('coins.png');
[r c]=size(a);
SAOE
BEII/DIP
E&TC
L=max(max(a)); b=double(a); %% Negative Transformation negative=zeros(r,c); for i=1:r for j=1:c negative(i,j)=La(i,j);
end
end
negative=uint8(negative);
%% Thresholding T=input('Please enter the threshold level between 0 to 255= '); thresimg=zeros(r,c); for i=1:r for j=1:c if a(i,j)>= T
thresimg(i,j)=255;
end
end
end
%% GreyScale Slicing %without Background T1=input('Enter the lower value for slicing= '); T2=input('Enter the upper value for slicing= '); sliceimwobg=zeros(r,c); for i=1:r for j=1:c if a(i,j)>= T1 && a(i,j)<=T2
sliceimwobg(i,j)=255;
end
end
end
%with background sliceimwbg=a; for i=1:r for j=1:c if a(i,j)>= T1 && a(i,j)<=T2
sliceimwbg(i,j)=255;
end
end
end
%% Log Transformation C1=input('Enter the coefficient for logarithmic transform= '); logtransimg=zeros(r,c); for i=1:r for j=1:c
logtransimg(i,j)=C1*log(1+b(i,j));
end
end
logtransimg2=uint8(logtransimg);
SAOE
BEII/DIP
E&TC
%% Power Law Transformation C2=input('Enter coefficient for Power Law transform= '); gamma=input('Enter Gamma factor= '); powlawtransimg=zeros(r,c); for i=1:r for j=1:c
powlawtransimg(i,j)=C2*(b(i,j).^gamma);
end
end
powlawtransimg=uint8(powlawtransimg);
%% Power Law Second Method
powlaw2=zeros(r,c);
for i=1:r for j=1:c
powlaw2(i,j)=exp(logtransimg(i,j)./C1)1;
end
end
powlaw2img=uint8(powlaw2);
%% Bit Plane Slicing
bitsliced=u_grey2binslice(a);
a1=bitsliced(:,:,1);
a2=bitsliced(:,:,2);
a3=bitsliced(:,:,3);
a4=bitsliced(:,:,4);
a5=bitsliced(:,:,5);
a6=bitsliced(:,:,6);
a7=bitsliced(:,:,7);
a8=bitsliced(:,:,8);
combined=uint8(u_binslice2grey(bitsliced));
%% Image Show imshow(a); figure
subplot(3,3,1);
imshow(a); title('Original');
subplot(332)
imshow(negative); title('Negative of Original');
subplot(333)
imshow(thresimg); title('Thresholded Image');
subplot(334)
imshow(sliceimwobg); title('Grey Level Sliced Image without Background');
subplot(335)
imshow(sliceimwbg); title('Grey Level Sliced Image with Background');
subplot(336)
imshow(logtransimg2);
title('Log Transformed Image');
subplot(337)
imshow(powlawtransimg); title('Power Law Transformed Image');
SAOE
BEII/DIP
E&TC
figure,subplot(331);
imshow(a);
subplot(332)
imshow(a8);
subplot(333)
imshow(a7);
subplot(334)
imshow(a6);
subplot(335)
imshow(a5);
subplot(336)
imshow(a4);
subplot(337)
imshow(a3);
subplot(338)
imshow(a2);
subplot(339)
imshow(a1);
figure,imshow(powlaw2img);
title('original from power law antilog'); figure,imshow(combined);
Results:
Please enter the threshold level between 0 to 255= 125 Enter the lower value for slicing= 34 Enter the upper value for slicing= 45 Enter the coefficient for logarithmic transform= 25 Enter coefficient for Power Law transform= 1.2 Enter Gamma factor= 1.1
SAOE
BEII/DIP
E&TC
SAOE
BEII/DIP
E&TC
SAOE
BEII/DIP
E&TC
Conclusion and Inferences:
SAOE
BEII/DIP
E&TC
Assignment No.:5 

Title: Spatial domain filtering of images. 

Date of performance : 

Date of submission : 

Name of Student: Prassanjeet Singh Dhanjal 

Class : BE [E & TC] 
Division : A 
Roll Number : 52 
Signature of the Teacher: 
SAOE
BEII/DIP
E&TC
Assignment No.5
Title: Spatial domain filtering of images.
Objective:
o Read an image file and perform
Spatial domain high pass filtering (HPF)
Spatial domain low pass filtering (LPF)
Spatial domain median filtering
Spatial domain highboost filtering
Theory:
Spatial frequencies:
All images and pictures contain spatial frequencies. Most of us are familiar with some type of frequency such as the 60cycle, 110volt electricity in our homes. The voltage varies in time as a sinusoid, and the sinusoid completes a full cycle 60 times in a seconda frequency of 60 Hz. Images have spatial frequencies. The grey level in the image varies in space (not time), i.e. it goes up and down.
Filtering:
Filtering is also a common concept. Adjusting the bass and treble on stereos filters out certain audio frequencies and amplifies others. High pass filters pass high frequencies and stop low frequencies. Low pass filters pass low frequencies and stop high frequencies. In the same manner it is possible to filter spatial frequencies in images. A highpass filter will amplify or pass frequent changes in gray levels and a lowpass filter will reduce frequent changes in gray levels.
SAOE
BEII/DIP
E&TC
Application of spatial image filtering:
Spatial image filtering has several basic applications in image processing. Among these are noise removal, smoothening and edge enhancement. Noise in an image usually appears as snow (white or black) randomly sprinkled over an image. Spikes or very sharp, narrow edges in the image cause snow. A low pass filter smoothes and often removes these sharp edges.
Convolution:
At the heart of any filtering process is the operation of convolution. Often a mask (2D) is convolved with an image to obtain the desire effect.
Following image describes the operation of convolution:
Low pass filtering:
(
)
∑
∑
(
)
(
)
Low pass filtering smoothes out sharp transitions in grey levels and removes noise. The output (response) of low pass filter is simply the average of the pixels contained in the neighbourhood of the filter mask. The averaging is achieved through spatial integration.
Low pass filter in which all coefficients are equal is called as box filter.
Following figure shows four low pass filter convolution masks. Convolving these filters with a constant gray level area of an image will not change the image. Notice how the second convolution mask replaces the center pixel of the input image with the average gray level of the area. The outer three masks have the same general form a peak in the centre with small values at the corners.
SAOE
BEII/DIP
E&TC
Low pass filtering:
High pass filtering:
1 
1 
1 

1/9 * 
1 
1 
1 

1 
1 
1 

0 
1 
0 

1/6 
* 
1 
2 
1 
0 
1 
0 

1 
1 
1 

1/10 * 
1 
2 
1 

1 
1 
1 

1 
2 
1 

1/16 * 
2 
4 
2 

1 
2 
1 

0 
1 
0 

1 
5 
1 

0 
1 
0 

1 
1 
1 

 1 
9 
1 

1 
1 
1 

1 
2 
1 

2 
5 
2 

1 
2 
1 
High pass filters amplify or enhance a sharp transition (an edge) in an image. Sharpening of image or high pass filtering is achieved through spatial differentiation.
SAOE
BEII/DIP
E&TC
Image differentiation enhances edges and other discontinuities such as noise and deemphasizes areas with slowly varying grey level values.
Above figure shows three 3X3 high pass filter convolution masks. Each will leave a homogenous area of an image unchanged. They all have the same form a peak in the centre, negative values above, below, and to the sides of the centre, and corner values near zero. The three masks, however, produce different amplifications to different spatial frequencies.
Median Filters:
Order statistics filter s are nonlinear spatial filters whose response is based on ordering the pixels contained in the image area encompassed by the filter and then replacing the value of the centre pixel with the value determined by the ranking result. Median filter is the best known example.
Median filter uses an empty mask. The median filter takes an area of an image (3X3, 5X5, 7X7 etc.) looks at all the pixel values in that area and replaces the centre pixel with the median value. The median filter does not require convolution. It does however, require sorting the values in the image area to find median value.
Median filters are particularly effective in the presence of impulse noise also called as salt and pepper noise.
High boost filtering:
High pass filters gives good result at the cost of background. At times, we need to enhance the edges and would also like to retain some part of background. A solution to this is a modified version of high pass filter called as highboost filtering.
We can think of high pass filtering in terms of subtracting a low pass image from the original image, that is,
High pass = Original  Low pass.
However, in many cases where a high pass image is required, we also want to retain some of the low frequency components to aid in the interpretation of the image. Thus, if we multiply the original image by an amplification factor A before subtracting the low pass image, we will get a high boost or high frequency emphasis filter. Thus,
SAOE
BEII/DIP
E&TC
High boost
= A*Original – Low pass
= (A1)*Original + OriginalLow pass
= (A1).Original + High pass
Now, if A = 1 we have a simple high pass filter. When A > 1 part of the original image is retained in the output.
A simple filter for high boost filtering is given by
1 
1 
1 

(1/9) * 
1 
X 
1 
1 
1 
1 
Where X=9A1
For example, if A=1.1, then
X=9A1 

So, 
X=8.9 
This implies that 10% of background of image is preserved.
GENERAL PROCEDURE TO BE FOLLOWED FOR FILTERING:
1. Take a mask of (3x3, 5x5, 7x7 etc.) size.
2. Place this mask on the image.
3. Multiply each component of the mask with the corresponding value of the image.
4. Add up all the values and place it in the centre.
5. Shift the mask by one pixel along the row till the row is completed.
6. Repeat the same procedure for each row
This operation is called as linear spatial filtering and it is to be followed for all the filters.
SAOE
BEII/DIP
E&TC
Algorithm:
SAOE
BEII/DIP
E&TC
Programs:
%% User defined Mean Filter(odd order window) function y=u_meanfltr(x,w,C) b=double(x);
[r c]=size(x);
w=w/C; %w=input('Enter the window i.e. square matrix for LPF: '); %C=input('Enter Division factor: '); [wr wc]=size(w);
wrm=ceil(wr/2);
wcm=ceil(wc/2);
mnfimg=zeros(r,c); for i=wrm:rwrm+1 for j=wcm:cwcm+1
temp=0;
for wi=1:wr for wj=1:wc temp=(w(wi,wj)*b((iwrm+wi),(jwcm+wj)))+temp;
end
end
mnfimg(i,j)=temp;
end
end
y=uint8(mnfimg);
%% User defined Median Filter(odd order window) function y=u_medfltr(x,S) b=double(x);
[r c]=size(x);
%S=input('Enter the window size i.e. square matrix for median filter: ');
%w=ones(S,S);
wrm=ceil(S/2);
wcm=ceil(S/2);
mdnfimg=zeros(r,c);
temp=zeros(1,S*S);
for i=wrm:rwrm+1 for j=wcm:cwcm+1
k=1;
for wi=1:S for wj=1:S
temp(1,k)=b((iwrm+wi),(jwcm+wj));
k=k+1;
end
end
mdnfimg(i,j)=median(sort(temp));
end
end
y=uint8(mdnfimg);
SAOE
BEII/DIP
E&TC
%% Practical 5: Filter Design %% I. LPF clc close all clear all
a=imread('cameraman.tif'); [r c]=size(a);
subplot(221)
imshow(a);
title('Original Image
%% user defined method %% LPF w1=[1 1 1;1 1 1;1 1 1];
C1=9;
b=u_meanfltr(a,w1,C1);
subplot(222)
imshow(b); title('Mean filtered Image ');
');
wfive=[1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1];
Cfive=25;
bfive=u_meanfltr(a,wfive,Cfive);
subplot(223)
imshow(bfive); title('Mean filtered Image 5x5 mask'); %%
order=3;
d=u_medfltr(a,order); figure
subplot(221)
imshow(d); title('Median filtered Image '); n=imnoise(a,'salt & pepper',0.1);
subplot(222)
imshow(n); title('Image with 10% added noise of type salt n pepper');
d1=u_medfltr(n,3);
d2=u_medfltr(n,5);
subplot(223)
imshow(d1);
title('Image after median filtering with 3x3 window ');
subplot(224)
imshow(d2);
title('Image after median filtering with 5x5 window '); %% HPF w2=[1 1 1;1 8 1;1 1 1];
C2=1;
f=u_meanfltr(a,w2,C2);
figure,imshow(f);
title('High Pass Filtered Image
');
%% HBF 

X1=1.05; 
%5 percent 
X2=1.1; 
%10 percent 
X3=1.2; 
%20 percent 
w3=[1 1 1;1 9*X11 1;1 1 1]; w4=[1 1 1;1 9*X21 1;1 1 1];
SAOE
BEII/DIP
E&TC
w5=[1 1 1;1 9*X31 1;1 1 1];
g=u_meanfltr(a,w3,2);
h=u_meanfltr(a,w4,2);
l=u_meanfltr(a,w5,2);
figure;
subplot(221)
imshow(g); title('High Boost Filtered Image with 5% background ');
subplot(222)
imshow(h); title('High Boost Filtered Image with 10% background ');
subplot(223)
imshow(l); title('High Boost Filtered Image with 20% background ');
Results:
SAOE
BEII/DIP
E&TC
SAOE
BEII/DIP
E&TC
SAOE
BEII/DIP
E&TC
Conclusion:
SAOE
BEII/DIP
E&TC
Assignment No.:6 

Title: Discrete Cosine Transform and Inverse Discrete Cosine Transform of an image. 

Date of performance : 

Date of submission : 

Name of Student: Prassanjeet Singh Dhanjal 

Class : BE [E & TC] 
Division : A 
Roll Number : 52 
Signature of the Teacher: 

Remarks: 
SAOE
BEII/DIP
E&TC
Assignment No.6
Title: To Perform Discrete Cosine Transform and Inverse Discrete Cosine Transform of an image.
Objective:
Read an image file and perform 1) Discrete Cosine Transform (DCT) 2) Inverse Discrete Cosine Transform (IDCT) on the DCT image
Theory:
Need of Transform:
Transform are used in image filtering, image data compression, image description, etc. The rapid growth of digital imaging applications, including desktop publishing, multimedia, teleconferencing, and highdefinition television (HDTV) has increased the need for effective and standardized image compression techniques. Among the emerging standards are JPEG, for compression of still images.MPEG, for compression of motion video and for compression of video telephony and teleconferencing.
DCT Importance:
Discrete Cosine Transforms (DCTs) express a function or a signal in terms of a sum of sinusoids with different frequencies and amplitudes. A DCT operates on a function at a finite number of discrete data points. The discrete cosine transform (DCT) is a technique for converting a signal into elementary frequency components. It is widely used in image compression. N*M image is subdivided in set of smaller n*m sub image and DCT is computed for each of sub image. Within each of sub image only non negligible components retained i.e. compression is achieved. Other DCT components are assumed to be zero. A fast version of the DCT is available, like the FFT, and calculation can be based on the FFT. Both implementation offers about the same speed. The Fourier transform is not actually optimal for image coding since DCT can give higher compression rate, for the same image quality.
OneDimensional Discrete Cosine Transform:
1d DCT of a sequence f(x) of length ‘L’ is given as,
SAOE
BEII/DIP
E&TC
f 
(u) = α (u) ∑ 
( 
) 
(( 
) 
) 
for 
0 ≤ u ≤ N1 
1 

Where, α (u) ={√ 
} 
u=0 


2 

= {√ 
} 
1 ≤ u ≤ N1 
α (u) is required to make it unitary or orthogonal
One Dimensional Inverse Discrete Cosine Transform:
f 
(x) = α (u) ∑ 
( 
) 
(( 
) 
) 
for 
0 ≤ u ≤ N1 

3 

Where, α (u) = {√ 
} 
u=0 

4 

= 
{√ 
} 
1 
≤ u ≤ N1 
This equation expresses ‘f’ as a linear combination of the basis vectors. The coefficients are the elements of the inverse transform, which may be regarded as reflecting the amount of each frequency present in the input ‘f’. The onedimensional DCT is useful in processing onedimensional signals such as speech waveforms.
TwoDimensional DCT:
f 
(u, v) = α (u)*α (v) ∑ 
∑ 
( 
) 
(( 
) 
Where, α (u) = { 
_{√} 
for 
u=0 

= { 
_{√} 
for 1 ≤ u ≤ M1 

α (v) = { 
_{√} 
for 
v=0 
= {
_{√}
for 1 ≤ v ≤ M1
)
((
for 
0 ≤ u ≤ M1 0 ≤ v ≤ N1 
5 

6 
)
)
Since the 2D DCT can be computed by applying 1D transforms separately to the rows and columns, we say that the 2D DCT is separable in the two dimensions.
SAOE
BEII/DIP
E&TC
TwoDimensional IDCT:
F(x, y) =∑
∑
α(
)α( )
(
Where, α (u) = {1/√
)
((
for u=0
)
)
= 
{ 
_{√} 
for 
1 ≤ u ≤ M1 
α (v) = {2/√ 
for 
v=0 

= 
{ 
_{√} 
for 
1 ≤ v ≤ N1 
Properties of DCT:
((
)
)
for 0 ≤ m ≤ M1 0 ≤ n ≤ N1
7
8
1. Due to its mirror symmetry property it produces less degradation at each sub image boundaries than DFT.
2. DCT has periodicity 2N. Since only real coefficients are used memory requirement of DCT is less than DFT
3. Decorrelation  The principle advantage of image transformation is the removal of redundancy between neighboring pixels. This leads to uncorrelated transform coefficients which can be encoded independently.
4. Energy Compaction  DCT exhibits excellent energy compaction for highly correlated images. The uncorrelated image has its energy spread out, whereas the energy of the correlated image is packed into the low frequency region.
5. Orthogonality  IDCT basis functions are orthogonal. Thus, the inverse transformation matrix of A is equal to its transpose i.e. inv A=A'.
6. Separability – Perform DCT operation in any of the direction first and then apply on second direction, coefficient will not change.
SAOE
BEII/DIP
E&TC
Algorithm:
SAOE
BEII/DIP
E&TC
Program:
%% user defined function to find DCT and IDT
function [y1,y2]=u_dct(x) [N M]=size(x); x=double(x); C=zeros(N,N);
const1=sqrt(1/N);
const2=sqrt(2/N);
for i=1:N for j=1:M if i==1
C(i,j)=const1;
else
C(i,j)=const2*cos((2*(j1)+1)*(i1)*pi/(2*N));
end
end
end
y1=C*x*C';
y2=C'*y1*C;
x=uint8(x);y2=uint8(y2);
figure,imshow(x);title('Original Image'); figure,imshow(uint8(y1));title('DCT Image'); figure,imshow(uint8(y2));title('Retrieved Image');
%% prac 6 %% dct and idct
clc close all clear all a=imread('lena.tif'); [b,d]=u_dct(a);
c=dct2(a);
figure,imshow(uint8(c));
title('DCT using inbuilt Function');
Result:
SAOE
BEII/DIP
E&TC
SAOE
BEII/DIP
E&TC
Conclusion:
SAOE
BEII/DIP
E&TC
Assignment No.:7 

Title: Edge detection using different operators. 

Date of performance :13/9/12 

Date of submission : 

Name of Student: Prassanjeet Singh Dhanjal 

Class : BE [E & TC] 
Division :A 
Roll Number : 52 
Signature of the Teacher: 

Remarks: 
SAOE
BEII/DIP
E&TC
Title: Edge detection
Assignment No.7
Objective: To understand and implement edge detection techniques based on:
1. Ordinary operator.
2. Roberts operator.
3. Sobel operator.
4. Prewitt operator.
5. Canny operator.
Theory:
An edge in an image is a boundary or contour at which a significant change occurs in some physical aspect of an image, such as illumination or the distances of the visible surfaces from the viewer. Changes in physical aspects manifest themselves in a variety of ways, including changes in intensity, color, and texture. Detecting edges is very useful in a number of contexts. For example, in a typical image understanding task such as object identification, an essential step is to segment an image into different regions corresponding to different objects in the scene. Edge detection is the 1 ^{s}^{t} step in image segmentation. A high amount of semantic information about image content is conveyed by shapes of objects. In a simplistic view 'edge' is a discontinuity of amplitude. In natural images it will barely happen that an edge sharply separates two distinct plateaus of amplitude. This type of "step edges" can be found in synthetically generated graphics images. In natural images, due to shadows and reflections, the type of "ramp edges" is a better model characterized by slope width "a" and edge slope 'b' Natural edges are even more smooth as shown below. The slope is not constant over the edge such that a point of maximum slope edge can be identified.
Amplitude B
Step
Amplitude A
Amplitude
Amplitude
a
B
Max slope
Amplitude A
a
Detecting edges is a basic operation in image processing.
Fig1.Different types of edges.
SAOE
BEII/DIP
E&TC
For detection of edges, we have to use mask processing. In mask processing the mask is shifted over the entire image to calculate weighted sum of pixels at a particular location.
E.g.:
∑
∑
(
)
Depending on the coefficients of these masks, we can perform different types of image processing operations.
EDGE DETECTION:
Consider the following example.
A
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 
B 
Intensity profile along Horizontal line
Taking its first derivative
Taking second derivative
Taking modulus
In the above example, consisting of two images a and b, it can be seen that images a and b consists of two main intensity levels. The separation of these intensity levels is the edge of the image.
It is observed that the intensity profile changes gradually. Because of quantization and sampling, the abrupt changes in the image are converted to gradual ones.
The first derivative responds whenever there is a transition in the intensity levels. The first derivative is positive at the leading edge (brighter side of edge) and negative at the falling edge (darker side of the edge).
Now, we will see how to apply these derivatives. The derivative can be found using the gradient operation.
SAOE
BEII/DIP
E&TC
Image gradient:
The tool of choice for finding edge strength and direction at location (x, y) of an image, f,
is the gradient denoted by,
f and defined as the vector
f = grad (f) =
=
 (1)
This vector has the important property that it points in the direction of the greatest rate of
f, denoted as M(x, y), where
change of f at location (x, y). The magnitude (length) of the vector
M(x, y) = mag (
f ) = (g _{x} ^{2} + g _{y} ^{2} ) ^{1}^{/}^{2} (2)
^{≈} g _{x}  +g _{y} 
The direction of gradient vector f:
( )
^{}^{1} ( g _{y} /g _{x} ).(3)
It tells us direction of gradient vector f. But the direction of the edge is perpendicular to it.
Using the gradient operators we can find direction as well as the strength of edge at a particular location (x, y).
Computing Gradient:
Case 1: 1D data
Y=f(x)
Differentiate (3) w.r.t. x
=
^{(}
^{)}
^{(} ^{)} (4)
The slope is
SAOE
BEII/DIP
E&TC
Case 2: 2D data
There are two variables x & y in 2D data. For finding gradient we work with each variable separately. i.e. we take partial derivatives. Therefore, we differentiate w.r.t x and keep y fixed.
=
^{(}
^{)}
^{(}
^{)} (5)
Now we differentiate w.r.t y and keep x fixed.
^{(} 
^{)} 
^{(} 

i 
+ j 

) 
( 
) 
^{)} (6)
=
Hence, the final gradient is
(7)
Its magnitude is given as

=√(
(8)
Finding gradient using masks
Considering a (3X3) neighbourhood with Z5 as the origin
Z1 
Z2 
Z3 
Z4 
Z5 
Z6 
Z7 
Z8 
Z9 
=
^{(}
^{)}
^{(}
^{)} (9)
=
^{(}
^{)}
^{(}
^{)} (10)
In discrete domain, h=k=1
= f(x+1,y) – f (x,y)(11)
SAOE
BEII/DIP
E&TC
= f(x,y+1) – f (x,y)(12)
From the (3X3) neighbourhood, we have
= Z8Z5 and
Therefore,
= Z6Z5
 F=Z8Z5+Z6Z5.(13)
This is the first order difference gradient. This can be implemented using 2 masks.
Z8Z5=
1.
0
1
0
Mask 1(along x gradient).
Z6Z5=
1.
1
0
0
Mask 2(along y gradient).
2
1
1
0
Combined mask.
This mask is known as ordinary operator.
SAOE
BEII/DIP
E&TC
Roberts mask.
 F=Z9Z5+Z6Z8(14)
Z9Z5=
1.
0
0
1
Mask 1(along x gradient).
Z6Z8=
0.
1
1
0
Mask 2(along y gradient).
Prewitt operator
1
1
1
1
Combined mask.
 F= (Z7+Z8+Z9)(Z1+Z2+Z3) + (Z3+Z6+Z9)(Z7+Z4+Z1)(15)
Prewitt edge operator 

1 
0 
1 

1 
0. 
1 

1 
1 
1 

1 
0 
1 

0 
0. 
0 

1 
1 
1 

Horizontal 
Vertical 
SAOE
BEII/DIP
E&TC
Sobel operator
 F= (Z7+2Z8+Z9)(Z1+2Z2+Z3) + (Z3+2Z6+Z9)(Z7+2Z4+Z1)(16)
Sobel edge operator. 

1 
2 
1 
1 
0 
1 
0 
0 
0 
2 
0 
2 
1 
2 
1 
1 
0 
1 
Horizontal 
Vertical 

II Derivative: 
In the case of second derivative, it is positive on darker side of edge and negative on brighter side of the edge. However, it is observed that the second derivative is very sensitive to noise present in the image. Also, they give double edges. Hence, they are not usually used for edge detection. But as its nature suggests we can use the sign of second derivative to determine whether a point is lying on the darker side of an edge or the brighter side. Also it is observed that, there are some zero crossings in the second derivative. This can be used to identify location of edge whenever there is a gradual transition of intensity.
Masks for second derivative:
Laplacian operator:
+
(17)
= f(x+1,y) – f (x,y)(18)
= f(x,y+1) – f (x,y)(19)
SAOE
BEII/DIP
E&TC
The second derivative is given by
Where
^{2} (f ) = [
^{2} (f)/
x ^{2} ] +
[
^{2} (f)/
y ^{2} ].(20)
[
^{2}
(f)/
x ^{2} ]=f(x+1, y)+f( x1,y) 2f(x,y)(21)
[
^{2}
(f)/
y ^{2} ]=f(x,y+1)+f( x,y1) 2f(x,y)(22)
Therefore,
^{2} (f ) = f(x+1, y)+f( x1,y)+ f(x,y+1)+f( x,y1)4f(x,y)(23)
Considering (3X3) neighbourhood,
^{2} (f ) = (Z2+Z4+Z6+Z84Z5)(24)
0 
1 
0 
1 
4 
1 
0 
1 
0 
This is known as Laplacian operator.
If we also consider the diagonal elements for second derivative, in that case the mask will be as follows:
1 
1 
1 
1 
8 
1 
1 
1 
1 
This operator is very sensitive to noise and so cannot be used for edge detection. To make it useful, the image is 1 ^{s}^{t} smoothed using a Gaussian operator and that smoothed image can be operated by Laplacian operator. This operator is called as LoG (Laplacian of Gaussian operator).
SAOE
BEII/DIP
E&TC
h(x,y) = exp((x ^{2} +y) ^{2} /2 ^{2} )(25)
 standard deviation.
Let
x ^{2} +y ^{2} =r ^{2}
then Laplacian of the edge can be written as
^{2} h = (( r ^{2} –
^{2} )
/
^{4} ) exp ( r ^{2} /2 ^{2} )(26)
NOTE:  LAPLACIAN OPERATOR ARE ISOTROPIC FILTERS. THAT IS THEIR REPONSE IS INDEPENDENT OF THE DIRECTION OF THE DISCONTINOUS IN THE IMAGE. THEY ARE “ROTATION INVARIANT”.
Image of the LoG operator.
Gaussian mask in two dimension.
SAOE
BEII/DIP
E&TC
Mask for LoG operator:
Laplacian of Gaussian.
Гораздо больше, чем просто документы.
Откройте для себя все, что может предложить Scribd, включая книги и аудиокниги от крупных издательств.
Отменить можно в любой момент.