6

I'm trying to create a 2D image known as a Gabor patch of variable size. A Gabor patch is best thought of as the convolution of a 2D sine and a 2D gaussian.

Below is the function used to generate the code. The code was ported from this tutorial for Matlab (assume import numpy as np):

def gabor_patch(size, lambda_, theta, sigma, phase, trim=.005):
    """Create a Gabor Patch

    size : int
        Image size (n x n)

    lambda_ : int
        Spatial frequency (px per cycle)

    theta : int or float
        Grating orientation in degrees

    sigma : int or float
        gaussian standard deviation (in pixels)

    phase : float
        0 to 1 inclusive
    """
    # make linear ramp
    X0 = (np.linspace(1, size, size) / size) - .5

    # Set wavelength and phase
    freq = size / float(lambda_)
    phaseRad = phase * 2 * np.pi

    # Make 2D grating
    Xm, Ym = np.meshgrid(X0, X0)

    # Change orientation by adding Xm and Ym together in different proportions
    thetaRad = (theta / 360.) * 2 * np.pi
    Xt = Xm * np.cos(thetaRad)
    Yt = Ym * np.sin(thetaRad)
    grating = np.sin(((Xt + Yt) * freq * 2 * np.pi) + phaseRad)

    # 2D Gaussian distribution
    gauss = np.exp(-((Xm ** 2) + (Ym ** 2)) / (2 * (sigma / float(size)) ** 2))

    # Trim
    gauss[gauss < trim] = 0

    return grating * gauss

I would like the size of the Gabor patch to increase proportionally to the size parameter. In other words, I would like the dimensions of the bounding box to dictate the diameter of the patch. The problem is that this function does not behave in this way. Instead, the bounding box increases in size while the patch retains the same dimensions.

Example 1: size = 100

enter image description here

Example 2: size = 500

enter image description here

It's not at all obvious to me what I'm doing incorrectly. Could somebody please point me in the right direction?

Please let me know if I can provide further information. Thank you!

5
  • 1
    Have you looked at the Gabor filter in scikit-image (scikit-image.org/docs/dev/auto_examples/…)? Commented Sep 26, 2013 at 22:24
  • @WarrenWeckesser, I have not, but I shall! For my own personal edification, however, I'll keep this question open =) Commented Sep 26, 2013 at 22:25
  • @WarrenWeckesser, then again, if you would be so kind as to post an example on how to achieve the above image using scikit-image, I'd be more than happy to accept that as an answer! Commented Sep 26, 2013 at 22:26
  • Personal edification is a good thing. :) Commented Sep 26, 2013 at 22:26
  • @WarrenWeckesser, It's strange, but all the gabor related stuff in skimage is throwing an ImportError. Have you used these functions? Is there something in particular that I need to do? Commented Sep 26, 2013 at 22:53

1 Answer 1

5

Okay, It turns out I was being silly.

The way to achieve what I want is to set the sigma parameter to something bigger, as this will broaden the "spread" of the gaussian, thereby exposing more of the sine texture.

tl;dr: the solution is to increase the sigma

Edification achieved!

1
  • @askewchan, Users are required to wait 48 hours before accepting their own answers. Thanks for the upvote, though! Commented Sep 27, 2013 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.