# iterative closest point

I want to implement ICP(iterative closest point) algorithm

- Associate points by the nearest neighbor criteria.
- Estimate transformation parameters using a mean square cost function.
- Transform the points using the estimated parameters.
- Iterate (re-associate the points and so on).

For every point in 1st set I found nearest point in 2nd set, but I don't understand how to do the 2nd step.

I tried folowing code, but it does't work, maybe I need to reject some pairs?

```
vector<Point> vec_pair;
for(int i=0;i<vec_M.size();++i)
{
double min_dist=INT_MAX;
int id=-1;
for(int j=0;j<vec_T.size();++j)
{
double metric= sqrt(double(vec_T[j].x-vec_M[i].x)*(vec_T[j].x-vec_M[i].x)+
(vec_T[j].y-vec_M[i].y)*(vec_T[j].y-vec_M[i].y));
if(min_dist>metric)
{
min_dist=metric;
id=j;
}
}
line(img,vec_M[i],vec_T[id],cvScalar(0,0,255));
vec_pair.push_back(vec_T[id]);
}
Mat m= estimateRigidTransform(vec_M,vec_pair,1);
cout<<m;
```

Without looking at your code, but looking at your image, i think you have to somehow apply the following logic: if a point already has a neighbor, do not attribute it another neighbor.