Adjusting the Look
Rotation angles
Increase or decrease the angle around
𝑥 (or 𝑦) to get our desired tilt.
Combining rotations
We can rotate around 𝑥 by some angle, then aroundn𝑦 by another angle to get a more complex orientation.
Focal length & viewer distance
Tweak these if the image looks too “stretched” or too “flat.” A larger focal length with a moderate viewer distance often looks more “telephoto,” while a smaller focal length with a moderate viewer distance looks more “wide‐angle.”
In contrast, the simpler 2D code snippet that uses cv2.getRotationMatrix2D(center, angle, scale) is effectively a rotation around the 𝑧-axis—but only in 2D image space. Internally, getRotationMatrix2D rotates the plane as though we are spinning it around an axis coming “straight out of” the image (which we can think of as the 𝑧-axis in 3D). But it doesn’t do an actual 3D transformation of the scene—just a flat rotation in the 2D plane. In the true 3D rotation example (the code with rotate_around_x and rotate_around_y functions), there is no explicit rotation around the 𝑧-axis. Those functions only construct the rotation matrix for 𝑥‐axis or 𝑦‐axis rotations. If we wanted to rotate around the 𝑧-axis in that 3D approach, we would need a similar function—for z:
1. 3D Rotation:
We can rotate the image in 3D space around three axes:
Rotate around X-axis: rotate_around_x rotates the image around the horizontal (x) axis.
Rotate around Y-axis: rotate_around_y rotates around the vertical (y) axis.
Rotate around Z-axis: rotate_around_z rotates around the depth (z) axis.
These rotations can be combined in any order, allowing for complex 3D orientations.
2. 2D Projection:
The project_points function simulates a "camera" viewing the rotated 3D image.
It uses a perspective model to project the 3D points onto a 2D plane.
The viewer_distance parameter controls how far the "camera" is from the image, affecting the perspective effect.
3. Perspective Transform Calculation:
OpenCV's cv2.getPerspectiveTransform function takes the original 2D corner points of the image and the newly projected 2D corner points (after 3D rotation and projection).
It calculates a 3x3 homography matrix (H), which describes the transformation needed to map the original image to the rotated and projected image.
4. Image Warping:
OpenCV's cv2.warpPerspective function applies the homography matrix (H) to the original image.
It re-maps each pixel of the original image to its corresponding location in the new, transformed image.
The output image size can be specified, just ensure its large enough to contain the entire transformed image.
Controlling the 3D Effect:
By adjusting the rotation angles (rot_x, rot_y, rot_z in degrees), you can control the 3D orientation of the image.
The focal_length and viewer_distance parameters allow you to fine-tune the perspective look, creating different visual effects.