# Search in a circle method for closest pixel

This topic is 3433 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm working on a edge expansion filter. Now it searches for closest pixel found from a given X Y point in a square type pattern. This square search method results in weird results on some places. It's all correct when you think about how it searches but it doesnt look good in some places. What I would like to do is to search in a circle instead. So I get rid of the straight lines. Any ideas how do I create a function that searches pixels in a circle?

##### Share on other sites
Does it need to be fast?
I don't know if I understand correctly, but do you have a grid, starting at some point, and want to search for the pixel closest to the origin point that matches some criteria?
Keep a list of the next pixels to search, sorted by distance to the origin pixel. You start with the four pixels surrounding your origin pixel. Then when you search you take the first pixel in the list out, and if it's not the one you're looking for, add all its neighbors to the list. Keep it sorted by distance to the origin, and keep going, always with the first pixel in the list (the closest one to the origin), until you find the one you're looking for.

##### Share on other sites

Any ideas how to get the distance in pixels when it's non linear?

##### Share on other sites
The squared distance could be expressed as d2 = dx2 + dy2. It should be sufficient to sort by the squared distance (in order to avoid the sqrt() ).

##### Share on other sites
You could keep the distance stored at each pixel, and when adding neighbors you add the distance between the active pixel and the relevant neighbor to the stored distance, and use as the distance for the new pixel.

##### Share on other sites
Doh :) my brain was all wrapped up in "pixels"
Thanks

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013599
×