Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find patterns in a chessboard using OpenCV Python?
Finding chessboard patterns is essential for camera calibration and computer vision applications. OpenCV provides two key functions: cv2.findChessboardCorners() to detect corner points and cv2.drawChessboardCorners() to visualize the detected pattern.
Syntax
ret, corners = cv2.findChessboardCorners(img, patternSize, None) cv2.drawChessboardCorners(img, patternSize, corners, ret)
Parameters
img ? Input grayscale image
patternSize ? Number of inner corners per row and column (width, height)
corners ? Output array of detected corner coordinates
ret ? Boolean indicating if pattern was found
Steps
To find chessboard patterns, follow these steps ?
Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you have already installed it.
Read the input image of a chessboard using cv2.imread() and convert it to grayscale using cv2.cvtColor().
Find the chessboard corners in a chessboard image using cv2.findChessboardCorners(). It returns two output parameters ret and corners. The ret is True if the chessboard corners are detected in the image. The corners are the coordinates of the detected chessboard corners in (x,y) format.
Draw the chessboard corners and pattern on the original input image using cv2.drawChessboardCorners()
Display the image with the drawn chessboard pattern.
Basic Chessboard Detection
In this example, we detect and draw chessboard corners ?
import cv2
import numpy as np
# Create a sample chessboard pattern for demonstration
def create_chessboard():
board = np.zeros((480, 640), dtype=np.uint8)
square_size = 60
for i in range(8):
for j in range(9):
if (i + j) % 2 == 0:
y1, y2 = i * square_size, (i + 1) * square_size
x1, x2 = j * square_size, (j + 1) * square_size
board[y1:y2, x1:x2] = 255
return board
# Create chessboard image
img = create_chessboard()
img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
# Find the chessboard corners (7x6 inner corners)
ret, corners = cv2.findChessboardCorners(img, (7, 6), None)
# If chessboard corners are detected
if ret:
# Draw and display the corners
img_color = cv2.drawChessboardCorners(img_color, (7, 6), corners, ret)
print("Chessboard pattern detected successfully!")
print(f"Number of corners found: {len(corners)}")
else:
print("Chessboard pattern not found")
Enhanced Detection with Sub-pixel Accuracy
This method refines corner positions using cv2.cornerSubPix() for better accuracy ?
import cv2
import numpy as np
# Create a sample chessboard pattern
def create_chessboard():
board = np.zeros((480, 640), dtype=np.uint8)
square_size = 60
for i in range(8):
for j in range(9):
if (i + j) % 2 == 0:
y1, y2 = i * square_size, (i + 1) * square_size
x1, x2 = j * square_size, (j + 1) * square_size
board[y1:y2, x1:x2] = 255
return board
# Create chessboard image
gray = create_chessboard()
img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (7, 6), None)
# Termination criteria for corner sub-pixel accuracy
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
if ret:
# Refine corner positions to sub-pixel accuracy
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
# Draw and display the refined corners
img = cv2.drawChessboardCorners(img, (7, 6), corners2, ret)
print("Enhanced chessboard detection completed!")
print(f"Corner coordinates refined with sub-pixel accuracy")
else:
print("Chessboard pattern not detected")
Key Points
The pattern size (7,6) refers to inner corners, not squares
Input image should be grayscale for corner detection
cv2.cornerSubPix() improves corner accuracy for calibration
Good lighting and image quality improve detection success
Conclusion
OpenCV's chessboard detection is crucial for camera calibration. Use cv2.findChessboardCorners() for basic detection and cv2.cornerSubPix() for enhanced accuracy in calibration applications.
