python - 如果它们相交,则合并两个区域

标签 python opencv computer-vision scikit-image

我使用skimage为图像创建区域

from skimage.measure import regionprops
from skimage import measure
label_image = measure.label(binary_car_image)
for region in regionprops(label_image):
  ...
在某些情况下,这些区域可以彼此相交。如下面的图片所示:
enter image description here
在此图像中有两个区域,但彼此相交。
在这种情况下,我需要将它们合并到一个区域而不是两个区域。
我发现这是link,两个检查它​​们是否相交。
因此,我需要将这两个区域(在某些情况下可能会大于两个)合并为一个区域。
谢谢

最佳答案

就像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,并通过contour为每个bounding box计算其boundingRectcontours存储在 vector contours中,该 vector 在C++中是cv::Point s的 vector 的 vector 。
//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给出。

关于python - 如果它们相交,则合并两个区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63144091/

相关文章:

java - 如何在Android中使用Opencv在图像上有效应用Canny边缘检测器?

java - OpenCv 图像转换

python - 如何在两个数据框中查找匹配项

python - 计算字典中 get 方法返回第二个选项的次数

c++ - SVM 训练 C++ OpenCV

image-processing - 在opencv中检测叶状形状的最佳方法

python - 获取图像的焦点像素

opencv - 平衡拼接图像之间的对比度和亮度

python - wxpython显示类型列表

python - 如何使用 pytest-qt 鼠标单击选择 QTableWidget 中的项目?