Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

PART-A

1. Write a program to load an image and


display the following:
a. Size of the following image
b. Average Intensity value
c. Maximum and minimum Intensity
value

>> Im=imread('tulips.jpg');
>> imshow(Im);
>> s=size(Im);
>> disp(s);
112 288 3

>> avg=mean(Im(:));
>> minIntensity=min(Im(:));
>> maxIntensity=max(Im(:));
>>
>> disp('Average Intensity of the image is:');
Average Intensity of the image is:
>> disp(avg);
123.8232

>> disp('Minimum Intensity of the image is:');


Minimum Intensity of the image is:
>> disp(minIntensity);
0

>> disp('Maximum Intensity of the image is:');


Maximum Intensity of the image is:
>> disp(maxIntensity);
255
OUTPUT

2. Write a program to load a color image and


display the following
a.RGB image
b.RGB channels separately

>> I=imread('butterfly.jpg');
>> imshow(I);
>>
>> r=I(:,:,1);
>> figure;
>>
image(r),colormap([[0:1/255:1]',zeros(256,1),zeros(256,
1)]),colorbar;
>>
>> g=I(:,:,2);
>> figure;
>>
image(g),colormap([zeros(256,1),[0:1/255:1]',zeros(256,
1)]),colorbar;
>>
>> b=I(:,:,3);
>> figure;
>>
image(b),colormap([zeros(256,1),zeros(256,1),[0:1/255:
1]']),colorbar;
OUTPUT
3. Write a program to demonstrate the
following distance measure A) Euclidean
distance B) City block distance C) Chess
board distance

>> x1=input('Enter x1 value:');


Enter x1 value:2
>> x2=input('Enter x2 value:');
enter x2 value:4
>> y1=input('Enter y1 value:');
Enter y1 value:2
>> y2=input('Enter y2 value:');
Enter y2 value:4
>>
>> ED=sqrt((x2-x1)^2+(y2-y1)^2);
>> CBD=abs(x2-x1)+abs(y2-y1);
>> chess=max(abs(x2-x1),abs(y2-y1));
>>
>> fprintf('Given points are (%d,%d) and
(%d,%d)\n',x1,y1,x2,y2);
Given points are (2,2) and (4,4)
>> fprintf('\nEuclidean distance between the given
points is %4f\n',ED);
Euclidean distance between the given points is
2.828427
>> fprintf('\nCity block distance between the given
points is %4f\n',CBD);
City block distance between the given points is
4.000000
>> fprintf('Chess board distance between the given
points is %4f\n',chess);
Chess board distance between the given points is
2.000000

4. Write a program to demonstrate using the


following arithmetic operation on an image
A) Addition B) Subtraction C)
Multiplication D) Division

>> i1=imread('img8.jpg');
>> i2=imread('img10.jpg');
>> imshow(i1);
>> imshow(i2);
>> Aimg=imadd(i1,i2);
>> imshow(Aimg,[]);
>> Simg=imsubtract(i1,i2);
>> imshow(Simg,[]);
>> Mimg=immultiply(i1,i2);
>> imshow(Mimg,[]);
>> Dimg=imdivide(i1,i2);
>> imshow(Dimg,[]);

OUTPUT
5. Write a program to obtain a histogram of
gray scale image

>> i=imread('dog.jpg');
>> g=rgb2gray(i);
>> subplot(1,2,1);
>> imshow(g);
>>
>> subplot(1,2,2);
>> imhist(g);
OUTPUT

6. Write a program to obtain a histogram and


equization on an image.

>> I=imread('cat.jpg');
>> I=rgb2gray(I);
>> imshow(I);
>> figure;
>> subplot(1,2,1);
>> imshow(I);
>> subplot(1,2,2);
>> imhist(I,64);
>>
>> J=histeq(I);
>> figure;
>> subplot(1,2,1);
>> imshow(j);
>> subplot(1,2,2);
>> J=histeq(I);
>> figure;
>> subplot(1,2,1);
>> imshow(J);
>> subplot(1,2,2);
>> imhist(J,64);

OUTPUT
7. Write a program to demonstrate the use of
logic operation A) AND B) OR C) NOT

>> a=imread('rose.jpg');
>> a=imresize(a,[512,512]);
>> a=rgb2gray(a);
>> a=im2bw(a);
>> subplot(2,3,1);
>> imshow(a);
>> title('Image 1');
>> b=imread('earl.jpg');
>> b=imresize(b,[512,512]);
>> b=rgb2gray(b);
>> b=im2bw(b);
>> subplot(2,3,2);
>> imshow(b);
>> title('Image 2');
>> na=not(a);
>> subplot(2,3,3);
>> imshow(na);
>> title('Not a');
>> nb=not(b);
>> subplot(2,3,4);
>> imshow(nb);
>> title('Not b');
>> aandb=and(na,nb);
>> subplot(2,3,5);
>> imshow(aandb);
>> title('a and b');
>> aorb=or(na,nb);
>> subplot(2,3,6);
>> imshow(aorb);
>> title('a or b');

OR
>> clc;
>> clear all;
>> close all;
>> a=ones(40);b=zeros(40);
>> c=[a b;b a];d=[b b;a a];
>> subplot(3,2,1);imshow(c);title('original image');
>> subplot(3,2,2);imshow(d);title('original image');
>> %NOT (take a compliment of c and display it
>> notimg=~c;
>>
subplot(3,2,3);imshow(notimg);title('Complimented
image C');
>> %AND operation(take AND of image c and image b
and display it
>> andimg=c&d;
>> subplot(3,2,4);imshow(andimg);title('Image C AND
image D');
>> %OR operation(Take OR of image c and image b
and display it)
>> subplot(3,2,5);
>> orimage=c|d;
>> imshow(orimage);title('Image C OR image D');
>>

OUTPUT

Or
PART-B
1.Demonstrate the following filters and
perform smoothening on an image A)
Minimum filter B) Maximum filter

>> I=imread('Nature.jpg');
>> BW=im2bw(I,0.6);
>> minf=@(x)min(x(:));
>> maxf=@(x)max(x(:));
>> min_img=nlfilter(BW,[3,3],minf);
>> max_img=nlfilter(BW,[3,3],maxf);
>> subplot(2,2,1),imshow(BW),title('Original Image');
>> subplot(2,2,2),imshow(min_img),title('Minimum
Image');
>> subplot(2,2,3),imshow(max_img),title('Maximum
Image');

OUTPUT

2.Demonstrate the following filter and perform


smoothening on an image A) Mean filter B)
Median filter

>> I=imread('nature.jpg');
>> f=fspecial('average',3);
>> bimg=imfilter(I,f);
>> nimg=imnoise(I,'salt & pepper',0.2);
>> med=fspecial('gaussian',7);
>> fimg=imfilter(nimg,med);
>> subplot(2,2,1),imshow(I),title('Original Image');
>> subplot(2,2,2),imshow(bimg),title('Blurred Image');
>> subplot(2,2,3),imshow(nimg),title('Noisy Image');
>> subplot(2,2,4),imshow(fimg),title('Filtered Image');

OUTPUT

3.Write a program to perform sharpening on


an image using second order derivative
>>A=imread('nature.jpg');
>> figure;
>> imshow(A);
>> I1=A;
>> I=zeros(size(A));
>> f1=[0 1 0;1 -4 1;0 1 0];
>> f1=[1 1 1;1 -8 1;1 1 1];
>> A=padarray(A,[1,1]);
>> A=double(A);
>> for i=1:size(A,1)-2
for j=1:size(A,2)-2
I(i,j)=sum(sum(f1.*A(i:i+2,j:j+2)));
end;
end;
>> I=uint8(I);
>> figure;
>> imshow(I);title('Filtered Image');
>> B=I1-I;
>> figure;
>> imshow(B);title('Sharpened Image');
OUTPUT

4.Write a program to perform sharpening on


an image using first order derivative

>> y=imread('nature.jpg');
>> y=rgb2gray(y);
>> lab=[0 1 0;1 -4 1;0 1 0];
>> result=uint8(filter2(lab,y,'same'));
>> sharp=imsubtract(y,result);
>> subplot(2,2,1);
>> title('Original Image');
>> imshow(y);
>> subplot(2,2,2);
>> imshow(sharp);
>> title('Sharp Image');
>> subplot(2,2,3);
>> imshow(result);
>> title(‘laplician filter’);

OUTPUT
5.Write a program to demonstrate the
following neighbors of following pixels
1) 4 neighbors of pixel
2) 4 diagonal neighbors of a pixel
3) 8 neighbors of a pixel

>> clc;
>> clear all;
>> close all;
>> %to find neighbor of a given pixel
>> a=magic(5);
>> disp('a=');disp(a);
a=
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

>> b=input('Enter the row <size of the matrix:');


Enter the row <size of the matrix:4
>> c=input('Enter the column <size of the matrix:');
Enter the column <size of the matrix:4
>> b=input('Enter the row<size of the matrix:');
Enter the row<size of the matrix:4
>> c=input('Enter the column<size of the matrix:');
Enter the column<size of the matrix:4
>> disp('Element');disp(a(b,c));
Element
21

>> %4point neighbor


>> n4=[a(b+1,c),a(b-1,c),a(b,c+1),a(b,c-1)];
>> disp('N4=');disp(n4);
N4=
2 20 3 19
>> %8 point neighbor
>> n8=[a(b+1,c+1),a(b,c+1),a(b,c-1),a(b+1,c+1),a(b+1,c-
1),a(b-1,c-1),a(b-1,c+1)];
>> disp('N8=');disp(n8);
N8=
9 3 19 9 25 13 22

>> %diagonal neighbor


>> ND=[a(b+1,c+1),a(b+1,c-1),a(b-1,c+1)];
>> disp('ND=');disp(ND);
ND=
9 25 22

6.Write a program to demonstrate color


transformation using tonal correction.
>> I=imread('blue.jpg');
>> %contrast enhancement for flat tone image
>> I2=rgb2hsv(I);%hue saturated value
>> v=I2(:,:,3);
>> v2=imadjust(v);
>> I2(:,:,3)=v2;
>> I3=hsv2rgb(I2);
>> figure;
>> subplot(2,2,1);imshow(I);title('Low contrast color');
>> subplot(2,2,2);imshow(v);title('low contrast intensity
image');
>> subplot(2,2,3);imshow(v2);title('improved contrast
intensity image');
>> subplot(2,2,4);imshow(I3);title('improved contrast
color image color');
>> a=imread('Labdoggo.jpg');
>> id=im2double(a);
>> a1=imread('Koala1.jpg');
>> id1=im2double(a1);
>> %power law transformation
>> o1=2*(id.^0.5);
>> o2=2*(id.^1.5);
>> o3=2*(id.^3.5);
>> i1=2*(id1.^0.5);
>> i2=2*(id1.^1.5);
>> i3=2*(id1.^3.5);
>> figure;
>> subplot(2,2,1);imshow(a);title('Light tone image');
>> subplot(2,2,2);imshow(o1);
>> subplot(2,2,3);imshow(o2);
>> subplot(2,2,4);imshow(o3);
>> figure;
>> subplot(2,2,1);imshow(a1);title('Dark tone image');
>> subplot(2,2,2);imshow(i1);
>> subplot(2,2,3);imshow(i2);
>> subplot(2,2,4);imshow(i3);
OUTPUT
7.Write a program to demonstrate the steps of
Cannys Edge detection algorithm on an
image.

>> i=imread('nature.jpg');
>> g=rgb2gray(i);
>> f=edge(g,'Canny');
>> subplot(1,2,1);imshow(f);title('Detected Edge');
>> subplot(1,2,2);imshow(i);title('Original Image');
>>
OUTPUT

8. Write a program to demonstrate the steps


of Sobel Edge detection algorithm on an
image.

>> i=imread('nature.jpg');
>> g=rgb2gray(i);
>> f=edge(g,'sobel');
>> subplot(1,2,1);imshow(f);title('Detected Edge');
>> subplot(1,2,2);imshow(i);title('Original Image');
>>

OUTPUT

You might also like