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

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)

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

0 votes

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

It doesn't make any sense. Say what you want as an output. Is it another image? Is it an N-by-16 matrix/list of (x,y) or (row, column) coordinates for every 8 neighbors for every pixel in the image (though that seems useless)? What do you really want to do?
I want a matrix 3 by 3 of each pixels in output. infact the value of graylevel 8-neighborhood for each pixle with the center pixle in a matrix 3by3.
I want to use this output for an input of fuzzy system.
why it doesn't make sens???
It was confusing for the reasons I gave - multiple ways to interpret that. "8 neighborhood connectivity" is a concept for how you process pixels -- it's not any specific kind of set of numbers, like their gray levels, their coordinates, their histogram, their mean, or whatever.
See the attached demo. It gives you an N-by-10 matrix: row, column, then the 8 neighbor pixel gray levels. Adapt as needed. If it doesn't work, attach it with your image to a new question.
thank you it works for me .
now I have a nother problem.
you know I wanted to use this output in a fuzzy system (a .fis) .the problem is when I run the code in a .m seprated file it works fast. but I need to do this code as a seprate function and then use the uotput of fuzzy system in my first code but this time it tooks about 400sec I don't know why ???
how could I fix this?is the problem with fuzzy system??? or what else?
appreciate your help :)
I have no idea. My code should run in pretty close to the same amount of time regardless if it's a script or a function. Are you sure it's MY code you're talking about and not your fuzzy code?
no your code is great . I know it's my evalfis that took so much time but I don't know why it run in a sec when it's not a function but when it's a function it takes 400 sec.
I don't know. I don't have the Fuxxy toolbox and never run evalfis(). Good luck though.
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.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!