17
1.WAP to display the Negative of a digital Image clear close all clc a= imread('lena.jpg'); b= im2double(a); c= 256*(1-b); d= uint8(c); subplot(2,1,1); imshow(a); title('original image'); subplot(2,1,2); imshow(d); title('negative image');

DIP Lab File

Embed Size (px)

Citation preview

Page 1: DIP Lab File

1.WAP to display the Negative of a digital Image

clearclose allclca= imread('lena.jpg');b= im2double(a);c= 256*(1-b);d= uint8(c);subplot(2,1,1);imshow(a);title('original image');subplot(2,1,2);imshow(d);title('negative image');

Page 2: DIP Lab File

2.WAP to perform Thresholding on an input Image

clear allclcclose allp= imread('pollen.tif');a=p;[row col]= size(a);t= input('enter value of threshold:')for i= 1:1:row for j= 1:1:col if(p(i,j)< t) a(i,j)= 0; else a(i,j)= 255; end endendfigure(1),imshow(p), title('original')figure(2),imshow(a),title('threshold')

Page 3: DIP Lab File

3 WAP to perform gray level slicing without backgroundclear allclcp= imread('kidney.tif');z= double(p);[row,col] = size(z)for i= 1:1:row for j= 1:1:col if((z(i,j)>150))&&(z(i,j)<250) z(i,j)= 255; else z(i,j)=0; end endendfigure(1);imshow(p),title('original image')figure(2);imshow(uint8(z)),title('without background')

Page 4: DIP Lab File

4. WAP to perform gray level slicing with backgroundclear allclcp= imread('kidney.tif');z= double(p);[row,col] = size(z)for i= 1:1:row for j= 1:1:col if((z(i,j)>150))&&(z(i,j)<250) z(i,j)= 255; else z(i,j)=p(i,j); end endendfigure(1);imshow(p),title('original image')figure(2);imshow(uint8(z)),title('with background'

Page 5: DIP Lab File

5. WAP to perform bit-plane slicingclear allclcclose alla = imread('pout.tif');a= double(a);r = input('which bit image do u want to see. 1 = MSB 8 = LSB');[row,col] = size(a);for x = 1:1:row for y = 1:1:col c= dec2bin(a(x,y),8); d= c(r); %w=double(d); w(x,y)= double(d); if w(x,y)==49 %if double(d)==49 w(x,y)=255; else w(x,y)=0; end endend%figure(1)subplot(2,1,1)imshow(uint8(a)),title('original image')%figure(2)subplot(2,1,2)imshow(uint8(w)),title('bit-plane image')

Page 6: DIP Lab File

1.

2.

Page 7: DIP Lab File

6. WAP to display Histogram of an imageclear allclca= imread('lena.jpg');a= double(a);[r,c]=size(a);h= zeros(1,300);

for n= 1:1:r for m= 1:1:c t= a(n,m); h(t) = h(t) + 1; endend%figure(1)subplot(2,1,1)imshow(uint8(a)),title('original Image')%figure(2)subplot(2,1,2)bar(h)

Page 8: DIP Lab File

7.Prog for Ideal low pass filterclear allclcclose alla= imread('cameraman.tif');a= double(a);c= size(a);N= c(1)D0= input('Enter the cut-off frequency');for u=1:1:c(1) for v=1:1:c(2) D= (((u-N/2))^2+(v-(N/2))^2)^0.5; if D<D0 H(u,v)=1; else H(u,v)=0; end endendvv=fft2(a);vc=fftshift(vv);x=vc.*H;X=abs(ifft2(x));figure(1), imshow(uint8(a)),title('original')figure(2),mesh(H),title('ILPF Frequency Response')figure(3),imshow(uint8(X)),title('Filtered Image')figure(4),imagesc(H), colormap(gray),title('2-D Ideal Lowpass Filter')

Page 9: DIP Lab File

Ideal low pass filter

Do=180

Page 10: DIP Lab File

8.Prog for Butterworth low passclear allclcclose alla= imread('cameraman.tif');a= double(a);c= size(a);N= c(1);n=input('Enter the order of filter');D0=input('Enter the cut-off frequency');for u= 1:1:c(1) for v= 1:1:c(2) D= (((u-(N/2))^2+(v-(N/2))^2))^0.5; H(u,v)=1/(1+((D/D0)^(2*n))); endendvv= fft2(a);vc=fftshift(vv);x= vc.*H;X= abs(ifft2(x));figure(1),imshow(uint8(a)),title('original Image')figure(2),mesh(H),title('Butterworth Frequency Response')figure(3),imshow(uint8(X)),title('Filtered image')figure(4),imagesc(H),colormap(gray),TITLE('2D Butterworth Filter')

Page 11: DIP Lab File

Butterworth low pass Filter

Order=2 Freq=15

Page 12: DIP Lab File

9.Prog for Gaussian low passclear allclcclose alla= imread('cameraman.tif');a= double(a);c= size(a);N= c(1);%n=input('Enter the order of filter');D0=input('Enter the cut-off frequency OR STANDARD DEVIATION');for u= 1:1:c(1) for v= 1:1:c(2) Dx= (((u-(N/2))^2+(v-(N/2))^2))^0.5; D= Dx*Dx H(u,v)=exp(-D/(2*D0*D0)); endendvv= fft2(a);vc=fftshift(vv);x= vc.*H;X= abs(ifft2(x));figure(1),imshow(uint8(a)),title('original Image')figure(2),mesh(H),title('GAUSSIAN Frequency Response')figure(3),imshow(uint8(X)),title('Filtered image')figure(4),imagesc(H),colormap(gray),TITLE('2D GAUSSIAN Filter')

Page 13: DIP Lab File

Gaussian Low pass filter

Page 14: DIP Lab File

10. Program for generating noise PDFs for Uniform, Rayleigh and Exponential Noiseclear allclcclose alla=0;b=1;f = imread('testimage.tif');f1=double(f);[r,c]= size(f);I= input('what type of noise? uniform=1,Rayleigh=2,Exp=3')if I==1 R= a+(b-a)*rand(r,c);elseif I==2 R= a+(-b*log(1-rand(r,c))).^0.5;elseif I==3 R=-log(1-rand(r,c));endmmax = max(max(R));mmin= min(min(R));const = 100/(mmax-mmin);for x= 1:1:r for y= 1:1:c noise(x,y) = const*(R(x,y)-mmin); endendnoisy_image = f1+noise;figure(1)imshow(f),title('original Image')figure(2)hist(noise),title('noise PDF')figure(3)imshow(uint8(noisy_image)),title('Noisy Image')