Monday, November 18, 2013

Write a program in MATLAB to design a Mask to generate a mean image considering 8 neighborhood.

clc;
clear all;
close all;

A=imread('median.tif');
A = im2double(A);
G=A;
% Pad the matrix with zeros on all sides
modifyA=zeros(size(A)+2);
% 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 Arithmetic Mean Operation please wait......');
m=3;n=3;
       
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
        s=0;
        kernel = ones(m, n) / 9; % mxn(8-neighbor) mean kernel
        for x = 1:m
            for y = 1:n
                s=s+modifyA(i+x-1,j+y-1)*kernel(x,y);
            end
        end
        G(i,j)=s;
    end
    waitbar(i/size(modifyA,1));
end
figure('Position',[100  100 1200 550]);
subplot(1,2,1),imshow(A);
ylabel('salt-papper noise Image','Color','r','FontSize', 20,'FontAngle', 'italic');
subplot(1,2,2),imshow(G);
ylabel('Arithmetic mean filter','Color','r','FontSize', 20,'FontAngle', 'italic');
close(h);

No comments:

Post a Comment