MATLAB: Reorganize a table of points based on their coodinates

image transformMATLABrearangingsorting points

Hi there,
I want to do a imwarp for an image based on the recognition of four markers in my image.
I can find the coordinates of those four points listed as input_points but depending on the angle of image acquisition my points are not always listed in the same order.
I define my base points for the imwrap as:
base_points = [0 0; 0 HEIGHT; WIDTH 0; WIDTH HEIGHT];
scale_calib = (pdist2(input_points(1,:), input_points(2,:)))/ HEIGHT; % %pixel/mm
base_points = base_points*scale_calib;
Tform = fitgeotrans(input_points,base_points,'projective');
I_full = imwarp(im1,Tform);
and do the imwarp
but my image points are not always ordered the same way,
I sometimes get it right sometimes get the first and second point inverted wich then completely mess my image correction.
How could I sort my input_points to ensure that the first one is always the one with a min (x+y), then second point the one with min(x) out of the three remaining points, the fourth point can be the one with max(x+y), and the third point is the last remaining one.
I know I could do it a dirty and not efficient way but I would like to find a more elegant (and perhaps lighter) way of doing it
Thanks for your help

Best Answer

I put down an index here for sorting the points as you requested. It should do the job.
ptstemp=input_points
[~,idx(1)]=min(sum(ptstemp));
ptstemp(:,idx(1))=[nan;nan];
[~,idx(2)]=min(ptstemp(1,:))
ptstemp(:,idx(2))=[nan;nan];
[~,idx(3)]=max(sum(ptstemp));
ptstemp(:,idx(3))=[nan;nan];
idx(4)=setdiff(1:4,idx);
input_points=input_points(:,idx)