Giter VIP home page Giter VIP logo

Comments (1)

junyuchen245 avatar junyuchen245 commented on June 16, 2024

Hi @chengjiawang,

There are two ways to visualize the deformed grid:

  1. Create a mesh grid. Then apply the displacement field to the mesh grid:
def plot_grid(gridx,gridy, **kwargs):
    for i in range(gridx.shape[1]):
        plt.plot(gridx[i,:], gridy[i,:], **kwargs)
    for i in range(gridx.shape[0]):
        plt.plot(gridx[:,i], gridy[:,i], **kwargs)

# Assume an image has size 384 x 384
x = np.arange(0, 384, 1)
y = np.arange(0, 384, 1)
X, Y = np.meshgrid(x, y)
u = displacement_field[0, 0, :, :] #batch, channel, size_x, size_y
v = displacement_field[0, 1, :, :] #batch, channel, size_x, size_y
for i in range(0, 384):
    for j in range(0, 384):
        # Apply displacements
        X[i, j] = X[i, j] - u[i, j]
        Y[i, j] = Y[i, j] - v[i, j]
plt.figure()
plot_grid(X, Y, color="C0")
plt.title('Deformed grid')
plt.show()
  1. Create an image with a "synthetic" mesh grid. Then apply the displacement field to the image:
# Assume an image has size 160 x 192 x 224
def mk_grid_img(grid_step, line_thickness=1, grid_sz=(160, 192, 224)):
    grid_img = np.zeros(grid_sz)
    for j in range(0, grid_img.shape[1], grid_step):
        grid_img[:, j+line_thickness-1, :] = 1
    for i in range(0, grid_img.shape[2], grid_step):
        grid_img[:, :, i+line_thickness-1] = 1
    grid_img = grid_img[None, None, ...]
    grid_img = torch.from_numpy(grid_img).cuda()
    return grid_img

grid_img = mk_grid_img(8, 1, (160, 192, 224))
def_grid = spatial_transformation_function(grid_img.float(), displacement_field.cuda())
def_grid = def_grid.detach().cpu().numpy()[0, 0, :, :, :]

plt.figure()
plt.imshow(img[80, :, :], cmap='gray') # visualize an arbitrary slice
plt.title('Deformed grid')
plt.show()

We used method 2 in our papers because it's more robust and straightforward, and I think the VoxelMorph paper used a similar method as well.

I hope this is helpful.

Junyu

from vit-v-net_for_3d_image_registration_pytorch.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.