MATLAB Answers

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

166 views (last 30 days)
Aliya Mohammed Salim on 5 Sep 2014
Edited: Yohanes Setiawan on 6 Feb 2020
I need to find out the neighbourhood pixel values, given a particular coordinate point in an image. the pixel point is user defined.
##### 6 CommentsShowHide 5 older comments
shikha mangal on 22 Apr 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.

Sign in to comment.

### Accepted Answer

Image Analyst on 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.
##### 4 CommentsShowHide 3 older comments
Yohanes Setiawan on 6 Feb 2020
Hello
Is there any way to automate those neighbors(1) to neighbors(8)?

Sign in to comment.

### More Answers (1)

Yawar Rehman on 5 Sep 2014
##### 1 CommentShowHide None
Aliya Mohammed Salim on 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.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!