Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gabor crashes with ZeroDivisionError when bandwidth=0 #8301

Description

@ravikumar266

What's happening

The gabor and gabor_kernel functions crash with a
ZeroDivisionError when bandwidth=0 is passed, instead of a clear
error message. The docstring doesn't say anywhere that bandwidth
has to be a positive number.

How to reproduce it

import numpy as np
from skimage.filters import gabor

image = np.zeros((10, 10))
gabor(image, frequency=0.1, bandwidth=0)

What actually happens

Why it fails

  • Internally, gabor calls gabor_kernel, which calls a helper
    function _sigma_prefactor(bandwidth)
  • That helper computes:
    1.0 / np.pi * sqrt(log(2) / 2.0) * (2.0**b + 1) / (2.0**b - 1)
  • When bandwidth is 0, 2.0**0 equals 1, which makes the bottom
    part of that formula, (2.0**b - 1), come out to exactly 0
  • This isn't a rare floating-point rounding issue — it's a guaranteed,
    exact division by zero for this one input
  • Nowhere in gabor, gabor_kernel, or _sigma_prefactor is there
    any check that stops bandwidth=0 from reaching this line

Evidence this is a real, reproducible bug

  • Reproduced it myself with the minimal script above — the traceback
    clearly shows the crash happening through gaborgabor_kernel
    _sigma_prefactor
  • None of the existing doctest examples use bandwidth=0 — they only
    use the default (1) or 0.1
  • None of the existing tests cover it either — the test for bandwidth
    only checks 1 and 0.5
  • The docstring for the bandwidth parameter never mentions that it
    needs to be strictly positive

Suggested fix

Add a simple validation check near the start of gabor_kernel (which
gabor also goes through), something like:

if bandwidth <= 0:
    raise ValueError('bandwidth must be > 0')

Version info

scikit-image version: 0.25.2
numpy version: 2.1.3
Platform: Linux-6.6.122+-x86_64-with-glibc2.35

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions