在Matlab中获取平面上一组3D点的边界矩形的最佳方法

时间:2016-05-03 13:40:50

标签: matlab matrix

我需要将一组3D点的边界矩形的四个边缘存储为3xN矩阵(tt)。 N> = 4。这些点位于一个平面上。

代码示例:

% Simulate some points
deltaXY = 20;
[xx,yy] = meshgrid(-100:deltaXY:100,-100:deltaXY:100);
XYZ = [xx(:)'; yy(:)'; zeros(1,numel(xx))];

% Add some imperfection to data removing the top rigth point
maxXids = find(XYZ(1,:) == max(XYZ(1,:)));
maxYids = find(XYZ(2,:) == max(XYZ(2,:)));
id = intersect(maxXids,maxYids);
XYZ = removerows(XYZ',id)';

% Lets rotate a bit
XYZ = roty(5)*rotx(7)*rotz(0)*XYZ;

% Plot points
figure;
grid on;
rotate3d on;
axis vis3d;
hold on;
plot3(XYZ(1,:),XYZ(2,:),XYZ(3,:),'.r');

% Find bounding rectangle
% ??? :(
%Currently I'm using this code:
tt = XYZ;
%Get the max and min indexes
minX = find(tt(1,:) == min(tt(1,:)));
minY = find(tt(2,:) == min(tt(2,:)));
maxX = find(tt(1,:) == max(tt(1,:)));
maxY = find(tt(2,:) == max(tt(2,:)));
%Intersect to find the common index
id1 = intersect(minX,minY);
id2 = intersect(maxX,minY);
id3 = intersect(maxX,maxY);
id4 = intersect(minX,maxY);
%Get the points
p1 = tt(:,id1(1));
p2 = tt(:,id2(1));
p3 = tt(:,id3(1));
p4 = tt(:,id4(1));

示例点图: Sample points plot

问题是交叉一些时间可以为null,例如:如果点不形成矩形。导致此错误:

  
    

指数超出矩阵维度。

  

2 个答案:

答案 0 :(得分:1)

第一个解决方案:使用逻辑索引来摆脱find来电

p1=tt(:,tt(1,:)==min(tt(1,:))&tt(2,:)==min(tt(2,:)));
p2=tt(:,tt(1,:)==max(tt(1,:))&tt(2,:)==min(tt(2,:)));
p3=tt(:,tt(1,:)==max(tt(1,:))&tt(2,:)==max(tt(2,:)));
p4=tt(:,tt(1,:)==min(tt(1,:))&tt(2,:)==max(tt(2,:)));

第二个解决方案:使用convhull获取角落:

k=convhull(tt(1,:),tt(2,:));
Corners=[tt(:,k(1:end-1))];

答案 1 :(得分:0)

好的找到了解决方案:

    ...
    processed file: C:\..\Anaconda\Lib\site-packages\__pycache__\readline.cpython-35.pyc
    processed file: C:\..\Anaconda\Lib\site-packages\__pycache__\simplegeneric.cpython-35.pyc
    processed file: C:\..\Anaconda\Lib\site-packages\__pycache__\six.cpython-35.pyc
    processed file: C:\..\Anaconda\Lib\site-packages\__pycache__\test_path.cpython-35.pyc
    processed file: C:\..\Anaconda\Lib\site-packages\__pycache__\test_pycosat.cpython-35.pyc
    ...

    Successfully processed 38589 files; Failed processing 0 files