LINUX.ORG.RU

При поиске пересечений (Intersections), возникает ошибка: operands could not be broadcast together with shapes (384,) (73,)

 ,


0

1

У меня появляеться ошибка про поиске пересечений общих участков.

if np.logical_and(cnts, contours):

ValueError: operands could not be broadcast together with shapes (384,) (73,)


    for cont in contours:
        x1,y1,w1,h1 = cv2.boundingRect(cont)
        area_red = cv2.contourArea(cont)
        if (1000 > area_red >200):
            cv2.rectangle(frame, (x1,y1), (x1+w1, y1+h1), (255,0,0, 2))

            image1 = cv2.drawContours(blank.copy(), contours, 0, 1)

    for c in cnts:
        x,y,w,h = cv2.boundingRect(c)
        approx = cv2.approxPolyDP(c, 0.04 * cv2.arcLength(c, True), True)
        area = cv2.contourArea(c)
        if (1000 > area > 250):
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
            image2 = cv2.drawContours(blank.copy(), cnts, 1, 1)

    if np.logical_and(cnts, contours):
        print("Intersection idenifyed")

    cv2.imshow("Video from camera", frame)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

Заранее спасибо, всем участникам форума!

Заранее спасибо

Заранее пожалуйста.

ox55ff ★★★★★
()

А что этот код вообще должен делать?

Ошибка ValueError: operands could not be broadcast together with shapes (384,) (73,) возникает, потому что у вас cnts и contours имеют разные размеры.

Если нужно найти пересечения контуров, то logical_and для этого явно не подходит - он просто поэлементно выполняет операцию & для двух numpy массивов.

vvvvvvvv
()
img1 = cv2.drawContours(np.zeros_like(img), contours, 0, 1)
img2 = cv2.drawContours(np.zeros_like(img), contours, 1, 1)

intersection = np.logical_and(img1, img2)

if intersection.any():
    print("Intersection identified")

Вот так можно обнаружить пересечение двух контуров из списка contours. Здесь img - это ваше исходное изображение.

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

def has_intersection(img, cs1, cs2)
    for c1 in cs1:
        for c2 in cs2:
            img1 = cv2.drawContours(np.zeros_like(img), [c1], 0, 1)
            img2 = cv2.drawContours(np.zeros_like(img), [c2], 1, 1)
            intersection = np.logical_and(img1, img2)
            if intersection.any():
                return True
    return False

if has_intersection(img, contours, cnts):
    print("Intersection identified")

Как-то так.

vvvvvvvv
()

Ждем следующий тред, заранее спасибо.

moonmadness
()

пожалуйста, пиши еще

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.