필터 지우기
필터 지우기

Neighbors of a pixel

조회 수: 37 (최근 30일)
Efstathios Kontolatis
Efstathios Kontolatis 2016년 10월 14일
댓글: Rose Mahmudi 2019년 4월 28일
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?

채택된 답변

Johannes Korsawe
Johannes Korsawe 2016년 10월 14일
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
Efstathios Kontolatis
Efstathios Kontolatis 2016년 10월 14일
Thank you very much that's correct

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Guillaume
Guillaume 2016년 10월 14일
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 2016년 10월 14일
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 2019년 4월 15일
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
Image Analyst
Image Analyst 2019년 4월 27일
I don't know. I don't have the Fuxxy toolbox and never run evalfis(). Good luck though.
Rose Mahmudi
Rose Mahmudi 2019년 4월 28일
thank you very much for your code and your help. I'll try to figure it out some how.
:) best regards

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by