This program loads a chessboard image, converts it to grayscale, and applies the Harris corner detector. It marks detected corners in red and saves the result. It also prints the maximum corner response value and how many corners were detected above the threshold.
import cv2
import numpy as np
# Load image and convert to grayscale
img = cv2.imread('chessboard.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
# Detect corners using Harris
block_size = 2
ksize = 3
k = 0.04
corner_response = cv2.cornerHarris(gray, block_size, ksize, k)
# Dilate corner points to mark them
corner_response = cv2.dilate(corner_response, None)
# Threshold to mark strong corners in red
img[corner_response > 0.01 * corner_response.max()] = [0, 0, 255]
# Save result image
cv2.imwrite('chessboard_corners.png', img)
# Print some stats
print(f"Max corner response: {corner_response.max():.2f}")
print(f"Number of corners detected: {(corner_response > 0.01 * corner_response.max()).sum()}")