Showing posts with label Basic Functions in Matlab. Show all posts
Showing posts with label Basic Functions in Matlab. Show all posts

Sunday, March 27, 2016

Gray Level Slicing in MATLAB

 Gray Level Slicing: High-light specific range of gray values without background and with background.

       Read kidney.tif file. Enter lower and upper threshold value from the user. Apply gray level slicing with and without background. Display the result.

 CODE:
clc; clear all;

i=imread('kidney.tif');          % should be graylevel image
j=double(i);
k=double(i);
[row,col]=size(j);
T1=input('Enter the Lowest threshold value:');
T2=input('Enter the Highest threshold value:');
for x=1:row            
    for y=1:col        
        if((j(x,y)>T1) && (j(x,y)<T2))
            j(x,y)=i(x,y);
            k(x,y)=255;
        else
            j(x,y)=0;
            k(x,y)=0;
        end
    end
end

subplot(311), imshow(i), title('Original image')   
subplot(312), imshow(uint8(j)), title('Graylevel slicing with background')
subplot(313), imshow(uint8(k)), title('Graylevel slicing without background')

OUTPUT RESULT:

Thursday, March 24, 2016

How to apply Threshold to an image in MATLAB ?

Exercise: Apply thresholding to the original image such that r1= r2=mean gray level. Display the result. (Hint: use built-in function im2bw.)

 CODE:

clc; clear all;

%% Reading an image
a1=imread('contrast.jpg');
a=double(a1);
[row,col]=size(a);

%% Calculating mean gray level
sum = 0;
for i=1:row
    for j=1:col
        sum=sum+a(i,j);
    end
end
avg=sum/(row*col);

%% Transformation function
t=0:255;
x1=0*(t>=0 & t<avg); 
x2=255*(t>=avg & t<=255);
x=x1+x2;

%% Obtaining contrast stretched image
for n=1:row
    for m=1:col
        out(n,m)=x(a1(n,m)+1);
    end
end

out1=im2bw(a1);

plot(x)
grid on;
xlabel('Intensity in input image');
ylabel('Intensity in output image')
title('Transformation function')

figure()
subplot(311), imshow(a1), title('Original image')
subplot(312), imshow(uint8(out)), title('Thresholding(using code)')
subplot(313), imshow(out1), title('Thresholding(using im2bw)')


OUTPUT RESULT:


Transformation Function

Wednesday, March 23, 2016

How to apply Power Law Transformation in MATLAB ?

Read remote.jpg image which is an aerial image which has washed out appearance. Compression of gray level is required. Apply power law transformation with γ =3,4,5. Display and comment on the results.


CODE:

clc; clear all;

c=1;
Gamma=input('Enter the Gamma values = ');       % Must be vector, Ex:[3 4 5]
x=imread('remote.jpg');
x1=double(x);      
y=c*(x1.^Gamma(1));                             % s=c*(r^ γ)
y1=c*(x1.^Gamma(2));
y2=c*(x1.^Gamma(3));

subplot(141),imshow(x), title('Aerial image')
subplot(142),imshow((y),[]), title('Corrected image(Gamma=3)')
subplot(143),imshow((y1),[]), title('Corrected image(Gamma=4)')
subplot(144),imshow((y2),[]), title('Corrected image(Gamma=5)')

OUTPUT RESULT: