Tuesday, December 3, 2013

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');

Monday, November 18, 2013

Write a program in MATLAB to generate a median of an image.

clc;
clear all;
close all;

A=imread('median.tif');
% Pad the matrix with zeros on all sides
modifyA=zeros(size(A)+2);
B=zeros(size(A));
% copy the original image matrix to the padded matrix
for x=1:size(A,1)
    for y=1:size(A,2)
        modifyA(x+1,y+1)=A(x,y);
    end
end
 
h = waitbar(0,'Calculating Median Operation please wait......');

% Let the window be an array store the 3-by-3 neighbour
% values in the array sort and find the middle element
     
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
        window=zeros(9,1);
        inc=1;
        for x=1:3
            for y=1:3
                window(inc,1)=modifyA(i+x-1,j+y-1);
                inc=inc+1;
            end
        end
        med=sort(window);
        % Place the median element in the output matrix
        B(i,j)=med(5);
    end
    waitbar(i/size(modifyA,1));
end
% Convert the output matrix to 0-255 range image type
B=uint8(B);
figure('Position',[100  100 1200 550]);
subplot(1,2,1),imshow(A),
ylabel('Image with salt-papper noise','Color','r','FontSize', 20,'FontAngle', 'italic');
subplot(1,2,2),imshow(B),
ylabel('Image after median filtering','Color','r','FontSize', 20,'FontAngle', 'italic');
close(h);