Showing posts with label Contrast streching in matlab. Show all posts
Showing posts with label Contrast streching in matlab. Show all posts

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 :


Thursday, March 24, 2016

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