# Neighbors of a pixel

60 views (last 30 days)

Show older comments

Efstathios Kontolatis
on 14 Oct 2016

Commented: Rose Mahmudi
on 28 Apr 2019

##### 0 Comments

### Accepted Answer

Johannes Korsawe
on 14 Oct 2016

Let A be your matrix.

% use the help of a bigger matrix

B=nan(size(A)+2);

B(2:end-1,2:end-1)=A;

% pre-define memory for result

result = 0*A;

% calculate!

for i=2:size(A,1)+1,

for j=2:size(A,2)+1,

tmp=B(i-1:i+1,j-1:j+1);

tmp(2,2)=nan;

result(i-1,j-1)=mean(tmp(~isnan(tmp)));

end

end

### More Answers (3)

Guillaume
on 14 Oct 2016

Well, you can certainly use a convolution for the central part. I would just use smaller convolution kernels for the edges so:

img = reshape(1:200, 10, 20); %demo image

meanimg = [mean2(img(1:2, 1:2)), conv2(img(1:2, :), ones(2,3)/6, 'valid'), mean2(img(1:2, end-1:end)); ...

conv2(img(:, 1:2), ones(3,2)/6, 'valid'), conv2(img, ones(3)/9, 'valid'), conv2(img(:, end-1:end), ones(3,2)/6, 'valid'); ...

mean2(img(end-1:end, 1:2)), conv2(img(end-1:end, :), ones(2,3)/6, 'valid'), mean2(img(end-1:end, end-1:end))]

That is one convolution for the central part, 1 convolution for each edge and just mean2 for each corner.

##### 0 Comments

Image Analyst
on 14 Oct 2016

I'd do it a different way. I'd do a full convolution so that I can get the sums and pixel counts at each window location. Then I'd crop off the outer layer (to give an output of the same size as the original) and finally divide them. Here's my demo, with extensive comments.

% Read in sample image.

grayImage = imread('cameraman.tif');

% Make an image of 1's so we can count how many

% neighbors there are at each pixel location.

binaryImage = ones(size(grayImage));

% Define a kernel to do the summing of the images at each location.

kernel = ones(3);

% Get sum of gray levels at each window location.

% Use 'full' option so we can let the window slide out and count neighbors of edge pixels.

sumImage = conv2(double(grayImage), kernel, 'full');

% Count the pixels at each window location.

countImage = conv2(double(binaryImage), kernel, 'full');

% Get the mean by dividing the sum by the pixel count.

% but ignore the outer 1-pixel-wide layer.

meanImage = sumImage(2:end-1, 2:end-1) ./ countImage(2:end-1, 2:end-1);

Don't be afraid - the actual code is only 5 lines long.

##### 0 Comments

Rose Mahmudi
on 15 Apr 2019

hello guys

I need help with the same question but a little diffrent.

I want to obtain all 8 neighborhood connectivity for each pixles in an image.

so after i read the image and convert it to gray level image what can I do for obtaining 8neighbor-c???

and I have another problem ... I want to use first row as neighbor for the last row and vice versa. also I want to do same for columns.

could you help me figure out pleaseeeee.

thank you very much

##### 8 Comments

Rose Mahmudi
on 28 Apr 2019

thank you very much for your code and your help. I'll try to figure it out some how.

:) best regards

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!