gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:14:15 29 4
gpt4 key购买 nike

我使用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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com