简体   繁体   English

在2D平面中找到数据集的外接矩形的长度和宽度(Matlab)

[英]find length and width of Circumscribed rectangle for a data set in 2d plane (Matlab)

有了一组二维数据(让我们知道平面中某些点的位置),如何在Matlab中找到这些点的外接矩形的长度和宽度?

To get the perimeter of the given points, you may proceed as below. 要获得给定点的周长,您可以按照以下步骤进行操作。 First get the boundary points using boundary . 首先使用boundary获取边界点。 Once you have boundary points, you can get the distances and sum them; 一旦有了边界点,就可以得到距离并求和; which gives you perimeter. 这给了你周长。

data = rand(10,2) ;
%% Get boundary of the points
idx = boundary(data(:,1),data(:,2)) ;
%%
figure
hold on
plot(data(:,1),data(:,2),'.r') ;
plot(data(idx,1),data(idx,2),'b') ;
%% perimeter
bound = [data(idx,1) data(idx,2)] ;
dx_boundary = diff(bound) ;
dist = sqrt(dx_boundary(:,1).^2+dx_boundary(:,2).^2);
perimeter = sum(dist) ;

You can get the bounding box using the maximum and minimum values of x and y coordinates. 您可以使用x和y坐标的最大值和最小值来获得边界框。 From these get the coordinates of the rectangle. 从这些获得矩形的坐标。

data = rand(10,2) ;
%% Get bounding box/ Rectangle
x0 = min(data(:,1)) ; x1 = max(data(:,1)) ;
y0 = min(data(:,2)) ; y1 = max(data(:,2)) ;
%% Legnth and breadth of rectangle
L = abs(x1-x0) ;
B = abs(y1-y0) ;
%% coordinates of rectangle 
Rect = [x0 y0 ; x0+L y0 ; x0+L y0+B ; x0 y0+B ; x0 y0] ;
%% figure
figure
hold on
plot(data(:,1),data(:,2),'.r')
hold on
plot(Rect(:,1),Rect(:,2),'b')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM