fbpx
Sigmoidal
  • Home
  • LinkedIn
  • About me
  • Contact
No Result
View All Result
  • Português
  • Home
  • LinkedIn
  • About me
  • Contact
No Result
View All Result
Sigmoidal
No Result
View All Result

Learn Camera Calibration using OpenCV

Carlos Melo by Carlos Melo
September 12, 2023
in Computer Vision
0
103
SHARES
3.4k
VIEWS
Share on LinkedInShare on FacebookShare on Whatsapp

The eyes of the surgical team are glued to the screens, where each movement of the surgical robot is magnified and meticulously analyzed. The scalpel glides smoothly over the patient’s skin, cutting through layers and layers of tissue. The sound is sharp and piercing, echoing through the sterile air of the operating room. The doctors watch the scene closely, their hands sweating coldly as the surgical robot performs its precise movements.

The operating room is a daunting place, illuminated only by the blue light from the monitors reflecting the camera’s image, where life and death walk side by side. After all, even with the best-prepared team, there’s always a possibility of failure. The equipment used in the surgery can have technical issues, and the algorithms usually employed are often unknown.

In this high-pressure environment, one mistake can be fatal. That’s why camera calibration is so crucial.

But have you ever stopped to think about how a lack of calibration can be critical in situations where precision is paramount, like in a robotic surgery or a military operation? Imagine a robotic surgery where a millimeter error could mean the death of the patient. Or consider launching a missile thousands of kilometers away at a military target, with a preschool right next door. Lens distortion and imperfect camera calibration can lead to fatal errors in situations like these.

In the field of computer vision research, camera calibration techniques are of high theoretical and practical value. In this post, you’ll learn the basic concepts of camera calibration and how to use them with OpenCV and Python.

Basics of Camera Calibration

Lens distortion is a common phenomenon in image capture and occurs when a lens cannot accurately reproduce the perspective of a scene in the captured image.

This issue can create shape and perspective distortions in the image, which can be especially problematic in applications involving computer vision and augmented reality. Among the various distortions, the two main ones are radial distortion and tangential distortion.

Radial distortion happens when light rays that pass through the edge of the lens are deflected at different angles than those passing through the lens’s center. This can result in curved or distorted lines at the image’s edges.

Example of radial distortion. Source: Analytics Vidhya

Tangential distortion occurs when the lens isn’t perfectly aligned with the camera’s sensor, leading to straight lines appearing curved or tilted. It’s that feeling that the motorcycle in the rearview mirror seemed farther or even closer than it actually was.

Example of tangential distortion. Source: Analytics Vidhya

Fortunately, lens distortion can be corrected through camera calibration, which involves measuring lens distortion and correcting the image data based on these measurements. This process is especially crucial in applications demanding high precision, like computer vision in autonomous cars and security monitoring systems.

Why does distortion happen?

It’s important to note that lens distortion isn’t exclusively a problem for low-quality or cheap cameras. Even high-quality lenses can suffer from distortion if not calibrated correctly.

To understand, consider this simple example of a camera model known as a pinhole (pinhole camera model). When a camera focuses on an object, it sees it in a manner similar to our eyes, concentrating the light reflected in the real world. Through a small aperture, the camera concentrates the light reflected from the 3D object onto a plane at the back of the camera.

Pinhole camera model.

The camera matrix, mapping a 3D scene onto a 2D image plane, is represented by a 3×4 matrix, also called a projection matrix. This matrix is composed of the multiplication of the intrinsic matrix by the extrinsic matrix. The intrinsic matrix, representing the camera’s internal parameters, is given by:

    \[ K=\begin{bmatrix} f_x & 0 & c_x \\ 0 & f_y & c_y \\ 0 & 0 & 1 \\ \end{bmatrix} \]

Where f_x and f_y are the focal lengths in pixels in the x and y directions, respectively. c_x and c_y are the coordinates of the main point, or optical center, of the camera in the image in pixels. The last column of the matrix is used to calculate the projection of 3D points on the image, but is not necessary for camera calibration.

