【问题标题】:How do I divide a bounding box into sub boxes in Matlab如何在 Matlab 中将边界框划分为子框
【发布时间】:2021-07-07 12:04:54
【问题描述】:

我有一个绑定框,其中的点位于不同的区域和位置。 我想根据DP[x,y]到可能彼此靠近的box [ax ay bx by]的划分点将该框划分为子框。 以下是说明这一想法的示例:

我试图用手计算并制作它,但它们有数百个,我卡住了。是否有任何 Matlab 函数可以做到这一点? 我的问题是:一个bounding box如何根据具体的点划分为sub box? 我使用了here 描述的函数point2bbox,但在这种情况下它不是一个合适的解决方案,因为点不在盒子的角落里。

这是我想做的一个例子:

绑定box = [341 91 24 74] x = 341y = 91width = 24height = 74 DP1(349,49) , DP2 (360,70) 要计算的点是:a1(349,91)a2(350,91)a2(360,91)a3(361,91) 大框的角点为:A(341,91)B(365,91)C(341,17)D(365,17)

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

请看下图了解更多:

请问您有什么想法吗?感谢您的帮助。

【问题讨论】:

    标签: matlab image-processing computer-vision coordinates bounding-box


    【解决方案1】:

    这是我的问题的解决步骤,将大盒子分成三个盒子。也许它可以帮助另一个人。

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

    感谢您的所有帮助。

    如果需要改进,您也可以帮助我们。

    【讨论】:

      【解决方案2】:

      嗯,我认为另一个更清晰的选择是使用 mat2cell: https://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)


      现在,对于更一般的情况。假设你有一个矩阵origMat。我们在矩阵上有DP = L*2 array 个分割点,我们想根据这些分割点将原始矩阵水平和垂直分割成更小的盒子。在您的示例中,您还将矩阵从上到下水平拆分为 21、21 和 32 个点。该解决方案忽略了原始矩阵的左上角点位置,因为如果需要,很容易将所有点移动一些偏移量。

      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 维度中保持除法并忽略超出范围的部分,请删除所有超出范围的已排序元素。

      差异主要是概念性的 - 您是想仅将一些内部小边界框(在更大的图像内)划分为内部的点,还是也划分为外部的坐标?这取决于应用程序。

      【讨论】:

      • @N.white 通常在矩阵中你有(Y,X)。但是,是的,如果您的点是 (X,Y),您应该将这些线与宽度/高度一起编辑。
      猜你喜欢
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2023-03-18
      • 2015-12-27
      • 2015-12-09
      • 2013-01-05
      • 2013-10-25
      相关资源
      最近更新 更多