python
import cv2
import numpy as np
def rotate_image(image, angle, center=None, scale=1.0):
"""Rotate the image based on the given angle."""
(h, w) = image.shape[:2]
if center is None:
center = (w / 2, h / 2)
# Perform the rotation
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
def adjust_normal_map(normal_map, angle):
"""Adjust the normal map based on the rotation."""
angle_rad = np.deg2rad(angle)
rotation_matrix = np.array([
[np.cos(angle_rad), -np.sin(angle_rad), 0],
[np.sin(angle_rad), np.cos(angle_rad), 0],
[0, 0, 1]
])
reshaped_normals = normal_map.reshape((-1, 3)).T
adjusted_normals = np.dot(rotation_matrix, reshaped_normals)
return adjusted_normals.T.reshape(normal_map.shape)
def process_image_with_normal_map(image_path, normal_map_path, angle):
image = cv2.imread(image_path)
normal_map = cv2.imread(normal_map_path, cv2.IMREAD_UNCHANGED)
rotated_image = rotate_image(image, angle)
adjusted_normal_map = adjust_normal_map(normal_map, angle)
return rotated_image, adjusted_normal_map
# Example usage
image_path = 'path/to/your/2d_image.jpg'
normal_map_path = 'path/to/your/normal_map.png'
angle = 30 # example angle in degrees
rotated_image, adjusted_normal_map = process_image_with_normal_map(image_path, normal_map_path, angle)
cv2.imwrite('rotated_image.jpg', rotated_image)
cv2.imwrite('adjusted_normal_map.png', adjusted_normal_map)
print('Processing completed. Rotated image and adjusted normal map saved.')