簡體   English   中英

如何在 Matlab 中將邊界框划分為子框

[英]How do I divide a bounding box into sub boxes in Matlab

我有一個邊界框和位於不同區域和位置的點。 我想根據分界點DP[x,y]將那個盒子分成可能彼此靠近的box [ax ay bx by]的子盒子。 以下是說明該想法的示例:

在此處輸入圖像描述

在此處輸入圖像描述

我試着用手計算並制作它,但它們有數百個,我卡住了。 有沒有 Matlab function 可以做到這一點? 我的問題是:一個bounding box如何根據具體的點划分子box? 我使用了這里描述的 function point2bbox ,但在這種情況下它不是合適的解決方案,因為點不在盒子的角落。

這是我想做的一個例子:

bound box = [341 91 24 74] x = 341 , y = 91 , width = 24 , height = 74 DP1(349,49) , DP2 (360,70)要計算的點是: a1(349,91) , a2(350,91) , a2(360,91) , and a3(361,91)大盒子的角點是: A(341,91) , B(365,91) , C(341,17) , and D(365,17)

bbox1 = [341 91 8 74]bbox2 = [350 91 10 74]bbox3 =[361 91 4 74]

請參閱下圖以獲取更多信息:

在此處輸入圖像描述

你能提出任何想法嗎? 我感謝任何幫助。

以下是我將大盒子分成三個盒子的問題的解決步驟。 也許它可以幫助另一個人。

%% 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];

我感謝你的幫助。

如果需要改進,您也可以幫助我們。

好吧,我認為另一個更清晰的選擇是使用mat2cellhttps://www.mathworks.com/help/matlab/ref/mat2cell.html

假設您的矩陣origMat是一個 22x74 矩陣,您想要將其拆分為三個大小為 8x74、10x74 和 4x74 的矩陣 - 如您的示例所示。

subboxes = mat2cell(origMat, [8,10,4]); % This gives you 3x1 cell array containing your matrices 8x74, 10x74 and 4x74.

如果問題是如何獲得數字 8、10 和 4,那么在您的示例中,您只需使用DP1(1)-A(1); DP2(1)-DP1(1); B(1)-DP2(1) DP1(1)-A(1); DP2(1)-DP1(1); B(1)-DP2(1) DP1(1)-A(1); DP2(1)-DP1(1); B(1)-DP2(1)


現在,對於更一般的情況。 假設您有一個矩陣origMat 我們在矩陣上有DP = L*2 array ,我們想根據這些划分點將原始矩陣在水平和垂直方向上划分為更小的框。 在您的示例中,您還將矩陣從上到下水平拆分為 21、21 和 32 點。 此解決方案忽略了原始矩陣的左上角點 position,因為如果需要,很容易將所有點移動一些偏移量。

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);

x 或 y 中的重復項將導致某些 0xN 或 Nx0 矩陣。 刪除該方向上 0 或 S1/S2 處的重復排序條目或點,以避免出現不需要的情況。 如果您的分割點超出了原始矩陣的邊緣,則此代碼將不起作用。 如何處理超出邊緣的點主要取決於你想做什么:

  1. 如果你想完全刪除有問題的點,你應該檢查 DP 列表並刪除 x 或 y 超出范圍的行。
  2. 如果你想在ok維度保持除法而忽略越界部分,移除所有越界的排序元素。

差異主要是概念上的——你是想只根據內部的點還是外部的坐標來划分一些內部的小邊界框(在更大的圖像內)? 這取決於應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM