Intersecting and non-intersecting box regions

Having a set of bounding box values [x y width height] , how can i find the number of bounding box that gets intersected and that do not gets intersected when plotted
From the above example, there are 5 intersecting boxes and 2 non-intersecting boxes
How can i do so with the attached bounding box values

 採用された回答

Matt J
Matt J 2021 年 7 月 10 日
編集済み: Matt J 2021 年 7 月 10 日

1 投票

Using rectint(), you can straightforwardly obtain a binary mask A such that A(i,j)=1 if rectangle bbx(i,:) and bbx(j,:) intersect.
load bbx
A=rectint(bbx,bbx)>0
A = 20×20 logical array
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
and therefore the number of rectangles that have an intersection another rectangle would be,
N=sum(tril(A,-1),'all')

6 件のコメント

Elysi Cochin
Elysi Cochin 2021 年 7 月 11 日
Sir, error in the last line
Error using sum
Invalid option. Option must be 'double', 'native', 'default', 'omitnan' or 'includenan'.
Error in demo
N = sum(tril(A,-1),'all')
Matt J
Matt J 2021 年 7 月 11 日
Youhave an oler version of Matlab. You can instead do
N = sum(sum(tril(A,-1)))
Elysi Cochin
Elysi Cochin 2021 年 7 月 12 日
編集済み: Elysi Cochin 2021 年 7 月 12 日
@Matt JSir, one doubt, can i compute non-intersecting boxes from the same code?
Matt J
Matt J 2021 年 7 月 12 日
Yes, absolutely. A(i,j)=0 for boxes i and j that don't intersect.
Elysi Cochin
Elysi Cochin 2021 年 7 月 12 日
編集済み: Elysi Cochin 2021 年 7 月 12 日
load bbx
A = rectint(bbx,bbx)>0
Sir there are total 20 boxes
and non-intersecting box = 13, but there are more zeros, in the matrix A
So Sir, if I use A(i,j), how will i get 13?
Matt J
Matt J 2021 年 7 月 12 日
>> nnz(sum(A,1)==1)
ans =
13

サインインしてコメントする。

その他の回答 (2 件)

Simon Chan
Simon Chan 2021 年 7 月 10 日

1 投票

If viusal inspection is allowed, then the number can be counted by plotting the boxes in a figure:
rawdata=load('bbx.mat');
figure
for k=1:length(rawdata.bbx)
rectangle('Position',rawdata.bbx(k,:))
end

4 件のコメント

Elysi Cochin
Elysi Cochin 2021 年 7 月 10 日
data is large, so counting will be difficult
i have attached a small data
Simon Chan
Simon Chan 2021 年 7 月 10 日
The following method may only appropriate for the case where multiples of x and y are both integers.
In this case, the x and y coordinates are in 0.5 format and hence I multiply by 2.
Assign a matrix B which is large enough to cover all the boxes.
Add 1 to the region of each box and the overlapping boxes will have value inside its box > 1, which indicates intersecting boxes.
data=2*rawdata.bbx; % Make all coordinates to integers
B = zeros(1000,1000); % Matrix to accumulate all boxes
for k=1:length(rawdata.bbx)
A=zeros(1000,1000);
A(data(k,2):data(k,2)+data(k,4),data(k,1):data(k,1)+data(k,3))=1;
B = B+A;
end
% Calculating all the values inside each box after accumulation
for k =1:length(rawdata.bbx)
finalarea(k,1)=sum(sum(B(data(k,2):data(k,2)+data(k,4),data(k,1):data(k,1)+data(k,3))));
end
area=(data(:,3)+1).*(data(:,4)+1); % Original area of each box
Intersect_Number = sum(finalarea-area>0)
Elysi Cochin
Elysi Cochin 2021 年 7 月 10 日
when i increase the number of rows in bbx, i get error
Matrix dimensions must agree.
Error in demo
B = B+A;
Simon Chan
Simon Chan 2021 年 7 月 10 日
Would you please run the following commands before the above script:
clear;
clc;
If the error happens again, would you please run the following:
size(A) % Check the dimension of Matrix A
size(B) % Check the dimension of Matrix B

サインインしてコメントする。

KSSV
KSSV 2021 年 7 月 10 日

0 投票

Run a loop for each box and find the intersection points. Use this to get the intersection points.
If your output is empty, it means there is no intersection.

3 件のコメント

Elysi Cochin
Elysi Cochin 2021 年 7 月 10 日
So in my example, what will be the x1,y1,x2,y2 values?
P = InterX([x1;y1],[x2;y2]);
So if bbx = [ x y width height]
x1 = bbx(:,1)
y1 = bbx(:,2)
Is x1, y1 correct? what about x2, y2?
KSSV
KSSV 2021 年 7 月 10 日
You have origin, length and width of bounding box. So you can get four vertices of the box. You need to use four coordinates for each bounding box to get the intersection points.
Elysi Cochin
Elysi Cochin 2021 年 7 月 10 日
i didnt get that Sir. Please can you show me an example?

サインインしてコメントする。

タグ

質問済み:

2021 年 7 月 10 日

コメント済み:

2021 年 7 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by