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

63 vues (au cours des 30 derniers jours)
I need to find out the neighbourhood pixel values, given a particular coordinate point in an image. the pixel point is user defined.
  6 commentaires
Image Analyst
Image Analyst le 22 Avr 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 le 22 Avr 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.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 5 Sep 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 commentaires
Image Analyst
Image Analyst le 5 Fév 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 le 5 Fév 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

Connectez-vous pour commenter.

Plus de réponses (1)

Yawar Rehman
Yawar Rehman le 5 Sep 2014
  1 commentaire
Aliya Mohammed Salim
Aliya Mohammed Salim le 5 Sep 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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by