Как узнать, находится ли ограничивающий прямоугольник (прямоугольник) внутри другого ограничивающего прямоугольника (прямоугольник)? - PullRequest
0 голосов
/ 31 марта 2020

Я пытаюсь получить коробки (координаты) внутри внешней коробки. Я использовал метод пересечения через объединение и хочу, чтобы это делали другие методы. enter image description here

Кроме того, не могли бы вы рассказать, как сравнить эти два внутренних блока?

Ответы [ 2 ]

1 голос
/ 31 марта 2020

Сравнивая координаты верхнего левого и нижнего правого углов ограничительной рамки и внутренних рамок, легко узнать, находится ли какая-либо из последних внутри первой.

Следующий код простой пример только с одной ограничительной рамкой и одной внутренней рамкой:

# Bounding box
boundb = {
    'x': 150,
    'y': 150,
    'height': 50,
    'width': 100
}

# Inner box
innerb = {
    'x': 160,
    'y': 160,
    'height': 25,
    'width': 25
}

# If top-left inner box corner is inside the bounding box
if boundb['x'] < innerb['x'] and boundb['y'] < innerb['y']:
    # If bottom-right inner box corner is inside the bounding box
    if innerb['x'] + innerb['width'] < boundb['x'] + boundb['width'] \
            and innerb['y'] + innerb['height'] < boundb['y'] + boundb['height']:
        print('The entire box is inside the bounding box.')
    else:
        print('Some part of the box is outside the bounding box.')

1 голос
/ 31 марта 2020

Вы можете использовать иерархию контуров .

На размещенном вами изображении внутренние прямоугольники - это контуры, в которых есть родитель и потомок (при использовании findContours с флагом RETR_TREE).

Вот пример кода:

import cv2

# Read input image
img = cv2.imread("boxes.png")

# Convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply threshold
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Find contours and hierarchy
cnts, hier = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[-2:]  # Use [-2:] for OpenCV 3, 4 compatibility.

# Draw all contours with green color for testing
cv2.drawContours(img, cnts, -1, (0, 255, 0))

# Hierarchy Representation in OpenCV
# So each contour has its own information regarding what hierarchy it is, 
# who is its child, who is its parent etc. 
# OpenCV represents it as an array of four values : [Next, Previous, First_Child, Parent]

# Iterate contours and hierarchy:
for c, h in zip(cnts, hier[0]):
    # Check if contour has one partent and one at least on child:
    if (h[3] > 0) and (h[2] > 0):
        # Get bounding rectange
        x, y, w, h = cv2.boundingRect(c)

        # Draw red rectange for testing
        cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), thickness=1)


# Show result for testing
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Результат:
enter image description here


Обновление:

У меня ошибка в приведенном выше коде (используется > 0 вместо >= 0).
Нам также необходимо убедиться, что родитель имеет родителя.
Похоже, это немного перегиб ...

# Iterate contours and hierarchy:
for c, h in zip(cnts, hier[0]):
    # Check if contour has one partent and one at least on child:
    if (h[3] >= 0) and (h[2] >= 0):
        # Get the partent from the hierarchy
        hp = hier[0][h[3]]

        # Check if the parent has a parent:
        if hp[3] >= 0:
            # Get bounding rectange
            x, y, w, h = cv2.boundingRect(c)

            # Draw red rectange for testing
            cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), thickness=1)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...