Example of applying a mask on an image using MATLAB (Convolution)

Supposing that we will apply

Blur kernel


on an image.


%read an image and assign to a variable
Input = imread('image path');

%convert image to grayscale
Input = rgb2gray(Input);

%get size of the image
[r,c] = size(Input);

Output = Input;

%apply the filter
for i=2:r-1
for j=2:c-1
Output(i,j) = Input(i-1,j-1)*1/9 +
Input(i-1,j)*1/9 + Input(i-1,j+1)*1/9 + Input(i,j-1)*1/9 + Input(i,j)*1/9 + Input(i,j+1)*1/9 + Input(i+1,j-1)*1/9 + Input(i+1,j)*1/9 + Input(i+1,j+1)*1/9;
end
end

figure, imshow(Input)
figure, imshow(Output)


This is the not-yet-test script. Please leave me a message if it doesn't work :)

This is an illustration on how the algorithm works


Applying a 3x3 Matrix Convolution: Matrix convolutions work by averaging pixel colors with those of surrounding pixels using this process.

(source from www.devx.com/webdev/Article/37179/0/page/7)

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...