python - Applying a 2D deformation field to an image - Stack Overflow

I am trying to deform an image using the following vector field.I have tried to use the response in th

I am trying to deform an image using the following vector field.

I have tried to use the response in this post. However, all of my attempts have been unsuccessful till now.

The deformation field is generated using the following code:

import numpy as np
import cv2

size = 100
x = np.linspace(-1, 1, size)
y = np.linspace(-1, 1, size)
X, Y = np.meshgrid(x, y)
phase_profile = 800.0*((X-0.0)**2 + (Y-0.0)**2)

How to deform an image given the deformation field represented by the light blue arrows ? Here is an example of an image I am trying to deform.

My attempt is the following:

def stack_overflow_gradient_distortion_map(phase_profile, size=256, coeff=30.0):

    # Create coordinate grids
    x = np.linspace(-1, 1, size)
    y = np.linspace(-1, 1, size)
    X, Y = np.meshgrid(x, y)

    shape = X.shape

    dx = 2 / (size - 1)
    grad_y, grad_x = np.gradient(phase_profile, dx, dx)

    # Compute gradients (deformation field)
    gy, gx = np.gradient(phase_profile)

    mapx_base, mapy_base = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]))

    mapx = mapx_base + gx*coeff
    mapy = mapy_base + gy*coeff

    return mapx, mapy

P.S: The plot function used is the following:

def plot_phase_heatmap_with_gradients(phase_profile, mesh_grid_x, mesh_grid_y):

    dx = 2 / (size - 1) # Physical spacing in the [-1, 1] range
    grad_y, grad_x = np.gradient(phase_profile, dx, dx)
    plt.figure(figsize=(8, 6))
    plt.imshow(phase_profile, cmap='hot', origin='lower', extent=[-1, 1, -1, 1])
    plt.colorbar(label='Phase Profile')

    print("max grad_x: ", np.max(grad_x))

    # Overlay gradient arrows
    skip = 10  # Adjust to reduce arrow density
    plt.quiver(mesh_grid_x[::skip, ::skip], mesh_grid_y[::skip, ::skip], grad_x[::skip, ::skip], grad_y[::skip, ::skip], color='cyan')

    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.title('Phase Profile with Gradient Vectors')
    plt.show()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744223081a4563877.html

相关推荐

  • python - Applying a 2D deformation field to an image - Stack Overflow

    I am trying to deform an image using the following vector field.I have tried to use the response in th

    9天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信