TheAlgorithms/Python · error · ValueError
ksize must be in {tuple(kernels)}
Error message
ksize must be in {tuple(kernels)} What it means
Raised by the laplacian_filter function in digital_image_processing/filters/laplacian_filter.py:57 when `ksize` is not one of the keys of the kernels dict — this implementation provides pre-built Laplacian kernels of size 1, 3, 5, and 7 only. The chosen kernel is then applied with cv2.filter2D using CV_64F depth, so even-sized or other odd sizes were simply not authored.
Source
Thrown at digital_image_processing/filters/laplacian_filter.py:57
[0, -1, -2, -1, 0],
[0, 0, -1, 0, 0],
]
),
7: np.array(
[
[0, 0, 0, -1, 0, 0, 0],
[0, 0, -2, -3, -2, 0, 0],
[0, -2, -7, -10, -7, -2, 0],
[-1, -3, -10, 68, -10, -3, -1],
[0, -2, -7, -10, -7, -2, 0],
[0, 0, -2, -3, -2, 0, 0],
[0, 0, 0, -1, 0, 0, 0],
]
),
}
if ksize not in kernels:
msg = f"ksize must be in {tuple(kernels)}"
raise ValueError(msg)
# Apply the Laplacian kernel using convolution
return filter2D(
src, CV_64F, kernels[ksize], 0, borderType=BORDER_DEFAULT, anchor=(0, 0)
)
if __name__ == "__main__":
# read original image
img = imread(r"../image_data/lena.jpg")
# turn image in gray scale value
gray = cvtColor(img, COLOR_BGR2GRAY)
# Applying gaussian filter
blur_image = gaussian_filter(gray, 3, sigma=1)
# Apply multiple Kernel to detect edgesView on GitHub (pinned to f5988cc097)
Solutions
- Restrict ksize to (1, 3, 5, 7) — mirror the tuple from the error message in your UI/config validation
- Default to ksize=3 (or 1) when the user's value is unsupported
- Catch ValueError at the config boundary and re-ask for a valid size
Example fix
// before out = laplacian_filter(img, ksize=user_ksize) # user_ksize=9 -> ValueError # after ksize = user_ksize if user_ksize in (1, 3, 5, 7) else 3 out = laplacian_filter(img, ksize)
Defensive patterns
Strategy: type-guard
Validate before calling
ksize = ksize if ksize in (1, 3, 5, 7) else 3 out = laplacian_filter(img, ksize)
Type guard
def is_supported_ksize(ksize: int) -> bool:
return isinstance(ksize, int) and ksize in (1, 3, 5, 7) Try / catch
try:
out = laplacian_filter(img, ksize)
except ValueError:
out = laplacian_filter(img, 3) Prevention
- This wrapper only ships kernels 1/3/5/7 — do not assume cv2.Laplacian's full ksize semantics
- Whitelist ksize in any config schema before it reaches the call
When it happens
Trigger: laplacian_filter(img, 2), laplacian_filter(img, 9), or passing cv2.LAPLACIAN_64F-style flags/enum values instead of a plain int size.
Common situations: Coming from cv2.Laplacian where ksize semantics differ, exposing ksize as a user config knob without a whitelist, or copy-pasting a ksize=9 from another pipeline.
Related errors
- level must be between -255.0 (black) and 255.0 (white)
- Factor value should be from 0 to {self.max_threshold}
- Destination width/height should be > 0
- number must be positive
- The value of input must be non-negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/3ad3df7dc2cc8464.
Report an issue: GitHub.