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
99
SHARES
3.3k
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.

Share7Share40Send
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

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
Point Cloud Processing with Open3D and Python
Computer Vision

Point Cloud Processing with Open3D and Python

by Carlos Melo
February 12, 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
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
Depth Anything - Estimativa de Profundidade Monocular

Depth Estimation on Single Camera with Depth Anything

February 23, 2024

Seguir

  • Cada passo te aproxima do que realmente importa. Quer continuar avançando?

🔘 [ ] Agora não
🔘 [ ] Seguir em frente 🚀
  • 🇺🇸 Green Card por Habilidade Extraordinária em Data Science e Machine Learning

Após nossa mudança para os EUA, muitas pessoas me perguntaram como consegui o Green Card tão rapidamente. Por isso, decidi compartilhar um pouco dessa jornada.

O EB-1A é um dos vistos mais seletivos para imigração, sendo conhecido como “The Einstein Visa”, já que o próprio Albert Einstein obteve sua residência permanente através desse processo em 1933.

Apesar do apelido ser um exagero moderno, é fato que esse é um dos vistos mais difíceis de conquistar. Seus critérios rigorosos permitem a obtenção do Green Card sem a necessidade de uma oferta de emprego.

Para isso, o aplicante precisa comprovar, por meio de evidências, que está entre os poucos profissionais de sua área que alcançaram e se mantêm no topo, demonstrando um histórico sólido de conquistas e reconhecimento.

O EB-1A valoriza não apenas um único feito, mas uma trajetória consistente de excelência e liderança, destacando o conjunto de realizações ao longo da carreira.

No meu caso específico, após escrever uma petição com mais de 1.300 páginas contendo todas as evidências necessárias, tive minha solicitação aprovada pelo USCIS, órgão responsável pela imigração nos Estados Unidos.

Fui reconhecido como um indivíduo com habilidade extraordinária em Data Science e Machine Learning, capaz de contribuir em áreas de importância nacional, trazendo benefícios substanciais para os EUA.

Para quem sempre me perguntou sobre o processo de imigração e como funciona o EB-1A, espero que esse resumo ajude a esclarecer um pouco mais. Se tiver dúvidas, estou à disposição para compartilhar mais sobre essa experiência! #machinelearning #datascience
  • 🚀Domine a tecnologia que está revolucionando o mundo.

A Pós-Graduação em Visão Computacional & Deep Learning prepara você para atuar nos campos mais avançados da Inteligência Artificial - de carros autônomos a robôs industriais e drones.

🧠 CARGA HORÁRIA: 400h
💻 MODALIDADE: EAD
📅 INÍCIO DAS AULAS: 29 de maio

Garanta sua vaga agora e impulsione sua carreira com uma formação prática, focada no mercado de trabalho.

Matricule-se já!

#deeplearning #machinelearning #visãocomputacional
  • Green Card aprovado! 🥳 Despedida do Brasil e rumo à nova vida nos 🇺🇸 com a família!
  • Haverá sinais… aprovado na petição do visto EB1A, visto reservado para pessoas com habilidades extraordinárias!

Texas, we are coming! 🤠
  • O que EU TENHO EM COMUM COM O TOM CRUISE??

Clama, não tem nenhuma “semana” aberta. Mas como@é quinta-feira (dia de TBT), olha o que eu resgatei!

Diretamente do TÚNEL DO TEMPO: Carlos Melo &Tom Cruise!
  • Bate e Volta DA ITÁLIA PARA A SUÍÇA 🇨🇭🇮🇹

Aproveitei o dia de folga após o Congresso Internacional de Astronáutica (IAC 2024) e fiz uma viagem “bate e volta” para a belíssima cidade de Lugano, Suíça.

Assista ao vlog e escreve nos comentários se essa não é a cidade mais linda que você já viu!

🔗 LINK NOS STORIES
  • Um paraíso de águas transparentes, e que fica no sul da Suíça!🇨🇭 

Conheça o Lago de Lugano, cercado pelos Alpes Suíços. 

#suiça #lugano #switzerland #datascience
  • Sim, você PRECISA de uma PÓS-GRADUAÇÃO em DATA SCIENCE.
  • 🇨🇭Deixei minha bagagem em um locker no aeroporto de Milão, e vim aproveitar esta última semana nos Alpes suíços!
  • Assista à cobertura completa no YT! Link nos stories 🚀
  • Traje espacial feito pela @axiom.space em parceria com a @prada 

Esse traje será usados pelos astronautas na lua.
para acompanhar as novidades do maior evento sobre espaço do mundo, veja os Stories!

#space #nasa #astronaut #rocket
  • INTERNATIONAL ASTRONAUTICAL CONGRESS - 🇮🇹IAC 2024🇮🇹

Veja a cobertura completa do evento nos DESTAQUES do meu perfil.

Esse é o maior evento de ESPAÇO do mundo! Eu e a @bnp.space estamos representando o Brasil nele 🇧🇷

#iac #space #nasa #spacex
  • 🚀 @bnp.space is building the Next Generation of Sustainable Rocket Fuel.

Join us in transforming the Aerospace Sector with technological and sustainable innovations.
  • 🚀👨‍🚀 Machine Learning para Aplicações Espaciais

Participei do maior congresso de Astronáutica do mundo, e trouxe as novidades e oportunidade da área de dados e Machine Learning para você!

#iac #nasa #spacex
  • 🚀👨‍🚀ACOMPANHE NOS STORIES

Congresso Internacional de Astronáutica (IAC 2024), Milão 🇮🇹
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 Clustering data science deep learning depth anything depth estimation detecção de objetos digital image processing histogram histogram equalization image formation job keras lens lente machine learning machine learning engineering nasa object detection open3d opencv pinhole profissão projeto python redes neurais roboflow rocket 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.