Realtime Distance Estimation Using Python OpenCV

Python and OpenCV have revolutionized the field of computer vision, enabling developers to build powerful real-time applications. Distance estimation is a crucial technique used in robotics, augmented reality, and autonomous systems.

In this article, we will explore how to implement real-time distance estimation using Python and OpenCV. We'll cover camera setup, object detection, and triangulation methods to calculate distances accurately.

Installation and Setup

Before implementing distance estimation, we need to install the required libraries ?

pip install opencv-python
pip install numpy
pip install matplotlib

These libraries provide computer vision functions, numerical computations, and visualization capabilities respectively.

Capturing Real-time Video Feed

The first step is accessing the camera feed using OpenCV's VideoCapture class ?

import cv2

# Create a VideoCapture object
cap = cv2.VideoCapture(0)

# Check if camera opened successfully
if not cap.isOpened():
    print("Failed to open camera")
    exit()

# Read and display frames
while True:
    ret, frame = cap.read()
    if not ret:
        print("Failed to read frame")
        break

    # Display the frame
    cv2.imshow("Camera Feed", frame)

    # Break loop if 'q' is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release resources
cap.release()
cv2.destroyAllWindows()

Object Detection Using Haar Cascades

For distance estimation, we need to detect objects first. OpenCV provides pre-trained Haar cascade classifiers for face detection ?

import cv2

# Load pre-trained face cascade classifier
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Access camera feed
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    # Convert to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detect faces
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

    # Draw rectangles around detected faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        cv2.putText(frame, "Face Detected", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)

    cv2.imshow("Face Detection", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Simple Distance Estimation Using Object Size

A practical approach for distance estimation uses the relationship between object size in pixels and its actual distance from the camera ?

import cv2
import math

# Known face width in centimeters (average human face width)
KNOWN_FACE_WIDTH = 14.0

# Focal length (needs calibration for your camera)
FOCAL_LENGTH = 615

def calculate_distance(face_width_pixels):
    """Calculate distance using focal length and known object size"""
    if face_width_pixels == 0:
        return 0
    return (KNOWN_FACE_WIDTH * FOCAL_LENGTH) / face_width_pixels

# Load face cascade
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

    for (x, y, w, h) in faces:
        # Calculate distance based on face width
        distance = calculate_distance(w)
        
        # Draw bounding box and distance
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        cv2.putText(frame, f"Distance: {distance:.1f} cm", (x, y-10), 
                   cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)

    cv2.imshow("Distance Estimation", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Camera Calibration for Better Accuracy

For more accurate distance estimation, camera calibration is essential. This process determines the camera's intrinsic parameters ?

import cv2
import numpy as np

# Camera calibration using chessboard pattern
def calibrate_camera():
    # Chessboard dimensions
    CHECKERBOARD = (7, 6)
    
    # Termination criteria for corner detection
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
    
    # Prepare object points
    objp = np.zeros((CHECKERBOARD[0] * CHECKERBOARD[1], 3), np.float32)
    objp[:, :2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
    
    # Arrays to store object points and image points
    objpoints = []  # 3D points in real world space
    imgpoints = []  # 2D points in image plane
    
    cap = cv2.VideoCapture(0)
    
    while len(objpoints) < 10:  # Collect 10 calibration images
        ret, frame = cap.read()
        if not ret:
            continue
            
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        
        # Find chessboard corners
        ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, None)
        
        if ret:
            objpoints.append(objp)
            corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
            imgpoints.append(corners2)
            
            # Draw corners on the image
            cv2.drawChessboardCorners(frame, CHECKERBOARD, corners2, ret)
        
        cv2.imshow('Calibration', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    cap.release()
    cv2.destroyAllWindows()
    
    # Calibrate camera
    ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(
        objpoints, imgpoints, gray.shape[::-1], None, None)
    
    return camera_matrix, dist_coeffs

# Example usage (requires chessboard pattern)
# camera_matrix, dist_coeffs = calibrate_camera()

Distance Estimation Methods Comparison

Method Accuracy Setup Required Best For
Object Size Moderate Known object dimensions Single object tracking
Stereo Triangulation High Two calibrated cameras Precise measurements
Depth Sensors Very High Specialized hardware Professional applications

Conclusion

Real-time distance estimation using Python and OpenCV combines object detection with geometric calculations. The object size method provides a practical solution for single-camera setups, while stereo vision offers higher accuracy for precise applications.

Updated on: 2026-03-27T10:02:44+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements