Sunteți pe pagina 1din 23

20/08/2019

Experiment 1: Reading and writing images


Basic image operations: (imread, imwrite, imshow).

Code:

A1 = imread('C:\Users\niit\Desktop\barbara.png');
A2 = imread('C:\Users\niit\Desktop\Lenna.png');
imshow (A2)
imfinfo('C:\Users\niit\Desktop\Lenna.png')
imwrite(A2,'C:\Users\niit\Pictures\Screenshots\Lenna.png');
imshowpair(A1, A2, 'montage')
imshowpair(A1, A2, 'blend')
figure, imshow (A1)
figure, imshow (A2)
figure, imshow (A2), title ('image alright')
figure, imshow (A2), text (100 , 100, 'text', 'fontsize', 50);
xlabel('Input')
ylabel('out')

Input: image

Output:
27/08/19
Experiment-2: RGB to Gray Scale Conversion

A = imread('C:\Users\niit\Desktop\this.png');
B = rgb2gray(A);
%imshow(B);

[x,y] = size(B);

C= B;

for i = 1:x
for j = 1:y
C(i,j) = C(i,j) + 100;
%imwrite (C, 'C:\Users\niit\Desktop\this.png');
end
end

imshowpair(B,C,'montage');

figure, imshow(B), title('original')

figure, imshow(C), title('final')


Experiment-3: Converting gray scale to black and white images

A = imread('C:\Users\niit\Desktop\this.png');

B = rgb2gray(A);

[x,y] = size(B);

C= B;

for x = 1:size(C,1)
for y = 1:size(C,2)
if C(x,y)>128
C(x,y) = 255;
else
C(x,y) = 0;
end
end
end
imshowpair(B,C,'montage');

03/09/2019
Experiment-4: To increase the dynamic range of gray levels
1)From RGB to Gray scale image:
f = rgb2gray(imread('C:\Users\NIIT\Desktop\this.png'));

fin = imadjust (f,[0.3 0.7])

imshowpair(f,fin,'montage');

2)From RGB image:

f = imread('C:\Users\NIIT\Desktop\this.png');
fin = imadjust(f,[0.1 0.2 0; 0.3 0.5 1], []);
imshowpair(f,fin,'montage');
z = [233 244 567;
566 789 0;
-100 -900 -34]

A = min(min(z))
B = max(max(z))

z1 = 255*((z - A)/(B - A));

z= imread('C:\Users\NIIT\Desktop\this.png');

A = min(min(z))
B = max(max(z))

z1 = 255*((z - A)/(B - A));

imshowpair(z, z1, 'montage')


% imshow(z1, [0 255]);
10/07/19

Experiment 5: Bit plane slicing

With inbuilt func


A= rgb2gray(imread('C:\Users\niit\Desktop\Lenna.png'))
[row,col]=size(A);
subplot(3,3,1), imshow(A), title('Original Image');
C=zeros(row,col,8);
for k=1:8
for i=1:row
for j=1:col
C(i,j,k)=bitget(A(i,j),k); %Bit slicing
end
end
subplot(3,3,k+1), imshow(C(:,:,k)), title(['Bit Plane ',num2str(k-1)]);
end
2)Without inbuilt function

A= rgb2gray(imread('C:\Users\niit\Desktop\Lenna.png'))

b1=[];
b2=[];
b3=[];
b4=[];
b5=[];
b6=[];
b7=[];
b8=[];

for m=1:256
for n=1:256
t=de2bi(A(m,n),8,'left-msb');
b1(m,n)=t(1,1);
b2(m,n)=t(1,2);
b3(m,n)=t(1,3);
b4(m,n)=t(1,4);
b5(m,n)=t(1,5);
b6(m,n)=t(1,6);
b7(m,n)=t(1,7);
b8(m,n)=t(1,8);
end
end
subplot(3,3,1);
imshow(A);
title('image of cameramen','color','r');
subplot(3,3,2);
imshow(b8);
title('image of bit-1','color','r');
subplot(3,3,3);
imshow(b7);
title('image of bit-2','color','r');
subplot(3,3,4);
imshow(b6);
title('image of bit-3','color','r');
subplot(3,3,5);
imshow(b5);
title('image of bit-4','color','r');
subplot(3,3,6);
imshow(b4);
title('image of bit-5','color','r');
subplot(3,3,7);
imshow(b3);
title('image of bit-6','color','r');
subplot(3,3,8);
imshow(b2);
title('image of bit-7','color','r');
subplot(3,3,9);
imshow(b1);
title('image of bit-8','color','r');

