computer graphics

40
DIGITAL IMAGE PROCESSING LAB FILE COMPUTER SCIENCE DEPARTMENT SABMITTED TO-MR.S.K.SINGH [AP] SIET 2011 SUBMITTED BY-GAGAN YADAV [email protected] 0715310027 FIRST GROUP

description

working of display devices, various algorithm for clipping an area

Transcript of computer graphics

Page 1: computer graphics

S.I.E.T. Page 1

DIGITAL IMAGE PROCESSING LAB FILE COMPUTER SCIENCE DEPARTMENT

SABMITTED TO-MR.S.K.SINGH [AP]

SIET

2011

1

SUBMITTED BY-GAGAN YADAV

[email protected]

0715310027 FIRST GROUP

Page 2: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 2

S.NO NAME OF PRACTICAL DATE PAGE SIGN.

P-01 Spatial Image Enhancement functions on a bitmap - Rotation (Clockwise).

P-02 Spatial Image Enhancement functions on a bitmap image Enlargement (Double Size).

P-03 Laplacian Transform on image

P-04 Low Pass filter on the image

P-05 Edge detection on an image

P-06 M-file function(enhacement[varargin]) to provide all basic gray-level transformations,such as,

a. Negative b. Log Law transformation c. Power Law transformation d. Contrast stretching e. Thresholding

P-07 Plot the histrograms of: a) Dark image b) Bright image c) Low contrast image d) High contrast image

For each image show the image & its histogram in two different sub regions of the single figure window.

P-08 M-file function(histogram[r]) to provide histrogram equalization for image- enhancement.

Page 3: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 3

P-09 Spatial domain filters & use convolution function conv2 () to filter the image.

P-10 Using laplacian filters implement

high-boost filtering.

Page 4: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 4

Program No-1 Aim:- Implement the spatial image enhancement functions on a bitmap image –Rotation(clockwise). Program- I = fitstread(‘solarspectra.fts’); I = mat2gray(I); J = importe(I,-10,’bilinear’,’crop’); Imshow(I) Figure,imshow(j) Result – File Name-rotate.m

Page 5: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 5

Original Image

Page 6: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 6

Rotated Image

Page 7: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 7

Program No-2 Aim:- Implement the spatial image enhancement functions on a bitmap image-Enlargement (Double Size). Porogram- I=imread(‘circuit.tif’); J=imresize(I,2.0); Imshow(I) Figure,imshow(J) Result- Screen shot On Next Page

Page 8: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 8

Original Image

Page 9: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 9

Output- Double Size Image

Page 10: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 10

Program No-3 Aim- To apply Laplacian Transform on image Program- clc; cloge all; f=imread(‘moon.tif’); w4=fspecial(‘laplacian’,0); w8=[1 1 1;1 -8 1;1 1 1]; f=im2double(f); g4=f-imfilter (f,w4,’replicate’); g8=f-imfilter (f,w4,’replicate’); imview(f) figure,imview(g4) figure,imview(g8) Result- Screen shot On Next Page

Page 11: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 11

Original Image

Page 12: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 12

Program No-4 AIM- To Apply Low Pass filter on the image- Program- clear; close all; Input_image=imread(‘lena.bmp’); Input_img = imread(‘lena.bmp’); %type = input(‘please choose the filter type: 1.Average filter; 2.Sobel filter(horizontal)); 3.Sobel filter(‘vertical.’); % Add Gaussian noise to image,please check to see the usage of imnoise noisyImg = imnoise(input_img,’gaussian’); % create 3x3 average filter filter = ones(3,3)/9; % apply the average filter to noisy image filteredImg = filter2(filter,noisyImg); % Display result Figure; colormap(gray); subplot(3,1,1); imshow(input_img); title(‘Original’); subplot(3,1,2); imshow(input_img); title(‘Original’); subplot(3,1,2); imshow(noisyImage); title(‘noisyImage’); subplot(3,1,3); imshow(filterImage,[]); title(‘Filter Image’); result – Screen Shots on left side

Page 13: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 13

Page 14: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 14

