排序后如何对等高线区域求和?

How to sum areas of contours after sorted them?

我想对排序后最大的五个等高线的面积求和,或者如果小于五个则全部求和。

机器人根据颜色跟随人的基础,但有时人有相同的颜色,我想使用区域从他们中选择一个。我将这条线用于两个轮廓,但这种方法并不好area1 = cv2.contourArea(cnts[0]) + cv2.contourArea(cnts[1])

完整代码:

import cv2
import numpy as np
from imutils.video import FPS
import time
cap = cv2.VideoCapture(0)
width = cap.get(3)  # float
height = cap.get(4)  # float
print width, height
time.sleep(2.0)
fps = FPS().start()
while (1):
    _, img = cap.read()
    if _ is True:
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    else:
        continue
    black_lower = np.array([0,0,0], np.uint8)
    black_upper = np.array([180,255,30], np.uint8)
    black = cv2.inRange(hsv, black_lower, black_upper)
    kernal = np.ones((5, 5), "uint8")
    black = cv2.dilate(black, kernal)
    res_black = cv2.bitwise_and(img, img, mask=black)
    # Tracking black
    (_, contours, hierarchy) = cv2.findContours(black, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted(contours, key=cv2.contourArea, reverse=True)[:2000]  # get largest 2000 contour area
    area1 = cv2.contourArea(cnts[0]) + cv2.contourArea(cnts[1])
    # area2 = cv2.contourArea(cnts[0])
    # total = area1 +area2
    print 'area', area1,   type(cnts)
    rects = []
    print len(cnts) , type(cnts[1])
    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        x, y, w, h = cv2.boundingRect(approx)
        if h >= 15:
            rect = (x, y, w, h)
            rects.append(rect)
            img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 2)
            cv2.putText(img, "Black Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0))
    cv2.imshow("Color Tracking", img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break

如有任何帮助或建议,我们将不胜感激。

您可以使用 list = [] 对它们求和,但也许您面临另一个问题,即所有人的面积总和。

import cv2
import numpy as np
from imutils.video import FPS
import time
cap = cv2.VideoCapture(0)
width = cap.get(3)  # float
height = cap.get(4)  # float
print width, height
time.sleep(2.0)
fps = FPS().start()
while (1):
    _, img = cap.read()
    if _ is True:
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    else:
        continue
    black_lower = np.array([0,0,0], np.uint8)
    black_upper = np.array([180,255,30], np.uint8)
    black = cv2.inRange(hsv, black_lower, black_upper)
    kernal = np.ones((5, 5), "uint8")
    black = cv2.dilate(black, kernal)
    res_black = cv2.bitwise_and(img, img, mask=black)
    # Tracking black
    (_, contours, hierarchy) = cv2.findContours(black, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    cnts = sorted(contours, key=cv2.contourArea, reverse=True)[:5]  # get largest five contour area
    areas = []
    for contour in cnts:
         area = cv2.contourArea(contour)
         if area > 300:
              areas.append(area)
              x, y, w, h = cv2.boundingRect(contour)
              img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 2)
              cv2.putText(img, "Black Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0))

    a = sum(areas)

    print areas
    print a

    cv2.imshow("Color Tracking", img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
         cap.release()
         cv2.destroyAllWindows()
         break

你可以只使用这一行:

area = sum([cv2.contourArea(cnt) for cnt in sorted(cnts, key=cv2.contourArea, reverse=True)[:5]])

我会添加完整的代码来比较它们。

import cv2
import numpy as np
from imutils.video import FPS
import time
cap = cv2.VideoCapture(0)
width = cap.get(3)  # float
height = cap.get(4)  # float
print width, height
time.sleep(2.0)
fps = FPS().start()
while (1):
    _, img = cap.read()
    if _ is True:
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    else:
        continue
    black_lower = np.array([0,0,0], np.uint8)
    black_upper = np.array([180,255,30], np.uint8)
    black = cv2.inRange(hsv, black_lower, black_upper)
    kernal = np.ones((5, 5), "uint8")
    black = cv2.dilate(black, kernal)
    res_black = cv2.bitwise_and(img, img, mask=black)
    # Tracking black
    (_, contours, hierarchy) = cv2.findContours(black, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    cnts = sorted(contours, key=cv2.contourArea, reverse=True)[:5]  # get largest five contour area
    area = sum([cv2.contourArea(cnt) for cnt in sorted(cnts, key=cv2.contourArea, reverse=True)[:5]])
    print 'area_method1', area
    areas = []
    for contour in cnts:
         area = cv2.contourArea(contour)
         if area > 300:
              areas.append(area)
              x, y, w, h = cv2.boundingRect(contour)
              img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 2)
              cv2.putText(img, "Black Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0))

    a = sum(areas)

    # print areas
    print 'area_method2',  a

    cv2.imshow("Color Tracking", img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
         cap.release()
         cv2.destroyAllWindows()
         break