Reorganize a table of points based on their coodinates

조회 수: 1 (최근 30일)
Quentin Govignon
Quentin Govignon 2019년 11월 13일
댓글: Quentin Govignon 2019년 11월 13일
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

채택된 답변

Turlough Hughes
Turlough Hughes 2019년 11월 13일
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)
  댓글 수: 1
Quentin Govignon
Quentin Govignon 2019년 11월 13일
thanks,
I made a few tweaks but it works,
changed the index name because I had already another index idx;
I had to transpose the input_points due to the formatting of my input points, and also add the 'omitnan' flag otherwise it took the nan lines as max in the sum
indx = zeros(1,4);
ptstemp=input_points';
[~,indx(1)]=min(sum(ptstemp));
ptstemp(:,indx(1))=[nan;nan];
[~,indx(2)]=min(ptstemp(1,:));
ptstemp(:,indx(2))=[nan;nan];
[~,indx(4)]=max(sum(ptstemp,'omitnan'));
ptstemp(:,indx(4))=[nan;nan];
indx(3)=setdiff(1:4,indx);
input_points=input_points(indx,:);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by