Experiment 6: Intensity transformation

1)Log

I= rgb2gray(imread('C:\Users\niit\Desktop\Lenna.png'))

imshow(I)
I2 = im2double(I);
J4 = 1 * log(1 + I2);
J5 = 2 * log(1 + I2);
J6 = 5 * log(1 + I2);

subplot(1,3,1), imshow(J4)
subplot(1,3,2), imshow(J5)
subplot(1,3,3), imshow(J6)

2)Negative

I= rgb2gray(imread('C:\Users\niit\Desktop\Lenna.png'))

imshow(I)
J=imcomplement(I);
figure,imshow(J)

3)Gamma
I= rgb2gray(imread('C:\Users\niit\Desktop\Lenna.png'))

J1 = imadjust(I,[],[],3);
J2 = imadjust(I,[],[],1);
J3 = imadjust(I,[],[],0.4);

subplot(1,3,1), imshow(J1)
subplot(1,3,2),imshow(J2)
subplot(1,3,3),imshow(J3)

01/10/19

Experiment:7 – Affine Transformation

1)Translation:

A = rgb2gray(imread('C:\Users\NIIT\Desktop\this.png'));

figure, imshow(A)
title('input image')

for x=1: size(A,1)


for y=1: size(A,2)
x2 = x +50;
y2 = y +50;
out(x2, y2) = A(x,y);
end
end

figure, imshow(out);
title('Output image');
2)Shearing:
A = rgb2gray(imread('C:\Users\NIIT\Desktop\this.png'));

figure, imshow(A)
title('input image')

for x=1: size(A,1)


for y=1: size(A,2)
x2 = x + 2*y;
y2 = y;
out(x2, y2) = A(x,y);
end
end

figure, imshow(out);
title('Output image');
01/10/19
Experiment 8: Gamma Correction
I= rgb2gray(imread('C:\Users\niit\Desktop\Lenna.png'))

J1 = imadjust(I,[],[],3);
J2 = imadjust(I,[],[],1);
J3 = imadjust(I,[],[],0.4);

subplot(1,3,1), imshow(J1)
subplot(1,3,2),imshow(J2)
subplot(1,3,3),imshow(J3)

15/09/19

Experiment:9-Histogram

1)a)using imhist

A = rgb2gray(imread('C:\Users\niit\Desktop\Lenna.png'))
subplot(1, 2, 1)
imshow(A)
imhist(A, 64)
b) plot, stem

c) without imhist func


% Read source image file
img = imread('C:\Users\niit\Desktop\Lenna.png');

% Convert image to grayscale image


img=rgb2gray(img);

% get the dimension of the image


[x, y] = size(img);

% Create a frequency array of size 256


frequency = 1 : 256;

count = 0;
% Iterate over grayscale image matrix
% for every possible intensity value
% and count them

for i = 1 : 256
for j = 1 : x
for k = 1 : y

% if image pixel value at location (j, k) is i-1


% then increment count
if img(j, k) == i-1
count = count + 1;
end
end
end
% update ith position of frequency array with count
frequency(i) = count;

% reset count
count = 0;

end

n = 0 : 255;

% Display Histogram
stem(n, frequency);

grid on;
ylabel('Number of pixels with such intensity levels -->');
xlabel('Intensity Levels -->');
title('HISTOGRAM OF THE IMAGE');

2) Histogram equalization(histeq)

A = rgb2gray(imread('C:\Users\niit\Desktop\Lenna.png'))

J = histeq(A);
% imshowpair(A, J, 'montage')

imhist(J, 64)
title('Histogram equalized')

22/10/19
Experiment 10: Convolution
1)
X1 = randi([0, 256], [3, 3])
X2 = randi([0, 256], [4, 4])
T = conv2(X1, X2)

T = conv2(X1, X2)
T(3:5,3:5)

2)
A = zeros(3);
A(:) = 1:9;
B = ones(3)/9;
Cfull = conv2(A,B);
display(Cfull);
Csame = conv2(A,B,'same');
display(Csame);
A = zeros(3);
A(:) = 1:9;
avg3 = ones(3)/9;

%PAD THE MATRIX WITH ZEROS


B = padarray(A,[1 1]);
Output = zeros([size(A,1) size(A,2)]);
%PERFORM CONVOLUTION
for i = 1:size(B,1)-2
for j = 1:size(B,2)-2
Temp = B(i:i+2,j:j+2).*avg3;
Output(i,j) = sum(Temp(:));
end
end
display(Output);

