function [ img ] = avg_neighbourhood( image, n )%AVG_NEIGHBOURHOOD Blurs the image by replacing each pixel by the average of its
% (2*n+1) x (2*n+1) neighbourhood. Pixels out of the image's range
clc; close all;clear all; [F path]=uigetfile('*.jpg;*.tif;*.png;*.gif','Select an image'); F=strcat(path,F); image= imread(F); n=4% 'n' must be a non-negative integer
if ( n<0 | floor(n)~=n ) error('Invalid n');end %if
% image's size:
[rows, cols] = size(image);% Create a matrix with the same dimensions
img = zeros(rows, cols);% for each pixel...
for i = 1 : rows for j = 1 : cols % make sure its nxn neighbourhood wil not reach out
% of the image's range!
i_lower = max([1, i-n]); i_upper = min([rows, i+n]); j_lower = max([1, j-n]);if true % code
end j_upper = min([cols, j+n]); % Sum up all valid pixels of the nxn neighbourhood...
img(i, j) = sum( sum( image(i_lower : i_upper,j_lower : j_upper) ) ); % And divide by the number of the neighbourhood's valid pixels:
img(i, j) = img(i, j) / ((i_upper - i_lower + 1)*(j_upper - j_lower + 1)); end % for j
end % for i
% Finally round the pixels to integer values:
img = uint8( floor( img + 0.5 ) );figureimshow(img)end
MATLAB: I use this code for averaging the neighbourhood but i got 3 time enlarged image any one help me
averaged neibconvolutionImage Processing Toolboximfilter
Best Answer