Gabor Kernel

The parentheses in your gaussian were misplaced. The x y terms are in the exponential, which is not what you wrote. In figuring this out, I also went ahead and wrote this in a more “julian” style, instead of the more pythonic (vectorized) approach.

function gen_gabor(sz, ω, θ)
    r = (sz .÷ 2)
    ga = zeros(sz)
    si = zeros(sz)
    gb = zeros(sz)

    f(x, y) = ω^2 / (4pi^3) * exp(-ω^2/(8pi^2) * (4*x^2 + y^2))
 
    for I in CartesianIndices(sz)
        x̃, ỹ = Tuple(I) .- r
        s, c = sincos(θ)
        x, y = [c s; -s c] * [x̃, ỹ]

        ga[I] = f(x, y)
        si[I] = cos(ω*x) * exp((pi^2)/2)
        gb[I] = ga[I]*si[I]
    end
    return ga, si, gb
end

Note that if not for the purposes of the exercise, ga and si would not be necessary outputs of the function, and in that case should not be allocated at all.

1 Like