Is there a way to find the neighbourhood pixels, given a cordinate point of the pixel?

32 visualizaciones (últimos 30 días)
I need to find out the neighbourhood pixel values, given a particular coordinate point in an image. the pixel point is user defined.
  6 comentarios
Image Analyst
Image Analyst el 22 de Abr. de 2016
Sorry, I haven't worked at that task before, and I don't have any demos. I'd have to delve into it and learn it just as you would.
shikha mangal
shikha mangal el 22 de Abr. de 2016
Ohh,well ok..i am still thankful to you for your help.If anyone find something related to the above problem please let me know it will be much helpful to me thanks.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 5 de Sept. de 2014
neighbors(1) = img(r-1,c-1); % Upper left. r = row, c = column.
neighbors(2) = img(r-1,c); % Upper middle. r = row, c = column.
neighbors(3) = img(r-1,c+1); % Upper right. r = row, c = column.
neighbors(4) = img(r,c-1); % left. r = row, c = column.
neighbors(5) = img(r,c+1); % right. r = row, c = column.
neighbors(6) = img(r+1,c+1); % Lowerleft. r = row, c = column.
neighbors(7) = img(r+1,c); % lower middle. r = row, c = column.
neighbors(8) = img(r+1,c-1); % Lower left. r = row, c = column.
Order them in whatever order is convenient for you.
  7 comentarios
Image Analyst
Image Analyst el 5 de Feb. de 2022
The only other way I know is
neighbors = img(r-1:r+1, c-1:c+1); % Get 3x3 array.
neighbors(5) = []; % Delete the center pixel itself.
Of course if you just want to process the array somehow, unless you're doing something really unusual, you can just use imfilter(), nlfilter(), or blockproc(). Demoa attached. Using those you don't need to manually scan the image and get the neighbors yourself since it's handled by the function.
DGM
DGM el 5 de Feb. de 2022
You should be able to do it any number of ways. This extracts the pixel and its neighbors:
inpict = randi([0 1],10);
pt = [5 5]; % row, col
imshow(inpict); hold on
plot(pt(2),pt(1),'yx','linewidth',2)
% find the sub-array
s = size(inpict);
idy = max(pt(1)-1,1):min(pt(1)+1,s(1));
idx = max(pt(2)-1,1):min(pt(2)+1,s(2));
nhood = inpict(idy,idx)
nhood = 3×3
0 0 1 0 1 1 1 1 0
Note that the output is truncated if the selected point is on the edge of the image.
It should be asked why this is being done. If this is part of a filter process, then edge handling wouldn't be done inside the loop. If the edge handling is done elsewhere and the neighborhood is guaranteed to be 3x3, then this can be simplified:
% another way
s = size(inpict);
idx = pt.' + [-1 0 1];
nhood = inpict(idx(1,:),idx(2,:))
nhood = 3×3
0 0 1 0 1 1 1 1 0
% or if only the neighbors are needed
nhood = nhood([1:4 6:9])
nhood = 1×8
0 0 1 0 1 1 1 0

Iniciar sesión para comentar.

Más respuestas (1)

Yawar Rehman
Yawar Rehman el 5 de Sept. de 2014
  1 comentario
Aliya Mohammed Salim
Aliya Mohammed Salim el 5 de Sept. de 2014
thanks yawar, but using the sub2ind, gives error, as in 'out of range subscripts', when I give a 2 x 3 matrix as input.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by