{"record":{"id":"3ad3df7dc2cc8464","repo":"TheAlgorithms/Python","slug":"ksize-must-be-in-tuple-kernels","errorCode":null,"errorMessage":"ksize must be in {tuple(kernels)}","messagePattern":"ksize must be in (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"digital_image_processing/filters/laplacian_filter.py","lineNumber":57,"sourceCode":"                [0, -1, -2, -1, 0],\n                [0, 0, -1, 0, 0],\n            ]\n        ),\n        7: np.array(\n            [\n                [0, 0, 0, -1, 0, 0, 0],\n                [0, 0, -2, -3, -2, 0, 0],\n                [0, -2, -7, -10, -7, -2, 0],\n                [-1, -3, -10, 68, -10, -3, -1],\n                [0, -2, -7, -10, -7, -2, 0],\n                [0, 0, -2, -3, -2, 0, 0],\n                [0, 0, 0, -1, 0, 0, 0],\n            ]\n        ),\n    }\n    if ksize not in kernels:\n        msg = f\"ksize must be in {tuple(kernels)}\"\n        raise ValueError(msg)\n\n    # Apply the Laplacian kernel using convolution\n    return filter2D(\n        src, CV_64F, kernels[ksize], 0, borderType=BORDER_DEFAULT, anchor=(0, 0)\n    )\n\n\nif __name__ == \"__main__\":\n    # read original image\n    img = imread(r\"../image_data/lena.jpg\")\n\n    # turn image in gray scale value\n    gray = cvtColor(img, COLOR_BGR2GRAY)\n\n    # Applying gaussian filter\n    blur_image = gaussian_filter(gray, 3, sigma=1)\n\n    # Apply multiple Kernel to detect edges","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/digital_image_processing/filters/laplacian_filter.py#L39-L75","documentation":"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.","triggerScenarios":"laplacian_filter(img, 2), laplacian_filter(img, 9), or passing cv2.LAPLACIAN_64F-style flags/enum values instead of a plain int size.","commonSituations":"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.","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"],"exampleFix":"// before\nout = laplacian_filter(img, ksize=user_ksize)  # user_ksize=9 -> ValueError\n\n# after\nksize = user_ksize if user_ksize in (1, 3, 5, 7) else 3\nout = laplacian_filter(img, ksize)","handlingStrategy":"type-guard","validationCode":"ksize = ksize if ksize in (1, 3, 5, 7) else 3\nout = laplacian_filter(img, ksize)","typeGuard":"def is_supported_ksize(ksize: int) -> bool:\n    return isinstance(ksize, int) and ksize in (1, 3, 5, 7)","tryCatchPattern":"try:\n    out = laplacian_filter(img, ksize)\nexcept ValueError:\n    out = laplacian_filter(img, 3)","preventionTips":["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"],"tags":["image-processing","opencv","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}