Applying a 4x4 3D transformation matrix to a PNG image

135 Views Asked by At

I'm working on a project where I need to apply a 3D transformation represented by a 4x4 transformation matrix to a PNG image using Python. However, I'm having difficulties finding a solution that works correctly.

The transformation matrix I have is as follows:

transformation_matrix = np.array([
    [0.90062326, -0.11755195, -0.4184016, 0.0],
    [0.17818762, 0.97796422, 0.10879099, 0.0],
    [0.39639321, -0.17253359, 0.90172333, 0.0],
    [0.0, 0.0, 0.0, 1.0]
])

I would like to know the correct approach to apply this transformation matrix to the PNG image and obtain the desired result.

Currently, I'm using the PIL library to load the image and convert it to a numpy array. Then, I'm attempting to apply the transformation matrix to the image array using matrix multiplication.

However, the results are not coming out as expected. The image has altered colors, and the transformation is not being applied correctly.

Using Blender was quite straightforward, but I would like to run this transformation without the need for Blender on my headless server.

import bpy
from mathutils import Matrix

# Selecione o objeto plano
bpy.context.view_layer.objects.active = bpy.data.objects["object_name"]
obj = bpy.context.active_object

# Defina a matriz de transformação
matriz_transformacao = Matrix(
    ((0.90062326, -0.11755195, -0.4184016, 0),
     (0.17818762, 0.97796422, 0.10879099, 0),
     (0.39639321, -0.17253359, 0.90172333, 0),
     (0.0, 0.0, 0.0, 1.0))
)

# Aplique a matriz de transformação ao objeto
obj.matrix_world = matriz_transformacao @ obj.matrix_world

# Configure as opções de renderização
bpy.context.scene.render.image_settings.file_format = 'PNG'  # Formato de arquivo PNG para suportar transparência
bpy.context.scene.render.image_settings.color_mode = 'RGBA'  # Modo de cor RGBA para suportar transparência
bpy.context.scene.render.film_transparent = True  # Configura o renderizador Cycles para suportar transparência

# Renderize a imagem
bpy.ops.render.render(write_still=True)

The original image.

The original image

The ultimate goal.

The ultimate goal

I would appreciate any help or guidance on how to properly perform this transformation and achieve the desired result.

0

There are 0 best solutions below