26/11/19

Experiment 11
I=imread('C:\Users\niit\Desktop\Coins.png');
figure
imshow(I)
title('Original Image');
h=ones(5,5)/25;
I2=imfilter(I,h);
figure
imshow(I2)
title('Filtered image')
%Filter Multidimensional Truecolor (RGB) Image Using imfilter
rgb = imread('C:\Users\niit\Desktop\peppers.png');
imshow(rgb);
h = ones(5,5)/25;
rgb2 = imfilter(rgb,h);
figure
imshow(rgb2)
2)
I = imread('C:\Users\niit\Desktop\Coins.png');
I = rgb2gray(I);
J = imnoise(I,'salt & pepper');
K = medfilt2(J);
%imshow(K)
imshowpair(J,K,'montage')

I = rgb2gray(imread('C:\Users\niit\Desktop\Coins.png'));
Min filter:

J = zeros(size(I));
modifyI = padarray(I,[1 1])
x = [1:3]';
y = [1:3]';
for i = 1:size(modifyI,1)-2
for j = 1:size(modifyI,2)-2
window = reshape(modifyI(i+x-1, j+y-1),[],1);
J(i,j) = min(window);
end
end
J = uint8(J)
imshowpair(I,J,'montage');

Max filter:
J = zeros(size(I));
modifyI = padarray(I,[1 1])
x = [1:3]';
y = [1:3]';
for i = 1:size(modifyI,1)-2
for j = 1:size(modifyI,2)-2
window = reshape(modifyI(i+x-1, j+y-1),[],1);
J(i,j) = max(window);
end
end
J = uint8(J)
imshowpair(I,J,'montage');

Experiment 12

a)Sobel operator

A=imread('peppers.png');
B=rgb2gray(A);

C=double(B);

for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Sobel mask for x-direction:
Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-
(2*C(i,j+1)+C(i,j)+C(i,j+2)));
%Sobel mask for y-direction:
Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-
(2*C(i+1,j)+C(i,j)+C(i+2,j)));

%The gradient of the image


%B(i,j)=abs(Gx)+abs(Gy);
B(i,j)=sqrt(Gx.^2+Gy.^2);

end
end
figure,imshow(B); title('Sobel gradient');
Thresh=100;
B=max(B,Thresh);
B(B==round(Thresh))=0;

B=uint8(B);
figure,imshow(~B);title('Edge detected Image');

b)Prewitt edge detection:

b=im2double(a);
a=imread('C:\Documents and
Settings\student\Desktop\lenna1.bmp');

b=im2double(a);
[m,n]=size(a);

N(1:m,1:n)=0
for i=1:m-2;
for j=1:m-2;
N(i,j)=-1*b(i,j)-1*b(i,j+1)-
1*b(i,j+2)+0+0+0+1*b(i+2,j)+1*b(i+2,j+1)+1*b(i+2,j+2);
end;
end;
O(1:m,1:n)=0
for i=1:m-2;
for j=1:m-2;
O(i,j)=-1*b(i,j)+0+1*b(i,j+2)-
1*b(i+2,j)+0+1*b(i+1,j+2)-1*b(i+2,j)+0+1*b(i+2,j+2);
end;
end;
figure;
subplot(2,2,1)
imshow(N)
title('Prewit Gx');
subplot(2,2,2)
imshow(O)
title('Prewit Gy');
Z=N+O;
subplot(2,2,3)
imshow(Z)
title('Prewit Gx+Gy');
subplot(2,2,4)
imshow(b)
title('Original Image');

c)Robert edge detection

%ROBERT

L(1:m,1:n)=0

for i=1:m-2;

for j=1:m-2;

L(i,j)=-1*b(i,j)+0+0+1*b(i+1,j+1);

end;

end;
M(1:m,1:n)=0

for i=1:m-2;

for j=1:m-2;

M(i,j)=0-1*b(i,j+1)+1*b(i+1,j)+0;

end;

end;

figure;

subplot(2,2,1)

imshow(L)

title('Robert Gx');

subplot(2,2,2)

imshow(M)

title('Robert Gy');

N=M+L;

subplot(2,2,3)

imshow(N)

title('Robert Gx+Gy');

subplot(2,2,4)

imshow(b)

title('Original Image');

S-ar putea să vă placă și