The extrinsic matrix, which represents the position and orientation of the camera in 3D space, is given by:

    \[ \begin{bmatrix} u \\ v \\ w \\ \end{bmatrix} = K \cdot [R | t] \cdot \begin{bmatrix} X \\ Y \\ Z \\ 1 \\ \end{bmatrix} \]

where u, v, and w are the homogeneous coordinates in the 2D image. To obtain the image coordinates (x, y), just divide u and v by w:

    \[ x = \frac{u}{w},\ y = \frac{v}{w} \]

Therefore, camera calibration is a crucial step in any computer vision application, which can have a significant impact on the accuracy and reliability of the results obtained.

By recognizing the importance of camera calibration, developers can ensure that their computer vision-based solutions are reliable and effective, regardless of the environment or image capture conditions.

Camera Calibration Implementation with OpenCV and Python

Now that we understand the importance of camera calibration and the basics behind it, let’s discuss the practical implementation using OpenCV and Python. OpenCV is a widely used computer vision library and has built-in functions to perform camera calibration efficiently.

Environment Setup

First, install OpenCV in your Python environment, if you haven’t already. You can install OpenCV using the following command:

pip install opencv-python

Image Collection

Before we start implementing camera calibration, we need to collect a set of images of a known pattern, like a chessboard. Images should be taken at different angles and positions to ensure accurate calibration. In this example, we will use a set of images of a 7×6 chessboard.

Exemplos de imagens que serão usadas para calibrar a câmera.

With the set of chessboard images, we can proceed to the detection of the board’s corners and the camera calibration. The following code demonstrates how to do this:

import cv2
import numpy as np
import glob

# Chessboard parameters
chessboard_size = (7, 6)  # Number of inner corners on the board (width x height)

# Prepare 3D object points
objp = np.zeros((np.prod(chessboard_size), 3), dtype=np.float32)
objp[:, :2] = np.indices(chessboard_size).T.reshape(-1, 2)

# Lists to store 3D object points and 2D image points
object_points = []
image_points = []

list_of_image_files = glob.glob('./data/chessboard/*.jpg')

# Load and process each image
for image_file in list_of_image_files:
    image = cv2.imread(image_file)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Detect chessboard corners
    ret, corners = cv2.findChessboardCorners(gray, chessboard_size, None)

    # If corners are found, add object and image points
    if ret:
        object_points.append(objp)
        image_points.append(corners)

        # Draw and display the corners
        cv2.drawChessboardCorners(image, chessboard_size, corners, ret)
        cv2.imshow('img', image)
        cv2.waitKey(500)

cv2.destroyAllWindows()

# Calibrate the camera
ret, K, dist, rvecs, tvecs = cv2.calibrateCamera(
    object_points, image_points, gray.shape[::-1], None, None
)

print("Calibration matrix K:\n", K)
print("Distortion:", dist.ravel())

In this code, we first define the chessboard size and prepare the 3D object points. Next, we load the chessboard images and convert each image to grayscale. We detect the chessboard corners using the cv2.findChessboardCorners function and store the corresponding object and image points in the object_points and image_points lists.

If the corners are successfully found, we draw the corners on the image using the cv2.drawChessboardCorners function and display the image for a short period of time.

Detecting chessboard corners

After processing all the images, we proceed to calibrate the camera using the cv2.calibrateCamera function, which takes the 3D object points, the 2D image points, and the grayscale image size as arguments. The function returns the calibration matrix K, the distortion coefficients, and the rotation and translation matrices for each image.

In the end, we print the calibration matrix K and the distortion coefficients.

Examples of images with and without distortion.

Now that we have the calibration matrix K and the distortion coefficients, we can correct the distortion in the images using the cv2.undistort function. Here’s an example of how to do it:

# Load a test image
test_image = cv2.imread('./data/test_image.jpg')

# Correct the image distortion
undistorted_image = cv2.undistort(test_image, K, dist, None, K)

# Display the original and corrected image side by side
combined_image = np.hstack((test_image, undistorted_image))
cv2.imshow('Original vs Undistorted', combined_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Conclusion

Camera calibration is a crucial aspect of computer vision, especially in applications that require high precision, such as robotic surgery and security monitoring systems. This article explored the fundamentals of lens distortion and the importance of camera calibration, as well as providing a practical implementation using Python and the OpenCV library.

By understanding the basic concepts and applying camera calibration techniques in your computer vision solutions, developers can ensure more reliable and effective results, regardless of image capture conditions.

Furthermore, computer vision is a constantly growing field, offering numerous opportunities for those wishing to enhance their skills and knowledge in this discipline. With the demand for computer vision specialists on the rise, there are plenty of opportunities abroad for qualified professionals. Seizing these opportunities can open doors to a rewarding and diverse career, allowing you to contribute to the advancement of technology and enhance your skills in a rapidly developing field.

So, don’t hesitate to dive into the world of computer vision and explore its possibilities, as the future of technology depends on bright and innovative minds like yours.

Share7Share41Send
Previous Post

Real-time Human Pose Estimation using MediaPipe

Next Post

Building Rome in a Day: 3D Reconstruction with Computer Vision

Carlos Melo

Carlos Melo

Computer Vision Engineer with a degree in Aeronautical Sciences from the Air Force Academy (AFA), Master in Aerospace Engineering from the Technological Institute of Aeronautics (ITA), and founder of Sigmoidal.

Related Posts

Blog

What is Sampling and Quantization in Image Processing

by Carlos Melo
June 20, 2025
Como equalizar histograma de imagens com OpenCV e Python
Computer Vision

Histogram Equalization with OpenCV and Python

by Carlos Melo
July 16, 2024
How to Train YOLOv9 on Custom Dataset
Computer Vision

How to Train YOLOv9 on Custom Dataset – A Complete Tutorial

by Carlos Melo
February 29, 2024
YOLOv9 para detecção de Objetos
Blog

YOLOv9: A Step-by-Step Tutorial for Object Detection

by Carlos Melo
February 26, 2024
Depth Anything - Estimativa de Profundidade Monocular
Computer Vision

Depth Estimation on Single Camera with Depth Anything

by Carlos Melo
February 23, 2024
Next Post

Building Rome in a Day: 3D Reconstruction with Computer Vision

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
Estimativa de Pose Humana com MediaPipe

Real-time Human Pose Estimation using MediaPipe

September 11, 2023
ORB-SLAM 3: A Tool for 3D Mapping and Localization

ORB-SLAM 3: A Tool for 3D Mapping and Localization

April 10, 2023

Build a Surveillance System with Computer Vision and Deep Learning

1
ORB-SLAM 3: A Tool for 3D Mapping and Localization

ORB-SLAM 3: A Tool for 3D Mapping and Localization

1
Point Cloud Processing with Open3D and Python

Point Cloud Processing with Open3D and Python

1

Fundamentals of Image Formation

0

What is Sampling and Quantization in Image Processing

June 20, 2025
Como equalizar histograma de imagens com OpenCV e Python

Histogram Equalization with OpenCV and Python

July 16, 2024
How to Train YOLOv9 on Custom Dataset

How to Train YOLOv9 on Custom Dataset – A Complete Tutorial

February 29, 2024
YOLOv9 para detecção de Objetos

YOLOv9: A Step-by-Step Tutorial for Object Detection

February 26, 2024

Seguir

  • Aqui nós 🇺🇸, a placa é sua. Quando você troca o carro,  por exemplo, você mesmo tira a sua placa do carro vendido e instala a parafusa no carro novo.

Por exemplo, hoje eu vi aqui no “Detran” dos Estados Unidos, paguei a trasnferência do title do veículo, e já comprei minha primeira placa. 

Tudo muito fácil e rápido. Foi menos de 1 hora para resolver toda a burocracia! #usa🇺🇸 #usa
  • Como um carro autônomo "enxerga" o mundo ao redor?

Não há olhos nem intuição, apenas sensores e matemática. Cada imagem capturada passa por um processo rigoroso: amostragem espacial, quantização de intensidade e codificação digital. 

Esse é o desafio, representar um objeto 3D do mundo real, em pixels que façam sentido para a Inteligência Artificial.

🚗📷 A visão computacional é a área mais inovadora do mundo!

Comente aqui se você concorda.

#carrosautonomos #inteligenciaartificial #IA #visãocomputacional
  • 👁️🤖Visão Computacional: a área mais inovadora do mundo! Clique no link da bio e se inscreva na PÓS EM VISÃO COMPUTACIONAL E DEEP LEARNING! #machinelearning #datascience #visãocomputacional
  • E aí, Sergião @spacetoday Você tem DADO em casa? 😂😂

A pergunta pode ter ficado sem resposta no dia. Mas afinal, o que são “dados”?

No mundo de Data Science, dados são apenas registros brutos. Números, textos, cliques, sensores, imagens. Sozinhos, eles não dizem nada 

Mas quando aplicamos técnicas de Data Science, esses dados ganham significado. Viram informação.

E quando a informação é bem interpretada, ela se transforma em conhecimento. Conhecimento gera vantagem estratégica 🎲

Hoje, Data Science não é mais opcional. É essencial para qualquer empresa que quer competir de verdade.

#datascience #cientistadedados #machinelearning
  • 🎙️ Corte da minha conversa com o Thiago Nigro, no PrimoCast #224

Falamos sobre por que os dados são considerados o novo petróleo - para mim, dados são o novo bacon!

Expliquei como empresas que dominam a ciência de dados ganham vantagem real no mercado. Não por armazenarem mais dados, mas por saberem o que fazer com eles.

Também conversamos sobre as oportunidades para quem quer entrar na área de tecnologia. Data Science é uma das áreas mais democráticas que existem. Não importa sua idade, formação ou cidade. O que importa é a vontade de aprender.

Se você quiser ver o episódio completo, é só buscar por Primocast 224.

“O que diferencia uma organização de outra não é a capacidade de armazenamento de dados; é a capacidade de seu pessoal extrair conhecimento desses dados.”

#machinelearning #datascience #visãocomputacional #python
  • 📸 Palestra que realizei no palco principal da Campus Party #15, o maior evento de tecnologia da América Latina!

O tema que escolhi foi "Computação Espacial", onde destaquei as inovações no uso de visão computacional para reconstrução 3D e navegação autônoma.

Apresentei técnicas como Structure-from-Motion (SFM), uma técnica capaz de reconstruir cidades inteiras (como Roma) usando apenas fotos publicadas em redes sociais, e ORB-SLAM, usada por drones e robôs para mapeamento em tempo real.

#visãocomputacional #machinelearning #datascience #python
  • ⚠️❗ Não deem ideia para o Haddad! 

A França usou Inteligência Artificial para detectar mais de 20 mil piscinas não declaradas a partir de imagens aéreas.

Com modelos de Deep Learning, o governo identificou quem estava devendo imposto... e arrecadou mais de €10 milhões com isso.

Quer saber como foi feito? Veja no post completo no blog do Sigmoidal: https://sigmoidal.ai/como-a-franca-usou-inteligencia-artificial-para-detectar-20-mil-piscinas/

#datascience #deeplearning #computerVision #IA
  • Como aprender QUALQUER coisa rapidamente?

💡 Comece com projetos reais desde o primeiro dia.
📁 Crie um portfólio enquanto aprende. 
📢 E compartilhe! Poste, escreva, ensine. Mostre o que está fazendo. Documente a jornada, não o resultado.

Dois livros que mudaram meu jogo:
-> Ultra Aprendizado (Scott Young)
-> Uma Vida Intelectual (Sertillanges)

Aprenda em público. Evolua fazendo.

#ultralearning #estudos #carreira
  • Como eu usava VISÃO COMPUTACIONAL no Centro de Operações Espaciais, planejando missões de satélites em situações de desastres naturais.

A visão computacional é uma fronteira fascinante da tecnologia que transforma a forma como entendemos e respondemos a desastres e situações críticas. 

Neste vídeo, eu compartilho um pouco da minha experiência como Engenheiro de Missão de Satélite e especialista em Visão Computacional. 

#VisãoComputacional #DataScience #MachineLearning #Python
  • 🤔 Essa é a MELHOR linguagem de programação, afinal?

Coloque sua opinião nos comentários!

#python #datascience #machinelearning
  • 💘 A história de como conquistei minha esposa... com Python!

Lá em 2011, mandei a real:

“Eu programo em Python.”
O resto é história.
  • Para rotacionar uma matriz 2D em 90°, primeiro inverto a ordem das linhas (reverse). Depois, faço a transposição in-place. Isso troca matrix[i][j] com matrix[j][i], sem criar outra matriz. A complexidade segue sendo O(n²), mas o uso de memória se mantém O(1).

Esse padrão aparece com frequência em entrevistas. Entender bem reverse + transpose te prepara para várias variações em matrizes.

#machinelearning #visaocomputacional #leetcode
  • Na última aula de estrutura de dados, rodei um simulador de labirintos para ensinar como resolver problemas em grids e matrizes.

Mostrei na prática a diferença entre DFS e BFS. Enquanto a DFS usa stacks, a BFS utiliza a estrutura de fila (queue). Cada abordagem tem seu padrão de propagação e uso ideal.

#machinelearning #visaocomputacional #algoritmos
  • 🔴 Live #2 – Matrizes e Grids: Fundamentos e Algoritmos Essenciais

Na segunda aula da série de lives sobre Estruturas de Dados e Algoritmos, o foco será em Matrizes e Grids, estruturas fundamentais em problemas de caminho, busca e representação de dados espaciais.

📌 O que você vai ver:

Fundamentos de matrizes e grids em programação
Algoritmos de busca: DFS e BFS aplicados a grids
Resolução ao vivo de problemas do LeetCode

📅 Terça-feira, 01/07, às 22h no YouTube 
🎥 (link nos Stories)

#algoritmos #estruturasdedados #leetcode #datascience #machinelearning
  • 💡 Quer passar em entrevistas técnicas?
Veja essa estratégia para você estudar estruturas de dados em uma sequência lógica e intuitiva.
⠀
👨‍🏫 NEETCODE.io
⠀
🚀 Marque alguém que também está se preparando!

#EntrevistaTecnica #LeetCode #MachineLearning #Data Science
  • Live #1 – Arrays & Strings: Teoria e Prática para Entrevistas Técnicas

Segunda-feira eu irei começar uma série de lives sobre Estruturas de Dados e Algoritmos. 

No primeiro encontro, falarei sobre um dos tipos de problemas mais cobrados em entrevistas: Arrays e Strings.

Nesta aula, você vai entender a teoria por trás dessas estruturas, aprender os principais padrões de resolução de problemas e aplicar esse conhecimento em exercícios selecionados do LeetCode.

📅 Segunda-feira, 23/06, às 21h no YouTube

🎥 (link nos Stories)

#machinelearning #datascience #cienciadedados #visãocomputacional
Instagram Youtube LinkedIn Twitter
Sigmoidal

O melhor conteúdo técnico de Data Science, com projetos práticos e exemplos do mundo real.

Seguir no Instagram

Categories

  • Aerospace Engineering
  • Blog
  • Carreira
  • Computer Vision
  • Data Science
  • Deep Learning
  • Featured
  • Iniciantes
  • Machine Learning
  • Posts

Navegar por Tags

3d 3d machine learning 3d vision apollo 13 bayer filter camera calibration career cientista de dados clahe computer vision custom dataset data science deep learning depth anything depth estimation detecção de objetos digital image processing histogram histogram equalization image formation job lens lente machine learning machine learning engineering nasa object detection open3d opencv pinhole projeto python quantization redes neurais roboflow rocket salário sampling scikit-learn space tensorflow tutorial visão computacional yolov8 yolov9

© 2024 Sigmoidal - Aprenda Data Science, Visão Computacional e Python na prática.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Cursos
  • Pós-Graduação
  • Blog
  • Sobre Mim
  • Contato
  • Português

© 2024 Sigmoidal - Aprenda Data Science, Visão Computacional e Python na prática.