Find n closest nonzero pixels for every nonzero pixel in an image

Hi,
I have the coordinates of every nonzero pixel within an image (see file: pixel_coords.mat & image: tripod_branchpoints.png). Using this information, I need to find the 10 closest nonzero pixels (indexed with their correponding distances to the central pixel) for every nonzero pixel within the image. Next, of these 10 closest nonzero pixels, I need to identify the 3 nonzero pixels that are within 120 degress (+/- 5 degrees) of the other 2 in this group. I also need to create a plot for these results, where each pixel has connections to these 3 points with distances shown (see attached images: nonzero_pixels.png & nonzero_pixels_example.png). Thanks in advance for your help!

Answers (1)

Using this information, I need to find the 10 closest nonzero pixels (indexed with their correponding distances to the central pixel)
Pretty straightforward using pdist2, if you have the Statistics Toolbox,
[D,I] = pdist2(xy,xy,'squaredeuclidean','Smallest',10)
I don't know what you mean by "the 3 nonzero pixels that are within 120 degress (+/- 5 degrees) of the other 2 in this group", but that is surely something you would compute by looping through the columns of D and I.

4 Comments

Thanks for your input. Regarding your comment about my earlier statement, if you take a look at the images (nonzero_pixels.png & nonzero_pixels_example.png), you will see that there are always three closest neighbors that surround every pixel; these neighbors are also situated approximately 120 degrees apart from each other. Hope this clears things up a bit.
Yes, it is clear, but again, you should just use a for-loop for that part.
Why find the 10 closest first, and then the 3 at 120 degrees? Is it not always the case where the 3 closest area also the ones at 120 degrees? If not, please direct me to a counter example where that is not the case.
Thanks for your input. Actually, no, it is not always the case (see attachment). The image in the attachment shows real pixel locations from another data batch, where the three closest pixels are not 120 degrees from each other, given a central target pixel. However, I'm actually (concurrently) working on another project and data set, where this is the case. I will need help with that one too. Thanks again for your input.

Sign in to comment.

Products

Release

R2018b

Asked:

on 12 Jun 2019

Commented:

on 13 Jun 2019

Community Treasure Hunt

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

Start Hunting!