Program no -05 Aim- Program to Imliment Edge Detection on an image. Program- clear; close all; input_img=imread(‘lena.bmp’); % Soble filter horizontal Hfilter=[-1 0 1;-2 0 2;-1 0 1]; % SObel filter vertical Vfilter=[-1-2-1;0 0 0;1 2 1]; % image filtering filteredimg1=filter2(Hfilter,input_img); filteredimg2=filter2(Vfilter,input_img); % Compare results figure; colormap(gray); subplot(3,1,1); imshow(input_img); title(‘Original’); subplot(3,1,2); imshow(filteredImg2,[]); title(Vertical Gradient’); %edge detection threshold=80; % use horizontal gradient to detect edge % learn how to use to find to get the index of edge points index=find(abs(filteredImg1(:))>threshold); Edge1=255*ones(1,prod(size(input_img))); Edge1(index)=0; edge1=reshape(edge1,size(input_img)); % use vertical gradient to detect edge index2=find(abs(filteredImg2(:))>threshold); Edge2=255*ones(1,prod(size(input_img))); Edge2(index2)=0; Edge2=reshape(edge2,size(input_img)):

Page 15: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 15

Figure; Subplot(2,1,1); Imshow(edge1/255); title(‘edge detected via horizontal gradient’); subplot(2,1,2); imshow(edge2/255); title(‘edge detected via vertical gradient’); RESULT- Screen Shots on left

Page 16: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 16

Page 17: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 17

Program No-6 Aim: Write a M-File function to provide all the basic gray-level transformations. Code: [A] - Negative function [y]=negative(varargin) a=varargin{1}; [m n]=size(a); for i=1:1:m for j=1:1:n b=a(i,j); c=255-b; a(i,j)=c; end end mat2gray(a) y=a Output: >>a=imread('cam.bmp') >>imshow(a) >>b=negative(a) >>imshow(b)

Page 18: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 18

Original Image

Output Image

Page 19: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 19

[B] - Log Transformation function [y]=logtran(varargin) aa=varargin{1}; [m n]=size(aa); z=varargin{2}; a=im2double(aa); for i=1:1:m for j=1:1:n b=a(i,j); c=z*log(1+b); a(i,j)=c; end end y=a Output: >>a=imread('cam.bmp') >>imshow(a) >>b=logtran(a,2) >>imshow(b) >>b=logtran(a,5) >>imshow(b)

Page 20: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 20

Original Image

Page 21: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 21

[C]- Power Law Transformation Code: function [y]=powerlaw(varargin) aa=varargin{1}; [m n]=size(aa); z=varargin{2}; a=im2double(aa); aa=a.^z mat2gray(aa) y=aa Output: >>a=imread('cam.bmp') >>imshow(a) >>b=powerlaw(a,3) >>imshow(b)

Page 22: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 22

Original Image

Output Image

Page 23: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 23

[D] - Contrast Streching Code: function [y]=contraststretch1(varargin) aa=varargin{1}; [m n]=size(aa); a=im2double(aa); sum=0 for i=1:1:m for j=1:1:n sum=sum+a(i,j); end end sum pro=m*n; level=sum/pro;(varargin) S = 1./(1+(level./a).^2) mat2gray(S) y=S Output: >>a=imread('cam.bmp') >>imshow(a) >>b=contraststretch(a) >>imshow(b)

Page 24: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 24

Original Image

Output Image

Page 25: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 25

[E]- Thresholding Code: function [y]=threshold(varargin) aa=varargin{1}; [m n]=size(aa); a=im2double(aa); sum=0 for i=1:1:m for j=1:1:n sum=sum+a(i,j); end end sum pro=m*n; level=sum/pro; for i=1:1:m for j=1:1:n if a(i,j)<level a(i,j)=0; else a(i,j)=1; end end end mat2gray(a) y=a Output: >>a=imread('cam.bmp') >>imshow(a) >>b=threshold(a) >>imshow(b)

Page 26: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 26

Original Image

Output Image

Page 27: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 27

Program No-07 Aim: Write a M-File function to plot the histogram of the given image. Code: Plot Histogram function histogram(varargin) u=varargin{1}; [m n]=size(u); h=zeros(1,255); for j=1:1:m for k=1:1:n if u(j,k)==0 u(j,k)=1; end end end for j=1:1:m for k=1:1:n a=u(j,k); h(a)=h(a)+1; end end figure(1) imshow(u) figure(2) bar(h) Output: >>a=imread('cam.bmp') >>histogram(a)

Page 28: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 28

Page 29: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 29

Program No-08 Equalize the histogram for Image Enhancement Code— function histogramequalize(varargin) u=varargin{1}; [m n]=size(u); h=zeros(1,255); z=zeros(1,255); maxim=max((max(u))); c=m*n; for j=1:1:m for k=1:1:n if u(j,k)==0 u(j,k)=1; end end end for j=1:1:m for k=1:1:n a=u(j,k); h(a)=h(a)+1; end end pdf=h/c; cdf(1)=pdf(1); for i=2:1:maxim cdf(i)=pdf(i)+cdf(i-1) end for i=1:1:maxim new(i)=cdf(i)*maxim; round(new(i)); end for i=1:1:maxim if new(i)==0 new(i)=new(i)+1; end end for j=1:1:m for k=1:1:n

Page 30: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 30

temp=u(j,k); b(j,k)=new(temp); a=b(j,k); z(a)=z(a)+1; end end b=b-1; figure(1) imshow(u) figure(2) bar(h) figure(3) imshow(b) figure(4) bar(z) OUTPUT: >>a=imread('cam.bmp') >>histogramequalize(a)

Page 31: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 31

Page 32: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 32

Page 33: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 33

Aim: To remove Gaussian Noise from the given image. Code: function lpfgauss(varargin) aa=varargin{1} f=double(aa) ab=imnoise(aa,'gaussian'); a=double(ab) w=[1 1 1;1 1 1;1 1 1]/9 [m n]=size(a) for i=2:1:m-1 for j=2:1:n-1 a1(i,j)=w(1)*a(i-1,j-1)+w(2)*a(i-1,j)+w(3)*a(i-1,j+1)+w(4)*a(i,j-1)+w(5)*a(i,j)+w(6)*a(i,j+1)+w(7)*a(i+1,j-1)+w(8)*a(i+1,j)+w(9)*a(i+1,j+1); end end figure(1) imshow(uint8(a)) figure(2) imshow(uint8(a1)) Output: >>a=imread('cam.bmp') >>lpfgauss(a)

Page 34: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 34

Page 35: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 35

Aim: To provide local enhancement using histogram processing. Code: function localhistoprocess(varargin) im=varargin{1}; %im=im2double(im); [row col]=size(im); max_im=max(max(im)); h=zeros(1,max_im+1); %first 120*120 block for n=1:1:120 for m=1:1:120 a(n,m)=im(n,m); end end %histogram of first block a=a+1; for n=1:1:120 for m=1:1:120 t=a(n,m); h(t)=h(t)+1; end end figure(1) bar(h) [X,Y]=ginput(1); for n=1:1:120 for m=1:1:120 if a(n,m)<X%threshold a(n,m)=0; else a(n,m)=255; end end end imshow(uint8(a)); % second block for n=1:1:120 for m=121:1:240 b(n,m-120)=im(n,m); end

Page 36: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 36

end %histogram of second block b=b+1; for n=1:1:120 for m=1:1:120 t=b(n,m); h(t)=h(t)+1; end end figure(2) bar(h) [X,Y]=ginput(1); for n=1:1:120 for m=1:1:120 if b(n,m)<X%threshold b(n,m)=0; else b(n,m)=255; end end end imshow(uint8(b)); % third block for n=121:1:240 for m=1:1:120 c(n-120,m)=im(n,m); end end %histogram of third block c=c+1; for n=1:1:120 for m=1:1:120 t=c(n,m); h(t)=h(t)+1; end end figure(3) bar(h) [X,Y]=ginput(1); for n=1:1:120 for m=1:1:120 if c(n,m)<X%threshold

Page 37: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 37

c(n,m)=0; else c(n,m)=255; end end end imshow(uint8(c)); % final block for n=121:1:row for m=121:1:col d(n-120,m-120)=im(n,m); end end %histogram of final block d=d+1; for n=1:1:120 for m=1:1:120 t=d(n,m); h(t)=h(t)+1; end end figure(4) bar(h) [X,Y]=ginput(1); for n=1:1:120 for m=1:1:120 if d(n,m)<X%threshold d(n,m)=0; else d(n,m)=255; end end end imshow(uint8(d)); s=[a b;c d] figure(5); imshow(uint8(s)); OUTPUT: >>a=imread('cam.bmp') >>imshow(a) >>localhistoprocess(a)

Page 38: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 38

Page 39: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 39

Page 40: computer graphics

DIGITAL IMAGE PROCESSING LAB FILE

SIET Page 40