如何对轮廓区域进行排序后求和?

问题描述 投票:0回答:1

我想对排序后最大的五个轮廓的面积求和,如果不超过五个,则全部归总。

机器人遵循人们的颜色,但是有时候人们使用相同的颜色,我想使用该区域从其中选择一种。我将这条线用于两个轮廓,但是这种方法不好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

任何帮助或建议将不胜感激。

python-2.7 image-processing area opencv-contour
1个回答
0
投票

您可以使用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
© www.soinside.com 2019 - 2024. All rights reserved.