【问题标题】:How to sum areas of contours after sorted them?排序后如何对轮廓区域求和?
【发布时间】:2019-12-30 02:26:28
【问题描述】:

我想对排序后最大的五个轮廓的面积求和,如果小于五个,我想对所有轮廓求和。

机器人会根据颜色跟随人的基地,但有时人们的颜色相同,我想从他们中选择一个使用该区域。我将这条线用于两个轮廓,但是这种方法不好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】:

    您可以使用 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
    

    【讨论】:

      【解决方案2】:

      你可以只使用这一行:

      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
      

      【讨论】:

        猜你喜欢
        • 2012-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-29
        • 2018-10-10
        • 2021-09-14
        相关资源
        最近更新 更多