Tuesday, December 3, 2013

Write a program in MatLab to extract a part of image using without a function .


clc;
clear all;
close all;

A=imread('contrast (3).tif');
r=size(A,1)-1;
i=int32(rand(1,1)*r)+1; % +1 for random value always from 0
j=i+500;
if(j>size(A,1))
    d=j-size(A,1);
    j=j-d;
end
c=size(A,2)-1;
k=int32(rand(1,1)*c)+1; % +1 for random value always from 0
l=k+500;
if(l>size(A,2))
    d1=l-size(A,2);
    l=l-d1;
end
B=zeros(size(A,1),size(A,2));
x=i;
for p=i:j-1
    y=k;
    for q=k:l-1
        B(x,y)=A(p,q);
        y=y+1;
    end
    x=x+1;
end
B1 = mat2gray(B);
figure('Position',[10  50 1400 630]);
subplot(1,2,1),imshow(A),
ylabel('Original Image','Color','r','FontSize', 10,'FontAngle', 'italic');
subplot(1,2,2),imshow(B1),
ylabel('Sub Image','Color','r','FontSize', 10,'FontAngle', 'italic');

Write a program in MatLab to enhance brightness and contrast of RGB and Gray images.

clc;
clear all;
close all;

im=imread('contrast (3).tif');
d=ndims(im);
if(d==2)
%----------------------------------------------------------
%                       Gray Image
%----------------------------------------------------------
    im=double(im);
    [M N]=size(im);
    MN=M*N;
    sum=0;
    for i=1:M
        for j=1:N
            sum = sum + im(i,j);
        end
    end
    B=sum/MN;
    disp(['Brightness of an Gray Image is ',num2str(B)]);
    sum=0;
    for i=1:M
        for j=1:N
            S=im(i,j)-B;
            S=S^2;
            sum = sum + S;
        end
    end
    C=sum/MN;
    con=double(C)^0.5;
    disp(['Contrast of an Gray Image is ',num2str(con)]);
else
%----------------------------------------------------------
%                       RGB
%----------------------------------------------------------
    B_sum=0;
    C_sum=0;
    for k=1:3
        I=im(:,:,k);
        I=double(I);
        [M N]=size(I);
        MN=M*N;
        sum=0;
        for i=1:M
            for j=1:N
                sum = sum + I(i,j);
            end
        end
        B=sum/MN;
        B_sum=B_sum+B;
        sum=0;
        for i=1:M
            for j=1:N
                S=I(i,j)-B;
                S=S^2;
                sum = sum + S;
            end
        end
        C=sum/MN;
        con=double(C)^0.5;
        C_sum=C_sum+con;
    end
    disp(['Brightness of an RGB image is ',num2str(B_sum)]);
    disp(['Contrast of an RGB image is ',num2str(C_sum)]);
end

Write a program in MatLab to remove background of an image.


clc;
clear all;
close all;

f=imread('n.jpg');
f=rgb2gray(f);
sub=zeros(size(f));
[ROW COLUMN]=size(sub);
f=im2double(f);

% Global Thresholding
T=0.5*(min(f(:))+max(f(:)));
done=false;
while ~done
    g=f>=T;
    Tn=0.5*(mean(f(g))+mean(f(~g)));
    done=abs(T-Tn)<0.1;
    T=Tn;
end
r=im2bw(f,T);
R=double(r);
%Image Subtraction
for i=1:ROW
        for j=1:COLUMN
            sub(i,j)=R(i,j)-f(i,j);
        end
end
% Sliding Neighborhood Normalization
fun = @(x) mode(x(:));
B = nlfilter(sub,[3 3],fun);

figure;
subplot(2,2,1),imshow(f),title('Original Image');
subplot(2,2,2),imshow(r),title('Global Thresholding - Iterative Method');
subplot(2,2,3),imshow(sub),title('Subtracted Image');
subplot(2,2,4),imshow(B),title('Final Result');

Monday, December 2, 2013

Write a program to implement Histogram equilization using MatLab.


clc;
clear all;
close all;

A1=imread('E:\M.Tech\1st Semester\Image Processing\Lab\histogram.tif');
B1=A1;
[row column]=size(B1);
L=256;
MN=row*column;
nk=zeros(1,L); %nk is the number of pixels that have intensity rk.
for i=1:row
    for j=1:column
        nk(B1(i,j)+1)=nk(B1(i,j)+1)+1;
    end
end
% The probability of occurence of intensity level rk in a image pr(rk).
nk=nk/MN;
% Transformation T(rk):a output image is obtained by transforming each
% pixel in the input image with intensity rk into a corresponding pixel
% with level sk in the output image.
s=zeros(1,L);
sum=0;
for k=1:L
    sum=sum+nk(k);
    s(k)=round((L-1)*sum);
end
% Equalization
equalized=zeros(1,L);
for i=1:L
    equalized(s(i)+1)=equalized(s(i)+1)+nk(i);
end
% Convert the image using transformation s(k)
for i=1:row
    for j=1:column
        B1(i,j)=s(B1(i,j)+1);
    end
end

figure;
subplot(2,2,1),imshow(A1),title('Original image');
subplot(2,2,2),imshow(B1),title('Equalized image');
subplot(2,2,3),bar(nk,'g'),title('Histogram of Original');
subplot(2,2,4),bar(equalized,'g'),title('Equalized Histogram');