# Finding neighboring pixels surrounding a region/patch

4 views (last 30 days)
Muzammil Behzad on 17 Jan 2017
Commented: Image Analyst on 12 Feb 2017
Hello Dear Community.
After trying and searching a lot, I ultimately have to put the question here. I need to find the neighbor pixels. For example, I have a random 3x3 neighborhood/patch/region and I want to find the outer pixels (pixels right next to the patch) that surrounds this patch. Once, I find all these outer pixels (which will be 16 pixels in this case), I'll choose one among these 16 pixels. Now, I have another a random patch (not squared patch as it has 3x3 + 1 pixels). Then, I want to apply the same steps i.e., find the outer pixels surrounding this non-squared region and choose one pixel among them and make another random non-squared patch, and so on... It's a kind of region/pixel growing but not exactly region growing. Can anyone help on this? Many thanks.
Image Analyst on 17 Jan 2017
I don't understand " Now, I have another a random patch (not squared patch as it has 3x3 + 1 pixels)" Exactly how many pixels wide and tall is it? And where is its location with respect to the first random region? And what help do you want? Is this a "Do it all for me" situation? You know how to extract a n-by-m submatrix from a larger matrix using indexing, and how to use randi(), don't you?
Muzammil Behzad on 17 Jan 2017
This is not a 'do it all for me' situation at all. Just for the simplicity, I have explained the scenario that I want to acquire. Yes I know the indexing method.
In simple words, if I have any random shaped patch with lets say 10 pixels, how do I get the locations of the pixels surrounded by that random-shaped patch? I am interested in finding the location of the neighboring pixels that make a boundary around the reference region.

Image Analyst on 17 Jan 2017
" I have any random shaped patch with lets say 10 pixels, how do I get the locations of the pixels surrounded by that random-shaped patch?"
Use imdilate() to dilate out that patch by one layer of pixels, then use bwperim to get the values. Use it as a mask if you don't care what order they are in. Otherwise you'd have to specify a starting pixel and direction (clockwise or counter clockwise). Briefly, to get you started, if originalImage is the image of values and binaryImage is an image where it's true where your initial patch is and false where the patch isn't, then ...
dilatedImage = imdilate(binaryImage, true(3));
borderPixels = bwperim(dilatedImage);
pixelValues = originalImage(borderPixels);
Piyum Rangana on 12 Feb 2017
Could you please explain 'originalImage()' function, that was used in the last row of above answer that it is an inbuilt function or not ?
Image Analyst on 12 Feb 2017
originalImage is a matrix variable. It's the name of your original image. I'm sure you didn't call it "originalImage" like I did, so what did you call it? Hopefully not I or A or some other cryptic, non-descriptive name. Just substitute your actual variable name for originalImage.

### Categories

Find more on Image Arithmetic in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!