MATLAB Answers

Neighbors of a pixel

138 views (last 30 days)
I want to calculate the mean of the neighbors of the pixels in an image. I want to do it for all pixels not only the internal ones. That means for example that I want to calculate the mean of neighbors of the pixels (1,1) or (1,size(image,2)) or (size(image,1),size(image,2)) which means I cannot use a matrix divided by 8 as a kernel for all these pixels because for example neighbors of (1,1) are only 3 not 8. Does anyone have an idea how to do it without using 8 ifs?

Accepted Answer

Johannes Korsawe
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
  1 Comment
Efstathios Kontolatis
Efstathios Kontolatis on 14 Oct 2016
Thank you very much that's correct

Sign in to comment.

More Answers (3)

Guillaume
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.

Image Analyst
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.

Rose Mahmudi
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
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!