siavashk / pycpd

Pure Numpy Implementation of the Coherent Point Drift Algorithm
MIT License
513 stars 115 forks source link

how this package could be used for aligning two 2D data (or two 2d cloud points ) #11

Closed rebeen closed 6 years ago

rebeen commented 6 years ago

I want to align two TSNE data using this package could you tell me how to use this package ?

from functools import partial
from scipy.io import loadmat
import matplotlib.pyplot as plt
from pycpd import affine_registration,rigid_registration(X, Y)
import numpy as np
import time

def visualize(iteration, error, X, Y, ax):
    plt.cla()
    ax.scatter(X[:,0] ,  X[:,1], color='red')
    ax.scatter(Y[:,0] ,  Y[:,1], color='blue')
    plt.draw()
    print("iteration %d, error %.5f" % (iteration, error))
    plt.pause(0.001)

def main():
    X = np.random.random((10, 2))  # 10 points in 2 dimensions
    Y = np.random.random((10, 2))  # 10 points in 2 dimensions
    fig = plt.figure()
    fig.add_axes([0, 0, 1, 1])
    callback = partial(visualize, ax=fig.axes[0])

    reg = rigid_registration(X, Y)
    reg.register(callback)
    plt.show()

main()
siavashk commented 6 years ago

Hi Rebeen,

The issue is these two lines:

    X = np.random.random((10, 2))  # 10 points in 2 dimensions
    Y = np.random.random((10, 2))  # 10 points in 2 dimensions

You simply cannot register random points to each other. Registration implies correspondence and random points do not correspond to each other.

If you are just testing the code out, you can apply a known transformation to a random point cloud and see if the registration can recover it:

# Known transform (rotation + translation)
R = np.array([[0.866, -0.5], [0.5, 0.866]])
t = np.array([0.5, 1.0])

# Source point cloud
Y = np.random.random((10, 2))  # 10 points in 2 dimensions

# Target point cloud
X = np.dot(Y, R) + np.tile(t, (np.shape(Y)[0], 1))
rebeen commented 6 years ago

can I use it for align two Tsne data? do you think it works good for the two TSNE , of course the two tsne has some correspondence between them because they comes from the same data ...... regarding this above code I checked got this result >see attached image ,, thanks for your help nn

siavashk commented 6 years ago

My bad. The order of matrix multiplication for X = R * Y is X = np.dot(Y, R).

Other than providing working examples, e.g. examples/fishRigid2D.py, I do not know how else I can help you.

rebeen commented 6 years ago

please can you help me to apply on my data the two TSNE data that are 2 D

yes know I got result for your data, it is aligned correctly tsne

siavashk commented 6 years ago

If you do TY, R_reg, t_reg, s_reg = reg.register(callback) instead of reg.register(callback) (line 13), you can check the following to see if the registration has worked:

  1. Does TY equal or close to X?
  2. Are R_reg, t_reg and s_reg close to R, t and 1.0, respectively?

If this is true then registration works. I can’t debug a problem that does not exist.

rebeen commented 6 years ago

this is the output of TY and X tsne

siavashk commented 6 years ago

They are equal, so the registration was successful.

rebeen commented 6 years ago

this is all

tsne

rebeen commented 6 years ago

I dont have any problem with this but my question is how to use this to align two TSNE(2d data) ?

siavashk commented 6 years ago

If the transformation between two t-SNE point clouds can be modelled using rigid, affine or deformable transformations, then you can use rigid_registration, affine_registration or deformable_registration using the same approach that you just tried.

Does this make sense theoretically? I am not sure if the transformation between two t-SNE point clouds can be modelled as such. Then again I am not a machine learning expert.

rebeen commented 6 years ago

the problem is I dont know about rigid_registration, affine_registration or deformable_registration . also I am confusing about the registration word , I think you meant align right ?

rebeen commented 6 years ago

usually for alignment people use Procrustes analysis and Iterative closes point but I dont know about this algorithm

siavashk commented 6 years ago

Rebeen you either need to take a course in point cloud registration or ask your graduate supervisor to help you with understanding underlying concepts. Unfortunately I do not have enough bandwidth to help you with your research.

siavashk commented 6 years ago

This might be a good starting point: https://en.wikipedia.org/wiki/Point_set_registration Or this survey paper: https://orca.cf.ac.uk/47333/1/ROSIN%20registration%20of%203d%20point%20clouds%20and%20meshes.pdf

siavashk commented 6 years ago

Good luck with your research!

rebeen commented 6 years ago

thank you