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

chain codeneighbour pixel

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.

Best Answer

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()