Reputation: 71
I have a bound box and points located inside it in different areas and positions.
I want to divide that box into sub boxes according to the dividing point DP[x,y]
into box [ax ay bx by]
that may locate close to each other.
Here are examples for illustrating the idea:
I tried to calculate and make it by my hand but they are hundreds, I stuck. Is there any Matlab function to do that? My question is: How to divide a bounding box into sub boxes according to the specific points? I used the function point2bbox described here, but it does not a suitable solution in this case because the points are not in the corners of the box.
Here is an example of what I want to do:
bound box = [341 91 24 74]
x = 341
, y = 91
, width = 24
, height = 74
DP1(349,49)
, DP2 (360,70)
points to calculated are: a1(349,91)
,a2(350,91)
,a2(360,91)
, and a3(361,91)
The corners points of big box are: A(341,91)
, B(365,91)
, C(341,17)
, and D(365,17)
bbox1 = [341 91 8 74]
, bbox2 = [350 91 10 74]
, and bbox3 =[361 91 4 74]
.
see the picture below for more clarifying:
Could you suggest any idea to do that, please? I appreciate any help.
Upvotes: 1
Views: 480
Reputation: 1390
Well, another in my opinion clearer option is using mat2cell
: https://www.mathworks.com/help/matlab/ref/mat2cell.html
Suppose your matrix origMat
is a 22x74 matrix, that you want to split it to three matrices sized 8x74, 10x74 and 4x74 - as in your example.
subboxes = mat2cell(origMat, [8,10,4]); % This gives you 3x1 cell array containing your matrices 8x74, 10x74 and 4x74.
If the question is how to get those numbers 8, 10 and 4, well, in your example, you would simply use DP1(1)-A(1); DP2(1)-DP1(1); B(1)-DP2(1)
.
Now, for a bit more general case. Suppose you have a matrix origMat
. We have DP = L*2 array
of division points on the matrix, and we want to divide original matrix to smaller boxes both horizontally and vertically according to those division points. In your example you would also split matrix horizontally to 21, 21 and 32 points from top to bottom. This solution ignores top left corner point position of the original matrix as it is easy to shift all points by some offset if required.
origMat = rand(74,22); %74 row 22 col matrix.
DP = [21,8; 42,18]; % 2 division points at row 21, col 8 and row 42, col 18.
[S1, S2] = size(origMat);
SDPy = sort(DP(:,1));
SDPx = sort(DP(:,2));
% widths are to first division point, then distances between division points, then from last division point to the edge of original matrix.
widths = [SDPx(1), SDPx(2:end)-SDPx(1:end-1), S2-SDPx(end)];
heights = [SDPy(1), SDPy(2:end)-SDPy(1:end-1), S1-SDPy(end)];
subboxes = mat2cell(origMat, heights, widths);
Duplicates in x or y will lead to some 0xN or Nx0 matrices. Remove duplicated sorted entries or points at 0 or S1/S2 in that direction to avoid that if it is undesirable. If you have division points beyond the edges of original matrix, this code won't work. How to handle points beyond the edges mainly depends what you want to do:
Differences are mainly conceptual - do you want to divide some internal small bounding box (inside a much larger image) only by points inside it, or also by coordinates outside? This depends on application.
Upvotes: 1
Reputation: 71
Here are the solution steps of my question to divide the Big box to three boxes. Maybe it could help another one.
%% Big box values in [ax,ay,bx,by]
bound = [341,91 ,365,17];
%% Two Divide Points are: [349,49,360,70]in form [x1,y1,x2,y2]
% store as struct
DP =struct('dividePoint1',{349,49},'dividePoint2',{360,70});
DP2 = [DP(1,1).dividePoint1;DP(1,1).dividePoint2];
DP1x = DP(1,1).dividePoint1;
DP1y = DP(1,2).dividePoint1;
DP2x = DP(1,1).dividePoint2;
DP2y = DP(1,2).dividePoint2;
%% Put the two points in one vector
DPmat = [DP1x DP1y;DP2x DP2y];
%% Sort Points
DPSorted = sort(DPmat,'ascend');
%% convert bound to 4 values [ax,ay,bx,by]
axBound =bound(1);ayBound = bound(2);bxBound = bound(3);byBound = bound(4);
%% Initial empty x-axis value
XsValues = {};
%% loop to divide two values to four values DP1,DP1+1,DP2,DP2+1
for k = 1:(numel(DP2))
boxes = [DPSorted(k,1),DPSorted(k,1)+1];
XsValues = [XsValues;boxes];
end
%% rearrang the points
% convert x-axis values to matrix
xBoxes = cell2mat(XsValues);
xValues = [xBoxes(1,:),xBoxes(2,:)];
subBox1 = [axBound,ayBound,xValues(1),byBound];
subBox2 = [xValues(2),ayBound,xValues(3),byBound];
subBox3 = [xBoxes(4),ayBound,bxBound,byBound];
%% all subBoxes in one matrix
AllBoxes = [subBox1;subBox2;subBox3];
I appreciate all of your help.
If it need improve you can help us also.
Upvotes: 2