我使用skimage为图像创建区域
from skimage.measure import regionprops
from skimage import measure
label_image = measure.label(binary_car_image)
for region in regionprops(label_image):
...
在某些情况下,这些区域可以彼此相交。如下面的图片所示:在此图像中有两个区域,但彼此相交。
在这种情况下,我需要将它们合并到一个区域而不是两个区域。
我发现这是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
计算其boundingRect
。 contours
存储在 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;
}
}
}
最后,您可以像这样获得“复合”边界框的最终width
和height
://width and height of the composite box:
int width = maxX - minX;
int height = maxY - minY;
最终边界框的数据应由minX
,minY
,width
和height
给出。
关于python - 如果它们相交,则合并两个区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63144091/