To solve this problem, I used an optimization method in 2 steps :
1) I loop on the lateral shift (X) and stretch of my map to find the greatest number of points with a nearest neighbor at a distance of less than 11 inches : a "pair" of points.
( R = red points, B = blue points )
D_min = zeros(length(R_X),1);
pair_numbers = zeros(length(steps_stretch), length(steps_X));
for l = 1:length(steps_stretch)
for k = 1:length(steps_X)
l
k
B_X_slide_stretch = B_X*steps_stretch(l) + steps_M(k);
B_Y_slide = B_Y;
for i=1:length(R_X)
D_min(i) = min((B_X_slide_stretch - R_X(i)).^2 + (B_Y_slide - R_Y(i)).^2);
end
pair_numbers(l,k) = size(D_min(D_min<121),1);
end
end
[min_per_row, Index_column_local_min_per_row] = max(pair_numbers, [],2);
[min_table, Index_row] = max(min_per_row);
Index_column = Index_column_local_min_per_row(Index_row);
B_Y_pairing = B_Y;
B_X_pairing = B_X * steps_stretch(Index_row) + steps_X(Index_column);
Then, I consider only the R points (red) that I assume have a pair in the B (blue) set of points.
for i=1:length(R_X)
D_min(i) = min(sqrt((B_X_pairing - R_X(i)).^2 + (B_Y_pairing - R_Y(i)).^2));
end
R_X_pairing = R_X(D_min<11);
R_Y_pairing = R_Y(D_min<11);
2) The second part of the optimization is about minimizing the distance between a point and its pair by playing with the 3 parameters : X (lateral shift), Y (vertical shift) and the stretch.
I use the fmincon function from the Optimization_Toolbox to find the minimum of the function @sum_squared_distances.
nterval = bounds_X_translation(2) - bounds_X_translation(1);
nsteps = 5;
x = zeros(nsteps+1,3);
fval = zeros(nsteps+1,1);
exitflag = zeros(nsteps+1,1);
tic
for i = 0:nsteps
i+1
TolX = 10^(-5);
options = optimset('TolX', TolX, 'Tolfun', 1, 'MaxIter', 500, 'MaxFunEvals', 5000, 'Display', 'iter',...
'LargeScale', 'off', 'DiffMaxChange', 500*12, 'DiffMinChange', TolX/1000,...
'LevenbergMarquardt', 'on', 'LineSearchType', 'cubicpoly');
[x(i+1,:),fval(i+1),exitflag(i+1),output] = fmincon(@sum_squared_distances,[bounds_X_translation(1) + i*interval/nsteps 0 1],[],[],[],[],...
[bounds_X_translation(1) bounds_Y_translation(1) bounds_X_stretch(1)],...
[bounds_X_translation(2) bounds_Y_translation(2) bounds_X_stretch(2)],...
[],options);
end
Optimization_time = toc;
save('Results_optimization')
[m, ind] = min(fval);
results = x(ind,:);
B_Y_final = B_Y_pairing + results(1,2);
B_X_final = B_X_pairing * results(1,3) + results(1,1);
@sum_squared_distances : sum of the distances to be minimized
function ssd = sum_squared_distances(inp)
global B_X_pairing....
R_X_pairing_short...
B_Y_pairing...
R_Y_pairing_short
B_X_pairing_slide_stretch = inp(1) + B_X_pairing*inp(3);
B_Y_pairing_slide = inp(2) + B_Y_pairing;
D_min = zeros(length(R_X_pairing_short),1);
for i=1:length(R_X_pairing_short)
D_min(i) = min(sqrt((B_X_pairing_slide_stretch - R_X_pairing_short(i)).^2 + (B_Y_pairing_slide - IR_Y_pairing_short(i)).^2));
end
ssd = sum(D_min);
end
At the end I have a very good overlap of the 2 maps and I can count the number of points with a pair among the blue points.

Now I should write a code to remove the red points that have the same blue point as a closest neigbor.
Another option would be to try to minimize the number of blue pixels.
3 Comments
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/490014-find-the-perfect-overlay-of-2-maps-of-points#comment_765178
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/490014-find-the-perfect-overlay-of-2-maps-of-points#comment_765178
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/490014-find-the-perfect-overlay-of-2-maps-of-points#comment_765184
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/490014-find-the-perfect-overlay-of-2-maps-of-points#comment_765184
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/490014-find-the-perfect-overlay-of-2-maps-of-points#comment_765194
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/490014-find-the-perfect-overlay-of-2-maps-of-points#comment_765194
Sign in to comment.