Sunday, April 10, 2016

Matlab: Smoothing spatial filter using manual convulation V2

%Smoothing spatial filter using manual convulation
%page=145

clear all;
close all;
img=imread('8.jpg');

%img=imread('moon.tif');
img=rgb2gray(img);

subplot(1,2,1);
imshow(img);
title('Original Image');

%L=[1 2 1;2 4 2;1 2 1];
L=(1/9)*[1 1 1;1 1 1;1 1 1];

out_img=uint8(conv_img(L,img));

subplot(1,2,2);
imshow(out_img);
title('Smoothing Image');


%------function--------
%convolution of image
function [Output] = img_conv2( mask,Input)

v=flipdim(mask,2);
w = flipdim(v,1);

[x y] = size(Input);
[m n]=size(w);


k1=(m-1)/2;
k2=(n-1)/2;
tempInput=padarray(Input,[k1 k2]);

for i = 1 : x
    for j = 1 : y
         aValue = tempInput(i,j)*w(1,1)+tempInput(i,j+1)*w(1,2)+tempInput(i,j+2)*w(1,3)+tempInput(i+1,j)*w(2,1)+tempInput(i+1,j+1)*w(2,2)+tempInput(i+1,j+2)*w(2,3)+ tempInput(i+2,j)*w(3,1)+tempInput(i+2,j+1)*w(3,2)+tempInput(i+2,j+2)*w(3,3);
         Output(i,j)= aValue;
    end
end