【问题标题】:Detect if a object is big enough open cv检测对象是否足够大opencv
【发布时间】:2020-12-05 17:31:18
【问题描述】:

我在这里需要一些帮助 :) 我有这段带有开放简历的代码:

import cv2
import numpy as np
import time

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()

    low_red = np.array([100, 100, 100])
    high_red = np.array([225, 225, 225])
    mask = cv2.inRange(frame, low_red, high_red)

    hasColor = np.sum(mask)
    if hasColor > 1000000:
        print(f'Hand opened, infos : {hasColor}')
    elif hasColor > 500000 and hasColor < 1000000:
        print(f'Hand closed, infos : {hasColor}')


    cv2.imshow("Camera", frame)
    cv2.imshow("Mask", mask)

    if cv2.waitKey(1) & 0xFF == ord('e'):
        break

而且我希望它只检测一定大小的物体(如手)而不是较小的物体。 感谢您的帮助:)

编辑:取得了一些不错的进展,但仍然不知道如何获得大小

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()

    low = np.array([100, 100, 100])
    high = np.array([225, 225, 225])
    mask = cv2.inRange(frame, low, high)

    cv2.imshow("Camera", frame)

    gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    ret, tresh = cv2.threshold(gray, 127, 255, 0)
    contours, hierarchy = cv2.findContours(tresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    for i in range(len(contours)):
        x,y,w,h = cv2.boundingRect(contours[i])
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)

    cv2.imshow("Mask", mask)
    cv2.imshow("Hull", frame)


    if cv2.waitKey(1) & 0xFF == ord('e'):
        break

所以我想我必须得到边界框的值,但不知道如何

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 什么不起作用,在哪里?您可以获取轮廓并测试轮廓的面积或周长。
  • 所以我编辑了帖子,我正在搜索以获取矩形大小的值
  • cv2.boundingRect() 返回的这 4 个值是什么?这些看起来很有希望。
  • OH THX 所以你我只需要检查 w 和 h 是否大于 150,例如

标签: python opencv detection


【解决方案1】:

多亏了好心人,问题得以解决, 我只需要在 for 循环中添加以下代码:

if h > 150 and w > 150:
    cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)

我只需要检查尺寸,感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多