rafaqz / DimensionalData.jl

Named dimensions and indexing for julia arrays and other data
https://rafaqz.github.io/DimensionalData.jl/stable/
MIT License
262 stars 38 forks source link

Selection of multiple surrounding pixels around a position #737

Open felixcremer opened 2 weeks ago

felixcremer commented 2 weeks ago

With Near I can select the nearest pixel at a given position. Is there an easy way to select multiple pixels surrounding this position. This is what I came up with:

using DimensionalData
julia> dd = DimArray(rand(6,5), (X(1:6), Y(4:8)))
julia> ypos=6
        ywidth=2
         xpos=3
         xwidth=1
1

julia> ddsub = dd[Y=ypos..(ypos+ywidth*step(dims(dd,Y))), X=xpos..(xpos+xwidth*step(dims(dd,X)))]
kw = Base.Pairs{Symbol, IntervalSets.ClosedInterval{Int64}, Tuple{Symbol, Symbol}, @NamedTuple{Y::IntervalSets.ClosedInterval{Int64}, X::IntervalSets.ClosedInterval{Int64}}}(:Y => 6 .. 8, :X => 3 .. 4)
d1 = Y{IntervalSets.ClosedInterval{Int64}}(6 .. 8)
ds = (X{IntervalSets.ClosedInterval{Int64}}(3 .. 4),)
╭─────────────────────────╮
│ 2×3 DimArray{Float64,2} │
├─────────────────────────┴─────────────────────── dims ┐
  ↓ X Sampled{Int64} 3:4 ForwardOrdered Regular Points,
  → Y Sampled{Int64} 6:8 ForwardOrdered Regular Points
└───────────────────────────────────────────────────────┘
 ↓ →  6         7         8
 3    0.127596  0.255981  0.347761
 4    0.542776  0.674099  0.19914

This is a bit annoying to writeout and also one has to make sure, that the direction is correct when the stepsize is negative.

rafaqz commented 2 weeks ago

Do I have a PR for you... https://github.com/rafaqz/DimensionalData.jl/pull/717

The idea is to allow Near and At to accept a Tuple{A,B} and return a range in that case. I think that branch should already do what you want with Near(ypos1, ypos2). Although you wont always get the same size range back...

felixcremer commented 2 weeks ago

Nice. I will try to test that PR.

rafaqz commented 2 weeks ago

It's possible it will break on reverse lookups 😅