Sie sind auf Seite 1von 15

Matlab Fundamental Function

Experiment No. - 1
a) Aim: Reading an Image

Theory: To import an image from any supported graphics image file format, in any of the
supported bit depths, use the imread function.

Syntax: A = imread(filename,fmt)

Description: A = imread(filename,fmt) reads a greyscale or color image from the file
specified by the string filename, where the string fmt specifies the format of the file.

Example:
A=imread('test','tif')


b) Aim: Display an Image

Theory: To display image, use the imshow function.

Syntax: imshow(A):

Description: imshow(A) displays the image stored in array A.

Example:
imshow(A);title('displaying image in matlab');
Result:




c) Aim: Writing Image Data

Theory: imwrite function write image to graphics file

Syntax: imwrite(A,filename,fmt)

Description: imwrite(A,filename,fmt) writes the image A to the file specified by filename
in the format specified by fmt.

Example:
a=imread('test2.jpg');
imshow(a);title('Original Image');
imwrite(rgb2gray(a),'t2.tif');




Input:



Result: image t2.tif








Basic Operations On Images

Experiment No. - 2


Aim: Display all three color-band of a true color image.

Program:

A=imread('test2.jpg');
subplot(2,2,1);imshow(A);title('Original Image');

% TO SHOW RED-BAND

B=A;
B(:,:,2)=0;
B(:,:,3)=0;
subplot(2,2,2);imshow(B);title('Red Component of The Image');

% TO SHOW GREEN-BAND

B=A;
B(:,:,1)=0;
B(:,:,3)=0;
subplot(2,2,3);imshow(B);title('Green Component of The Image');

% TO SHOW BLUE-BAND
B=A;
B(:,:,1)=0;
B(:,:,2)=0;
subplot(2,2,4);imshow(B);title('Blue Component of The Image');

Result:


Experiment No. - 3

Aim: Mirror Image Generation.

Program:

clear
A=imread('test.tif');
[r c]=size(A);
for i=1:r
for j=1:c
B(i,j)=A(i,c-j+1);
end
end
subplot(1,2,1); imshow(A);title('Original image');
subplot(1,2,2); imshow(B);title('Mirrored Image');


Result:














Experiment No. 4

Aim: Flipped Image Generation.

Program:

clear
A=imread('test.tif');
[r c]=size(A);
for i=1:r
for j=1:c
B(i,j)=A(r-i+1,j);
end
end
subplot(1,2,1); imshow(A);title('Original image');
subplot(1,2,2); imshow(B);title('Flipped Image');




Result:














Gray-level transformation functions

Experiment No. - 5

Aim: Image Negative Function

Program:

clear
A=imread('test3.tif');
[r c]=size(A);
L=256;
for i=1:r
for j=1:c
B(i,j)=L-1-A(i,j);
end
end
subplot(1,2,1); imshow(A); title('Original Image');
subplot(1,2,2); imshow(B);
title('Image after Negative Transformation');


Result:












Experiment No. - 6

Aim: Log transformation function

Program:
clear
A=imread('test5.tif');
r c]=size(A);
L=256;
C=1;
for i=1:r
for j=1:c
B(i,j)=c*(log(double(A(i,j))+1));
end
end
subplot(1,2,1); imshow(A);title('input image');
subplot(1,2,2); imshow(im2uint8(mat2gray(B)));title('output image');


Result:














Experiment No. - 7

Aim: Power Law (Gamma) Transformation Function

Program:
clear
A=imread('test6.tif');
[r c]=size(A);
L=256;
const_val=1;
g=5; %gamma vaariable
for i=1:r
for j=1:c
B(i,j)=const_val*(power(im2double(A(i,j)),g));
end
end
subplot(1,2,1); imshow(A);title('input image');
subplot(1,2,2); imshow(im2uint8(B));
title('output image at g = 5 and c =1');

Result:













Experiment No. - 8
Aim: Gray - Level Thresholding Of The Image (Binarization)

Program:

clear
A=imread('test4.tif');
[r c]=size(A);
L=256;
t=input('enter the threshold value'); %t=128 is entered
for i=1:r
for j=1:c
if(A(i,j)>t)
B(i,j)=L-1;
else
B(i,j)=0;
end
end
end
subplot(1,2,1); imshow(A)
subplot(1,2,2); imshow(B)

Result:








Histogram Processing

Experiment No. - 9
Aim: Draw The Histogram Of Given Image.

Program:
clear
A=imread('test4.tif');
subplot(1,2,1); imshow(A)
subplot(1,2,2); my_hist(A)

my_hist.m
function my_hist(img)

[r c]=size(img);
x=[0:255];
y=zeros(1,256);
for i=1:r
for j=1:c
y(img(i,j)+1)=y(img(i,j)+1)+1;
end
end
stem(x(1:256),y(1:256),'marker','none')

Result:









Experiment No. - 10

Aim: Performing Histogram Equalization.

Program:
clear
A=imread('test7.tif');
B=my_histeq(A);
subplot(2,2,1); imshow(A);title('input image');
subplot(2,2,2); my_hist(A); title('histogram of the i/p image');
subplot(2,2,3); imshow(B);title('output image');
subplot(2,2,4); my_hist(B); title('histogram of the o/p image');

my_histeq.m

function eq_img=my_histeq(img)

[r c]=size(img);
y=zeros(1,256);
L=256;
for i=1:r
for j=1:c
y(img(i,j)+1)=y(img(i,j)+1)+1;
end
end

total_pixel=r*c;
norm_y=y/total_pixel;
cum_y(1)=norm_y(1);

for i=2:256
cum_y(i)=cum_y(i-1)+norm_y(i);
end

new_value= round(cum_y*(L-1));

for i=1:r
for j=1:c
eq_img(i,j)=uint8(new_value(img(i,j)+1));
end
end
















Result:

























Image Enhancement using Filters

Experiment No. 11

Aim: Performing Smoothing in Spatial Domain using Mean Filter

Program:

clear
clc
z=imread('test8.tif');
[r,c]=size(z);
m_size=input('Enter t5he size of mask');
avg_val=m_size*m_size;
c_pos=ceil(m_size/2);
b_dist=m_size-c_pos;

d=z;

for i=c_pos:r-b_dist
for j=c_pos:c-b_dist
t=0;
for p=i-b_dist:i+b_dist
for q=j-b_dist:j+b_dist
t=t+(z(p,q)/avg_val);
end
end
d(i,j)=t;
end
end
subplot(1,2,1); imshow(z);title('input image');
subplot(1,2,2); imshow(d);
title('output of filtering when avg filter size is 5x5 ');

Result:




Experiment No. 12

Aim: Performing Sharpening in Spatial Domain using Second Order Derivatives:
The Laplacian

Program:

clear
clc

%Input Image
A=imread('test_coins.tif');
subplot(1,3,1);imshow(A);title('Original Image');
%Preallocate the matrices with zeros
I1=A;
I=zeros(size(A));
I2=zeros(size(A));

%Filter Masks
F1=[0 1 0;1 -4 1; 0 1 0];
F2=[1 1 1;1 -8 1; 1 1 1];

%Padarray with zeros
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);
subplot(1,3,2);imshow(I);title('Filtered Image');
%Sharpenend Image

B=I1-I;
subplot(1,3,3);imshow(B);title('Sharpened Image');

Result:

Das könnte Ihnen auch gefallen