【问题标题】:merge two region if they intersect如果两个区域相交,则合并它们
【发布时间】:2020-07-28 23:11:30
【问题描述】:

我使用 skimage 为图像创建区域

from skimage.measure import regionprops
from skimage import measure
label_image = measure.label(binary_car_image)
for region in regionprops(label_image):
  ...

在某些情况下,这些区域可以相互交叉。如下图所示:

在此图像中有两个区域,但它们彼此相交。 在这种情况下,我需要将它们合并到一个区域而不是两个区域。

我发现了这个link,两个检查它​​们是否相互交叉。 所以我需要将这两个区域(或者在某些情况下可能超过 2 个)合并到一个区域。

谢谢

【问题讨论】:

  • 获取每个矩形的边界框(角)坐标。然后从集合中获取最小和最大 x 以及最小和最大 y 坐标。用它来定义一个新的矩形,它是两者的边界框。

标签: python opencv computer-vision scikit-image


【解决方案1】:

正如 fmw42 建议的那样,做这样的事情(这是未经测试的 C++ 代码,但它应该让你知道该怎么做):

首先,设置一些初始值:

int maxX = 0; // width of the contour + minimum x
int minX = 5000; // minimum x (big value)
int minY = 5000; // height of the contour + min y (big value)
int maxY = 0; // min y

然后,遍历所有contours,并通过boundingRect 为每个contour 计算其bounding boxcontours 存储在向量 contours 中,在 C++ 中是 cv::Points 向量的向量。

//loop through all the contours:    
for( int i = 0; i < (int)contours.size(); i++ ){

    //process the outter most contours only:
    if ( hierarchy[i][3] == -1 ) {

        //get the simple bounding box:
        cv::Rect contourRect = cv::boundingRect( contours[i] );

        //get the coordinates of the bounding box for x:
        int countourMaximumX = contourRect.x + contourRect.width;
        if ( countourMaximumX > maxX ){
            maxX = countourMaximumX;
        }

        if ( contourRect.x < minX ){
            minX = contourRect.x;
        }

        //get the coordinates of the bounding box for y:
        int countourMaximumY = contourRect.y + contourRect.height;
        if ( countourMaximumY > maxY ){
            maxY = countourMaximumY;
        }

        if ( contourRect.y < minY ){
            minY = contourRect.y;
        }
    }

}

最后,您可以像这样获得“复合”边界框的最终widthheight

//width and height of the composite box:  
int width = maxX - minX;
int height = maxY - minY;

最终边界框的数据应该由minXminYwidthheight给出。

【讨论】:

    【解决方案2】:

    感谢 fmw42 的提议,我确实实现了 eldesgraciado 提出的 C++ 代码并且它的工作,这是我对 python 的实现,适用于在 python 中遇到相同问题的人:

    label_image = measure.label(binary_car_image)
    
    minY, minX, maxY, maxX = 5000, 5000, 0, 0
    
    for region in regionprops(label_image):
        if region.area < 50:
            continue
        min_row, min_col, max_row, max_col = region.bbox
        region_height = max_row - min_row
        region_width = max_col - min_col
        countourMaximumY = max_row + region_height
        if countourMaximumY > maxY:
            maxY = countourMaximumY
        countourMaximumX = max_col + region_width
        if countourMaximumX > maxX:
            maxX = countourMaximumX
        if min_row < minY:
            minY = min_row
        if min_col < minX:
            minX = min_col
    

    并使用

    min_row, min_col, max_row, max_col = minY, minX, maxY, maxX
    

    而不是

    min_row, min_col, max_row, max_col = region.bbox
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 2018-09-02
      • 2013-09-15
      相关资源
      最近更新 更多