import math
import numpy as np
import matplotlib.pyplot as plt
#cái này là 3D với 2D nha hình ảnh máy tính tự động chọn
def rotate_point_2d(x, y, angle):
"""Rotate a point (x, y) around the origin by the given angle (in
degrees)."""
angle_rad = math.radians(angle)
cos_theta = math.cos(angle_rad)
sin_theta = math.sin(angle_rad)
x_new = x * cos_theta - y * sin_theta
y_new = x * sin_theta + y * cos_theta
return x_new, y_new
def rotate_point_3d(x, y, z, angle_x, angle_y, angle_z):
"""Rotate a point (x, y, z) around the origin by the given angles (in
degrees)."""
rotation_matrix_x = np.array([[1, 0, 0],
[0, math.cos(math.radians(angle_x)), -
math.sin(math.radians(angle_x))],
[0, math.sin(math.radians(angle_x)),
math.cos(math.radians(angle_x))]])
rotation_matrix_y = np.array([[math.cos(math.radians(angle_y)), 0,
math.sin(math.radians(angle_y))],
[0, 1, 0],
[-math.sin(math.radians(angle_y)), 0,
math.cos(math.radians(angle_y))]])
rotation_matrix_z = np.array([[math.cos(math.radians(angle_z)), -
math.sin(math.radians(angle_z)), 0],
[math.sin(math.radians(angle_z)),
math.cos(math.radians(angle_z)), 0],
[0, 0, 1]])
rotated_point = np.dot(rotation_matrix_z, np.dot(rotation_matrix_y,
np.dot(rotation_matrix_x, np.array([x, y, z]))))
return rotated_point[0], rotated_point[1], rotated_point[2]
def rotate_image(input_image, angle_2d, angles_3d=None):
"""Rotate the image at the given angle (in degrees) and return the
rotated image."""
width, height = input_image.shape[:2]
if angles_3d is None:
angle = angle_2d % 360
center_x, center_y = width / 2, height / 2
output_image = np.ones_like(input_image) * 255
for x in range(width):
for y in range(height):
x_translated, y_translated = x - center_x, y - center_y
x_rotated, y_rotated = rotate_point_2d(x_translated,
y_translated, angle)
x_rotated += center_x
y_rotated += center_y
if 0 <= x_rotated < width and 0 <= y_rotated < height:
output_image[int(x_rotated), int(y_rotated)] =
input_image[x, y]
else:
angle_x, angle_y, angle_z = angles_3d
output_image = np.ones_like(input_image) * 255
for x in range(width):
for y in range(height):
for z in range(3): # 3 channels (RGB)
x_rotated, y_rotated, z_rotated = rotate_point_3d(x,
y, z, angle_x, angle_y, angle_z)
x_rotated, y_rotated = int(x_rotated), int(y_rotated)
if 0 <= x_rotated < width and 0 <= y_rotated < height:
output_image[int(x_rotated), int(y_rotated), z] =
input_image[x, y, z]
return output_image
input_image_2d = np.zeros((100, 100, 3), dtype=np.uint8)
input_image_2d[20:80, 20:80] = [255, 0, 0] # Add a red square
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(input_image_2d)
plt.title('input_image_2d')
input_image_3d = np.zeros((100, 100, 3), dtype=np.uint8)
input_image_3d[20:80, 20:80] = [255, 0, 0] # Add a red square in all 3
channels
plt.subplot(1, 2, 2)
plt.imshow(input_image_3d)
plt.title('input_image_3d')
angle_2d = 10
angles_3d = (45, 30, 60)
rotated_image_2d = rotate_image(input_image_2d, angle_2d)
rotated_image_3d = rotate_image(input_image_3d, None, angles_3d)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(rotated_image_2d)
plt.title('Rotated 2D Image')
plt.subplot(1, 2, 2)
plt.imshow(rotated_image_3d)
plt.title('Rotated 3D Image')
plt.show()