It is a bit difficult to reason about the computation without seeing some concrete shapes, but I think the general pattern might be amenable to vectorization as I don’t see any obvious tricky control flow.
Is where being called on just a single pixel? I think it would likely be faster to precompute both branches first and then select them later.
I think this can be done if the weight function can be vectorized across pixels (just make sure that all of the inputs have the same shape or can be broadcast in a way that makes sense).