Clear Filters
Clear Filters

How to find neighbour pixels of a central pixel in a binary image?

4 views (last 30 days)
I am working on a binary image so how to find its neighbours. I have successfully found neighbour pixels in gray scale image but after its conversion from gray to binary its not giving me the pixels of binary image, its still showing me the pixels of gray scale image. Can anyone provide help on this?
Thanks in advance.
  2 Comments
tauseef ahmad
tauseef ahmad on 8 Feb 2017
hi, Ekta sharma, how you found neighbour pixel in gray image? can you send me the code at tauseefmmd@gmail.com
kmla
kmla on 6 Apr 2018
Hi Ekta Sharma, are you find the code to find neighbors pixels of a central pixel in a binary image? can you send it to me houdakhmila@gmail.com

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jan 2016
What do you need to do with the neighbours? Do you specifically need to know their indices? If so then in what order?
If you are doing a calculation such as region finding then you would normally use regionprops() or one of the image morphological operators.
If you are doing a calculation such as mean you would normally use conv2()
If you are doing a calculation such as local min or local max then one of the morphological operators can handle that.
If you are doing more complex arithmetic calculations on a block centered around the pixel then nlfilt()
  3 Comments
Walter Roberson
Walter Roberson on 8 Feb 2017
To find the neighbouring pixels for each non-zero pixel in a grayscale image:
[nr, nc] = size(YourImage);
[r, c] = find(YourImage);
aboves = [r - 1, c];
aboves(r == 1, :) = [];
belows = [r + 1, c];
belows(c == nr, :) = [];
lefts = [r, c - 1];
lefts(c == 1, :) = [];
rights = [r, c + 1];
rights(c == nc, :) = [];
aboveleft = [r - 1, c - 1];
aboveleft(r == 1 | c == 1, :) = [];
aboveright = [r, c + 1];
aboveright(r == 1 | c == nc, :) = [];
belowleft = [r + 1, c - 1];
belowleft(r == nr | c == 1, :) = [];
belowright = [r + 1, c + 1];
belowright(r == nr | c == nc, :) = [];
neighbors = [aboveright; lefts; belows; aboveleft; belowright; aboves; rights; belowleft];
Now, neighbors will be two columns, each row of which is the row and column number of a pixel that is a neighbor of a non-zero pixel in the grayscale image. Pixels might occur up to 8 times in the list, due to being neighbors of different non-zero pixels.
Good luck doing anything useful with this list.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!