Tuesday, March 29, 2016

Histogram Processing in MATLAB

     Read and Plot the histogram of dark, bright, Low contrast and High contrast image without using built-in function. Verify with the built-in function imhist.

       CODE :

clc; clear all;
img1=imread(
'hist1.jpg');
img2=imread(
'hist2.jpg');
img3=imread(
'hist3.jpg');
img4=imread(
'hist4.jpg');
subplot(431), imshow(img1), title(
'Dark image')
subplot(434), imshow(img2), title(
'Bright image')
subplot(437), imshow(img3), title(
'Low contrast image')
subplot(4,3,10), imshow(img4), title(
'High contrast image')

for a=2:3:11
   
if(a==2)
        img=img1;
   
elseif(a==5)
        img=img2;
   
elseif(a==8)
        img=img3;
   
else
        img=img4;
   
end

[M, N]=size(img);
histbar=zeros(1,256);
for i=1:M
   
for j=1:N
            v=img(i,j);
            histbar(v+1)=histbar(v+1)+1;
   
end
end
subplot(4,3,a), bar(histbar);
if (a==2), title('Histogram Using code'); end
subplot(4,3,a+1), imhist(img);
if (a==2), title('Histogram using Built-in function'); end
end

OUTPUT RESULT :


Monday, March 28, 2016

Bit Plane Slicing in MATLAB

       Read fractal.jpg file. Display individual bits as binary image. Display and comment on the result.  (Hint: use bitget built-in function)

CODE:

clc; clear;

A=imread('fractal.jpg');
[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


OUTPUT RESULT :

We observe that the three higher order planes, especially the 8th bit plane, contain a significant amount of the visually significant data. The lower order planes contribute to more subtle intensity details in the image. Decomposing an image into its bit planes is useful for analyzing the relative importance of each bit in the image and is useful for image compression.

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

Contrast stretching in MATLAB

     Increase the dynamic range of gray values in the input image.

Read contrast.jpg image which is a low contrast image. Apply the transformation such that r1=rmin, S1=0, r2=rmax, S2=L-1. Plot the transformation function and display the contrast stretched image.

CODE:

clc; clear all;

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

%% Transformation function (r1,s1)=(rmin,0), (r2,s2)=(rmax,255)
t=0:255;
x1=0*(t>=0 & t<=100); 
x2=7.285*(t-100).*(t>100& t<135);          
x3=255*(t>=135 & t<=255);
x=x1+x2+x3;

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

out1=imadjust(a1,stretchlim(a1),[]);

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

figure()
subplot(131), imshow(a1), title('Original image')
subplot(132), imshow(uint8(out)), title('Contrast Stretching(using code)')
subplot(133), imshow(out1), title('Contrast Stretching(using stretchlim)')



OUTPUT RESULT:


Transformation Output

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:


Tuesday, March 22, 2016

Basic Intensity Transformation Functions

1) Image Negative: s=L-1-r where r is the input intensity level and s is the output intensity level.
Read northpole.jpg image, obtain its negative. Display the original image and its negative.

clc; clear all;

a=imread('northpole.jpg');
b=255-a;                    % s=L-1-r
subplot(211), imshow(a), title('Northpole Image')
subplot(212), imshow(b), title('Negative of Northpole')



2)   Log Transformation:   s=T(r)=clog(1+r) where c is a constant and r is the input gray level.

    Read fourier.jpg image and apply log transformation and display the fourier Spectrum.

clc; clear all;

in=imread('fourier.jpg');
c=input('Enter the constant value, c = ');
a=im2double(in);
a=a*255;
out=c*log10(1+a);                   % s=T(r)=clog(1+r)
out=out/max(max(out));              % Normalization
subplot(121), imshow(in), title('Original Image')
subplot(122), imshow(out), title('Log Transformed Image(c=1)')



3)   Power Law Transformations: s=crγ. To account for an offset, when the input is zero use s= c(r+ε)γ.
 Read crt.jpg, apply the transformation function with γ=1/2.5. Display the gamma corrected image.
clc; clear all;

c=1;
Gamma=input('Enter the Gamma value = ');
x=imread('crt.jpg');
x1=double(x);      
y=c*(x1.^Gamma);                % s=c*(r^ γ)

subplot(211),imshow(x), title('CRT input image')
subplot(212),imshow((y),[]), title('Corrected image(Gamma=0.4)')



4)     Contrast Manipulation:
Read mr.jpg image which is a magnetic resonance image with an upper thoracic human spine with a fracture dislocation. Apply power law transformation with exponents γ=0.6,  γ=0.4,  γ=0.3. Display the outputs and comment on the results.

clc; clear all;

c=1;
Gamma=input('Enter the Gamma value = ');  % Must be vector, Ex:[0.6 0.4 0.3]
x=imread('mr.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('MRI scanned image')
subplot(142),imshow((y),[]), title('Corrected image(Gamma=0.6)')
subplot(143),imshow((y1),[]), title('Corrected image(Gamma=0.4)')
subplot(144),imshow((y2),[]), title('Corrected image(Gamma=0.3)')