A Python Library for 3D Image Transformation.
Project description
image_transformer
Abstract
This is 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.
Introduction
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 3×3 homography matrix, which can be directly integrated with perspective-warping functions in image processing libraries such as OpenCV, VPI, or other compatible frameworks. By default, the ImageTransformer leverages OpenCV (if installed) for efficient implementation.
Description
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:
$$
f =
\begin{bmatrix}
x \
y \
1 \
\end{bmatrix}
$$
where $x$ and $y$ are the pixel coordinates respectively.
The translation matrix is defined as:
$$
T(dx, dy, dz) =
\begin{bmatrix}
1 & 0 & 0 & dx \
0 & 1 & 0 & dy \
0 & 0 & 1 & dz \
0 & 0 & 0 & 1 \
\end{bmatrix}
$$
The rotation matrices are defined as:
$$
R_x(\alpha) =
\begin{bmatrix}
1 & 0 & 0 & 0 \
0 & cos(\alpha) & -sin(\alpha) & 0 \
0 & sin(\alpha) & cos(\alpha) & 0 \
0 & 0 & 0 & 1 \
\end{bmatrix}
$$
$$
R_y(\beta) =
\begin{bmatrix}
cos(\beta) & 0 & -sin(\beta) & 0 \
0 & 1 & 0 & 0 \
sin(\beta) & 0 & cos(\beta) & 0 \
0 & 0 & 0 & 1 \
\end{bmatrix}
$$
$$
R_z(\gamma) =
\begin{bmatrix}
cos(\gamma) & -sin(\gamma) & 0 & 0 \
sin(\gamma) & cos(\gamma) & 0 & 0 \
0 & 0 & 1 & 0 \
0 & 0 & 0 & 1 \
\end{bmatrix}
$$
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 ($R_{4\times4}$) and translation ($T_{4\times4}$) matrices are applied by the user, the image center is restored to its original position.
The algorithm of 3D image transformation in this class is as below:
Step 1: 2D to 3D projection ($A_1$)
The first step is to bring the image into a 3D space, using the following 2D to 3D projection matrix:
$$
A_1 =
\begin{bmatrix}
1 & 0 & 0 \
0 & 1 & 0 \
0 & 0 & 1 \
0 & 0 & 1 \
\end{bmatrix}
$$
Step 2: Center to origin ($T_c$)
Translate the image using $T(\frac{-W}{2}, \frac{-H}{2}, 0)$, where $W$ nad $H$ are the width and height of the image, respectively. This positions the image center at the origin.
Step 3: Desired transformation ($M$)
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 ($T_f$)
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 ($A_2$)
The following matrix moves the image to the origin and from 3D to 2D coordinates, forming the final $3\times3$ homography matrix.
$$
A_2 =
\begin{bmatrix}
f & 0 & \frac{W}{2} & 0 \
0 & f & \frac{H}{2} & 0 \
0 & 0 & 1 & 0 \
\end{bmatrix}
$$
By going through these steps, the final homography matrix is formed, using the following matrix multiplication.
$$
H=A_2 , T_f , M , T_c , A_1
$$
Then, the homography matrix $H$ is applied to the image, using the following formula (using OpenCV or VPI):
$$
f_{new}=Hf
$$
Note that in case that $M=I_{4\times4}$, the image stays intact.
Example
In this section, brief examples are explained. For more examples, please refer to the 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)
Example I:
T.rotate(alpha = 20, beta = 30, gamma = 10)
dst = T.transform(src)
Example II:
T.translate(dx = 640//2, dy = 480//2)
T.rotate(gamma = 149)
H = T.get_homography()
dst = cv2.warpPerspective(frame, H, (width, height))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file img3d-1.0.0.tar.gz.
File metadata
- Download URL: img3d-1.0.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22ab1ebf970a8a54c7762ed4a2cd64912807ead9eafe780dec9ef693a67189ec
|
|
| MD5 |
899e9008890d0316b37917fd229c3ecd
|
|
| BLAKE2b-256 |
d4614001fa4347728f3acf65fbd4603768bfd236782c9fe8d63007b0cb33d66d
|
File details
Details for the file img3d-1.0.0-py3-none-any.whl.
File metadata
- Download URL: img3d-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a0330763da483a4557f5f9e2567033de70793089f6ec0f7305495758f9f2e76
|
|
| MD5 |
1b8e3c5d2993f332dfbbc9c1ce7847e4
|
|
| BLAKE2b-256 |
df8b5bcf5f0506b7005a64d3dafb695fc3edd389e71f667bba343c91281f6d5d
|