4

I'm working on image related algorithms and was wondering what some good choices would be for unpredictable 2D traversal of structures as such:

  • Start from a set pixel
  • Traverse to a neighbor (up left down right or diagonal, always 1 away, no jumps).
  • Keep doing more of those traversals to the next pixel with the next direction depending on some output (similar rate of each direction over time).

For simple traversal then arrays are just fine and very fast when reading sequentially, for random access not much can be done but i'm wondering if here Something could be done, if simply stored in an array going "up" could actually be pretty far in memory on large is (5+megapixel). Can you think of any good structures for this? I'm more than happy to trade memory for speed here but i can't think of any structure that could help short of actually making an array of items that each store a pointer to their 8 neighbors, but that sounds like it would be slow to create to begin with and i'm not making more than 1 to 3 passes on those images so it may end up being slower than the naive array implementation.

Any suggestions are welcome.

Sachin
  • 359
  • 2
  • 18
Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78

1 Answers1

4

Space-filling curves are what you are looking for. They provide cache-friendly data layout in memory for neighbor-related operations.

However, be aware of premature optimization. Before doing any kind of optimizations, make sure you have a working algorithm. Then you can start optimizing it, taking benchmarks and comparing them with the base non-optimized version.

torvin
  • 6,515
  • 1
  • 37
  • 52
  • It's far from premature as i'm doing very little work per pixel most of the time is already spent waiting on data reads. I'll look your duggestion up to see if it sounds like it could work – Ronan Thibaudau Sep 10 '15 at 05:19
  • Sorry, just wanted to make sure you know what you are doing :) – torvin Sep 10 '15 at 05:20
  • Related: http://stackoverflow.com/questions/27891563/2d-cache-friendly-data-structures-and-space-filling-curves – torvin Sep 10 '15 at 05:23
  • Related: http://stackoverflow.com/questions/499166/mapping-n-dimensional-value-to-a-point-on-hilbert-curve – torvin Sep 10 '15 at 05:36
  • This sounds exactly lile what i need after some reading, thanks! – Ronan Thibaudau Sep 10 '15 at 05:40