TheAlgorithms/Python · error · ValueError
invalid k value
Error message
invalid k value
What it means
HarrisCorner.__init__ raises this ValueError when k is not one of exactly two floats: 0.04 or 0.06. The docstring says k is 'an empirically determined constant in [0.04,0.06]' (a range), but the implementation tests membership with `k in (0.04, 0.06)`, so any other value in the documented range — most notably 0.05 — is rejected. This is effectively a bug in the guard, not in the caller.
Source
Thrown at computer_vision/harris_corner.py:21
"""
Harris Corner Detector
https://en.wikipedia.org/wiki/Harris_Corner_Detector
"""
class HarrisCorner:
def __init__(self, k: float, window_size: int):
"""
k : is an empirically determined constant in [0.04,0.06]
window_size : neighbourhoods considered
"""
if k in (0.04, 0.06):
self.k = k
self.window_size = window_size
else:
raise ValueError("invalid k value")
def __str__(self) -> str:
return str(self.k)
def detect(self, img_path: str) -> tuple[cv2.Mat, list[list[int]]]:
"""
Returns the image with corners identified
img_path : path of the image
output : list of the corner positions, image
"""
img = cv2.imread(img_path, 0)
h, w = img.shape
corner_list: list[list[int]] = []
color_img = img.copy()
color_img = cv2.cvtColor(color_img, cv2.COLOR_GRAY2RGB)
dy, dx = np.gradient(img)
ixx = dx**2View on GitHub (pinned to f5988cc097)
Solutions
- Pass exactly 0.04 or 0.06: HarrisCorner(0.04, window_size)
- Or fix the guard to match the documented range: `if 0.04 <= k <= 0.06:` (this is the correct upstream fix)
- If you must keep the strict check, snap your tuned k to the nearest allowed value before constructing
Example fix
# before corner = HarrisCorner(0.05, 3) # ValueError: invalid k value # after (use an accepted literal) corner = HarrisCorner(0.04, 3) # or fix the library guard if 0.04 <= k <= 0.06:
Defensive patterns
Strategy: validation
Validate before calling
K_VALUES = (0.04, 0.06) k = 0.04 if k not in K_VALUES else k hc = HarrisCorner(k, window_size)
Type guard
def is_valid_k(k) -> bool:
return isinstance(k, float) and k in (0.04, 0.06) Try / catch
try:
HarrisCorner(k, window_size)
except ValueError as e:
if 'invalid k value' in str(e):
HarrisCorner(0.04, window_size) # fall back to accepted literal
else:
raise Prevention
- Treat k as an enum of two values, not a continuous range, despite the docstring
- Pin k=0.04 in configs so tuning tools never inject 0.05
- Patch the guard to 0.04 <= k <= 0.06 if you fork the repo
When it happens
Trigger: HarrisCorner(0.05, 3); HarrisCorner(0.041, 5); HarrisCorner(0, 3); any k arrived at by tuning or random search inside [0.04, 0.06] other than the two endpoints.
Common situations: Following tutorials/textbooks that recommend k=0.05; hyperparameter sweeps; reading the docstring's [0.04,0.06] range and picking a midpoint. Only the literal values 0.04 and 0.06 pass.
Related errors
- The input array is not a square matrix
- number must be positive
- The value of input must be non-negative
- Input list must contain at least two elements
- Inputs and select signal must be 0 or 1
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/6b21b71e63d433da.
Report an issue: GitHub.