The img3d
library unlocks infinite possibilities.
- In the following example, the image rotates around the points
$(0, 0, 300)$ and$(0, 0, -300)$ in a 3-dimensional Cartesian space. Can you spot the difference?
- This example demonstrates that the rotation center plays a critical role.
This repository provides a tool for 3D image transformations, including rotations and translations in 3D Cartesian coordinates. The transformations are implemented in 3D space using the sequential logic of the main class.
The primary concept of this library originates from the work of Hou-Ning Hu, whose GitHub repository can be found here. In addition to fixing existing bugs, the logic for utilizing this ImageTransformer
has been modified to a sequential logic to accommodate all use cases.
This repository is linked to the PyPI library img3d
, which can be installed using the following command:
pip install img3d
Image transformation is a critical component of image processing, particularly for data fusion in real-time applications. This class enables users to perform 3D rotations and translations on images in any desired spatial configuration.
The library's primary output is a ImageTransformer
leverages OpenCV (if installed) for efficient implementation.
The img3d
library treats the image center as the origin of the coordinate system during transformations. Each pixel in the image data is represented as:
where
The translation matrix is defined as:
The rotation matrices are defined as:
Given that most image processing frameworks (e.g., OpenCV) define the origin of coordinates at the upper-left corner, it is essential to translate the image center to this reference point. This adjustment ensures proper rotational transformations, as the image center serves as the logical origin in real-world applications. After the desired rotation (
The algorithm of 3D image transformation in this class is as below:
Step 1: 2D to 3D projection (
The first step is to bring the image into a 3D space, using the following 2D to 3D projection matrix:
Step 2: Center to origin (
Translate the image using
Step 3: Desired transformation (
This step represents the primary usage of the library, meaning that all rotations and transformations occur at this level. All other steps operate behind the scenes, while this step is explicitly controlled by the user.
Step 4: Move to the focal point (
In this step, the image is positioned at the focal point of the image. For more information on the details of the current and next step, please refer to this link.
Step 5: 3D to 2D projection (
The following matrix moves the image to the origin and from 3D to 2D coordinates, forming the final
By going through these steps, the final homography matrix is formed, using the following matrix multiplication.
Then, the homography matrix
$$
F_{new}=HF
$$
where
Note that in case that
In this section, brief examples are explained. For more examples, please refer to the example folder in this repository.
Firstly, import the necessary libraries, create an instance of the ImageTransformer
class, and import an image.
Note that after calling the transform
or get_homography
functions, the class's homography matrix is reset to an identity matrix.
from img3d import ImageTransformer
import cv2
# import an image
src = cv2.imread('image.jpg')
height, width, _ = src.shape
# define an instance of the ImageTransformer class
fov_vertical = 70 # vertical field of view of the camera
T = ImageTransformer(width, height, fov_vertical)
T.rotate(alpha = 20, beta = 30, gamma = 10)
dst = T.transform(src)
T.translate(dx = 640//2, dy = 480//2)
T.rotate(gamma = 149)
H = T.get_homography()
dst = cv2.warpPerspective(frame, H, (width, height))