You are on page 1of 16

# Lab Manual

ECS 702
Digital Image Processing

Even Semester 2013
B.Tech (CSE)
VII Semester

List Of Programs (Practicals) Sl No. They should be able to write and implement the programs in c language with turbo c compilers. Outcomes: After knowing all the mentioned below programs either in C or in Matlab the students will become to do research by applying different techniques of problem identification and their solution with innovative methodologies. Program Name Language 1 C or Matlab Program in Matlab for digital Negative.Prerequisite : Student has knowledge about the c and matlab. The basic functional aspect of Matlab should be known by the students. 2 C or Matlab A. Program for Median Filter C or Matlab 3 Program for Histogram Equilization Matlab 4 Program for Low Pass Filter Matlab 5 Program for High Pass Filter Matlab 6 Program for Boundary Extraction Matlab . Program For Mean Filter B.

7 Program for Grahm’s Scan Line Algorithm Matlab 8 Program For Edge Detection Matlab 9 Program for Line Detection Matlab .

[320 540 posi(3:4)]) pause c = 255/log10(1+255). clear all. load a gray scale image [xi. close all echo on % 1.2).title('mid-range stretched') posi=get(3.s. % x1 range to 0 and 1 y=(0.title('log-transformed') posi=get(4.bmp').s.*[0. figure(3).2 <= r & r <= 0.5*x1).'position'. .5*(x1-0.imshow(xmid.ind]=imread('images\p64.indy]=cmunique(y).'position').'position'.5*(r(find(r>0.2)) 0. % a 1 x 4 vector gives lower left corner and size of image set(2. % pixel value in [0 255] figure(1).title('original image') posi=get(1. [xmid.2)). % c is chosen to ensure only 256 gray levels after transform r=[0:255].5*(x1-1)).*[x1 > 0.nv]=size(xi).title('mid-range strech') set(7. figure(2).'-').1+1.indy).plot(r. imshow(x).2]+(0.'-').imshow(xlog)..7))-0. +(1+0.'position').ind).01:1].title('image negatives') posi=get(2.[0 540 posi(3:4)]) % Image negatives: T(r) = L-1-r L=256 xneg=255-x.'position'.subplot(132).Program 1.. [nh.plot(r..2 <= x1 & x1 <= 0. % a 1 x 4 vector gives lower left corner and size of image set(3. s=c*log10(1+abs(r)).imshow(xneg).'position'. % a 1 x 4 vector gives lower left corner and size of image set(4.5*(r(find(0.'position'). figure(4).5*r(find(r<0. % a 1 x 4 vector gives lower left corner and size of image set(1. % size of the image x=ind2gray(xi.7].7] . Implement the program in Matlab for digital Negative.[160 540 posi(3:4)]) pause % % Let us now consider some transformation function % r=[0:0. 1+0.[480 540 posi(3:4)]). s=[0.subplot(131). figure(7). figure(7).7))-1)].[275 265 712 194]) % x1=double(x)/255.1+1..title('logarithm compression') axis([0 256 0 256]) xlog=uint8(round(c*log10(1+double(x)))).'position').'position'.*[x1<0.

^2))/length(n). title('with noise') k=medfilt2(n). . title('wiener'). imshow(h1).'salt & pepper'.2. rms4=sqrt(sum(sum(i-h1). title('avg').%applying mean filter subplot(3. imshow(i) title('org image').4). rms1=sqrt(sum(sum(n-i). subplot(3.5).%adding salt & pepper noise subplot(3. title('mean filter') h = fspecial('average'. imshow(k).2.%applying 2D filter over the noise image 'n' with the averager 'h' and then converting double to uint8 subplot(3. close all.5).jpg').2.0. a=imread('car.n)).%applying root mean square PSNR1noise=20 * log10(255/rms1). PSNR2mean=20 * log10(255/rms2).^2))/length(n).05).2. imshow(n). i=rgb2gray(a).2.2). imshow(m).%finding peak signal to noise ratio rms2=sqrt(sum(sum(i-k). m=wiener2(n). PSNR3weiner=20 * log10(255/rms3).^2))/length(n). n=imnoise(i.1). PSNRavg=20 * log10(255/rms4).Program 2a. rms3=sqrt(sum(sum(i-m).^2))/length(n).3).%applying wiener filter subplot(3. Mean Filter with 3 x 3 mask clear all.%averager of 5x5 pixel dimension h1= uint8(filter2(h.

bottombound=ycoord+1.Program 2b. index=1. if topbound<1 topbound=1. end if yindex>bottombound break end end sortedarr = SelectionSort(arr).Height). Median Filter with 3 x 3 mask function [ img2 ] = MedianFilter( img ) [Width.1)=sortedarr(ceil((arrsize)/2)). end if bottombound>Height bottombound=Height. yindex=bottombound. xindex=xindex+1. end arrsize = (bottombound-topbound)*(rightbound-leftbound). while index<arrsize arr(index)=img2(xindex. end for ycoord=1:1:Height topbound=ycoord-1. rightbound=xcoord+1. if leftbound<1 leftbound=1. arr = zeros(arrsize). img2 = zeros(Width.yindex). xindex=leftbound.Height]=size(img). for xcoord=1:1:Width leftbound=xcoord-1. end if rightbound>Width rightbound=Width. end end end function [ numbers ] = SelectionSort( numbers ) . img2(xcoord.ycoord. if xindex >rightbound xindex=1. yindex=yindex+1.

numbers(max) = numbers(i). for j = i-1:-1:1 if numbers(max) < numbers(j) max = j. for i = length: -1: 1 max = i.length=size(numbers). end end . numbers(i) = temp. end end temp = numbers(max).

end end ypdf=hist(reshape(y0. point-wise gray-level transform according to T(r) if nargin<3. y0=y0+[x0==i-1]*tr(i). compute the PDF. original') axis([0 256 0 500]) subplot(212).[0:L-1]).Program 3.title('after') figure(3) stairs([0:L-1].colormap('gray') % subplot(122).axis([0 256 0 500]) % figure(2). L=256.stem([0:L-1].n]=size(x0). if xpdf(i)>0.title('transformation').len. for i=1:L.title('before') subplot(122). range from 0 to L-1 y0=zeros(m. end % default L=256 .stem([0:L-1].ypdf).1).1).n). % cdf. clear all. equalized').image(x0).txt. end % default not plotting figures if nargin<2. [m. 1 x L tr=round(xpdf*triu(ones(L))*(L-1)/len). close all load p64int. 1 x L figure(1). L=256.xpdf).[0:L-1]). % pdf of y.len.imshow(uint8(x0)). and then CDF (T(r))of x0 % 2. Also equalize and plot the Flat histogram. mode=0.axis([0 256 0 256]) % Algorithm: % 1.tr). % pdf. x=reshape(x0.image(y0). % 64 x 64 image.imshow(uint8(y0)).subplot(211).subplot(121). Plot the regular histogram of M x N Image of 3 bits in Matlab.title('histogram.subplot(121).title('histogram.colormap('gray') figure(2). len=m*n. xpdf=hist(x. L = 256 x0=p64int.

subplot(121).title('transformation'). % pdf of y.stem([0:L-1].bins=[0:L-1]. x=reshape(x0.ypdf). 1 x L tr=round(xpdf*triu(ones(L))*(L-1)/len).title('histogram.title('after'). [m. axis([0 256 0 500]) figure(2).n]=size(x0). len=m*n.colormap('gray') subplot(122). if xpdf(i)>0.title('histogram. equalized').stem([0:L-1].xpdf).len. figure(1). y0=y0+[x0==i-1]*tr(i). stairs([0:L-1].bins). end end ypdf=hist(reshape(y0.imagesc(uint8(x0)). original') axis([0 256 0 500]) subplot(212).1).colormap('gray') end .1).title('before').tr). for i=1:L. range from 0 to L-1 y0=zeros(m. xpdf=hist(x. subplot(211).[0:L-1]). % pdf. 1 x L if mode==1. % cdf. axis([0 256 0 256]) figure(3).len.imagesc(uint8(y0)).n).

for i = 1:O G(:.N. D=sqrt(U. idx=find(u>M/2).figure.P) f=imread(X). end g=uint8(real(ifft2(double(G)))).i) = H. imshow(f).*F(:.^2+V.^2).:. end .imshow(g). v(idy)=v(idy)-N. [V. u=0:(M-1). %Question No:5 %IDEAL LOW-PASS FILTER function idealfilter(X.:.O]=size(f). G=F.u). Write a program to implement the low pass filter in Matlab.i). v=0:(N-1).Program 4. u(idx)=u(idx)-M.U]=meshgrid(v. [M. F=fft2(double(f)). idy=find(v>N/2). H=double(D<=P).

fftx(bounder(2):1:M) = 0. end g=real(ifft(fftx)).fftref) < (max(fftref) . binsize.* 2 . P) M=length(X). xidx=1:1:M. % do the filter fftx = fft(X) if ( P ) fftx(bounder(1):1:bounder(2))=0 else fftx(1:1:bounder(1)) = 0. end . Write a program to implement the high pass filter in Matlab. bounder = find( (max(fftref) .Program 5. % get the fft position of cutoff frequence (reference % frequency) f = f / binsize./f)))./ 1000 )). function [g] = FFTPF1D(X. f. fftref = abs(fft(sin(xidx .* pi .

m % (C) 2006 by Yu Hen Hu % created: Dec. uint8 A1=1-double(A0). imshow(Ab).B). title('extracted boundary'). A2=imopen(imclose(A1. imshow(A0). % boundary demo.:)=1.[100 100]).B). imshow(Ac) . % a heart shape hand drawing title('original image'). pause Ab=A-double(erode(A. Ac(vidx. Write a program to implement the boundary extraction algorithm in Matlab. 19. clear A1 A2 A3. % fill the interior A=double(A2) + double(A3). pause % A0 contains mostly 1s and the drawing contains 0s.bmp').Program 6. vidx=[[1:20:280] 280]. Ac(:.B)). % fill 1 pixel hole and remove sticks A3=imfill(A2.title('after interior filling using imfill'). close all A0=imread('myshap4.vidx)=1. Ac=Ab. 2006 % demonstrate morphological boundary extraction % clear all. % invert black and white B=ones(3). imshow(A).

while i < N { Let PT1 = the top point on W Let PT2 = the second top point on W if (P[i] is strictly left of the line PT2 to PT1) { Push P[i] onto W i++ // increment i } else Pop the top point PT1 off the stack } Output: W = the convex hull of S. Input: a set of points S = {P = (P. discard the closer points. For ties.y)} Select the rightmost lowest point P0 in S. Push P[0]=P0 and P[1] onto a stack W. Sort S angularly about P0 as a center.P.x.Program 7. Write a program to implement the grahm’s scan line algorithm in Matlab. . Let P[N] be the sorted array of points.

i)-x(b..char(filename))). 'Choose image(s) to be processed'. { '*.fig'. '*. d1=abs(x(j. pathname. for j=2:r-1 for i=2:c-1 p=i-1. e2=x(j. clear all.i)-x(a.i)-x(j. .q).'JPEG (*..i).jpg'.i)-x(j. e1=x(j.jpg)'. [r.. break. .i)-x(b. y= imread(horzcat(pathname... % convert color image to gray scale x= rgb2gray(y).. a=j-1. f1=x(j. 'All Files (*. .p). q=i+1..fig)'.*)'}.q). 'off').c]=size(x). . if filterindex==0. filterindex] = uigetfile( . min=0. '*. f2=x(j.i)-x(a.*'. imshow(x).'Windows Bitmap (*.. close all. % y=imread('car3..jpg').. .. max=0. b=j+1.'Figures (*.Program 8.bmp'. mm=input('Input your threshold between 0 and 255 '). .bmp)'.p)).end filename=cellstr(filename).. 'MultiSelect'. Write a program for edge detection algorithm in Matlab % program for edge detection clc.i). d2=x(j. %read an image [filename. '*.

elseif (f1 >= mm) && (f2 >= mm) x(j.q).i)-x(b.i)-x(a.p). BW = im2bw(x.i)=255.g1=x(j.i)=255. elseif (g1 >= mm) && (g2 >= mm) x(j. end if (f1 < 0) f1=f1*(-1). end if (d2 < 0) d2=d2*(-1). end if (g2 < 0) g2=g2*(-1). end if (d1 >= mm) && (d2 >= mm) x(j. end if (f2 < 0) f2=f2*(-1). elseif (e1 >= mm) && (e2 >= mm) x(j. end if (g1 < 0) g1=g1*(-1). else x(j.i)=0.5).i)=255.i)=255. 0. g2=x(j. if (d1 < 0) d1=d1*(-1). figure imshow(BW) . end end end figure imshow(x). end if (e1 < 0) e1=e1*(-1). end if (e2 < 0) e2=e2*(-1).

varargin) %Draw line segments. USA % pengtao@glue.. function DrawLines_2Ends(lineseg.. for k = 1 : size(lineseg... parameterized as (x1. Write a Program for line detection algorithm in Matlab..5.3:4)+0. 2005 hold on. y1. Will be transferred % to function 'plot' without modification for line drawing.)' can be put here directly. y2) of line segments to draw. % The image origin defined in function '[. x2. Maryland 20742. on graph % % DrawLines_2Ends(lineseg. if nargin > 1. % Is a Ns-by-4 matrix with each row contains the parameters % (x1. comments and suggestions to % pengtao@glue. 0. y1.5.. 'LineWidth'. Made as an % auxiliary tool for function '[.umd.)'. % Author: Tao Peng % Department of Mechanical Engineering % University of Maryland.umd.5. 1). lineseg(k. varargin{1}). off by (0. y1. end end hold off...] = Hough_Grd(.1:2)+0.edu % Version: alpha Revision: Dec. The output 'lineseg' from the function % '[.. .. 02.Program 9.1:2)+0. % % OUTPUT: None % % BUG REPORT: % Please send your bug reports. plot(lineseg(k.] = Hough_Grd(.3:4)+0. Thanks. lineseg(k.] = Hough_Grd(. x2. properties) % lineseg: Parameters (x1.5.. College Park. else plot(lineseg(k.5). % % INPUT: (lineseg. % properties: (Optional) % A string of line drawing properties.edu . y2) that define the two ends of a line % segment. 2).5. varargin) % A simple function for drawing line segments on graph.)' is % different from what is defined in Matlab. x2.. y2).