简体   繁体   English

如何检测对象球(使用opencv进行跟踪)是否通过给定区域

[英]How can I detect if a object ball(being tracked using opencv) passed through a given area

I am trying to develop a foosball game analysis app. 我正在尝试开发桌上足球游戏分析应用程序。 I am able to track the ball using opencv color based tracking. 我可以使用基于opencv颜色的跟踪来跟踪球。 the real problem is goal detection. 真正的问题是目标检测。 possible solution is- if a ball passed through a marked area on the goal, and the ball vanishes from the table for a significant time, it can be considered as a goal and the goal counter should be incremented. 可能的解决方法是-如果球经过球门上的标记区域,并且球在桌子上消失了很长时间,则可以将其视为球门,并应增加球门计数器。

I went through a lot of code on github and stackoverflow, but can't correctly implement the needs. 我在github和stackoverflow上经历了很多代码,但是无法正确实现需求。

from collections import deque
from imutils.video import VideoStream
import numpy as np
import argparse
import cv2
import imutils
import time

ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
    help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=64,
    help="max buffer size")
args = vars(ap.parse_args())
match_score = (0, 0)

greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)

pts = deque(maxlen=args["buffer"])
counter = 0
(dX, dY) = (0, 0)
direction = ""

if not args.get("video", False):
    vs = VideoStream(src=1).start()
else:
    vs = cv2.VideoCapture(args["video"])

time.sleep(2.0)

while True:

frame = vs.read()


frame = frame[1] if args.get("video", False) else frame
if frame is None:
    break

frame = imutils.resize(frame, width=640, height=480)
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)

mask = cv2.inRange(hsv, greenLower, greenUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
center = None
cv2.rectangle(frame,(0,0),(640,360),(0,255,255),7)
cv2.line(frame,(10,135),(10,225),(255,0,0),7)
cv2.line(frame,(625,140),(625,230),(255,0,0),7)
cv2.putText(frame, str(match_score[0]) + " - " + str(match_score[1]), (260, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (120, 255, 50), 2)

if len(cnts) > 0:
    c = max(cnts, key=cv2.contourArea)
    ((x, y), radius) = cv2.minEnclosingCircle(c)
    M = cv2.moments(c)
    center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

    if radius > 5:
        # draw the circle and centroid on the frame,
        # then update the list of tracked points
        cv2.circle(frame, (int(x), int(y)), int(radius),
            (0, 255, 255), 2)
        cv2.circle(frame, center, 5, (0, 0, 255), -1)


pts.appendleft(center)
for i in range(1, len(pts)):
    if pts[i - 1] is None or pts[i] is None:
        continue
    if counter >= 10 and i == 1 and pts[-10] is not None:
        dX = pts[-10][0] - pts[i][0]
        dY = pts[-10][1] - pts[i][1]
        (dirX, dirY) = ("", "")

    thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5)
    cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness)
cv2.putText(frame, direction, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
    0.65, (0, 0, 255), 3)
cv2.putText(frame, "dx: {}, dy: {}".format(dX, dY),
    (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX,
    0.5, (0, 0, 255), 1)

cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
counter += 1
if key == ord("q"):
    break

if not args.get("video", False):
    vs.stop()
else:
    vs.release()

cv2.destroyAllWindows()

these are the clips I am currently using. 这些是我当前正在使用的剪辑。 https://drive.google.com/file/d/1Y7Dzzfx_V0fsDZRZAC4d0MuEpSnKnodI/view?usp=sharing https://drive.google.com/file/d/1jazU_-tdp9e-nALTqT04_U_8gj8rovSL/view?usp=sharing https://drive.google.com/file/d/1Y7Dzzfx_V0fsDZRZAC4d0MuEpSnKnodI/view?usp=sharing https://drive.google.com/file/d/1jazU_-tdp9e-nALTqT04_U_8gj8rovSL/view?usp=sharing

python trackball.py --video test3.mp4 python trackball.py --video test3.mp4

I am completely open to new suggestions! 我完全愿意接受新的建议!

Use a voting algorithm to determine if a goal has been scored. 使用投票算法来确定是否已进球。 Find the location of the goal in the image. 在图像中找到目标的位置。 If the tracked position of the ball is inside the goal, increment votes. 如果跟踪的球位置在球门内,请增加投票数。 If there are several votes for a goal, count the goal. 如果一个目标有几票,则计算该目标。

This voting method will help eliminate erroneous tracking values. 这种投票方法将有助于消除错误的跟踪值。

计数目标的投票方法

# Find the ball position (an x,y coordinate)
x, y = ball_position
# Find the goal position (a rectangle)
goal_x, goal_y, width, height = 0, 400, 50, 170
# Is the ball inside the goal?
if x>goal_x and p<goal_x+width and y>goal_y and y<goal_y+height: votes += 1
else: votes -= .1
if votes < 0: votes = 0
if votes > 7: print('goal!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在OpenCV,Python中为要跟踪的对象绘制路径行进线 - How do I Draw a Path Travelled Line in OpenCV, Python for an Object Being Tracked 如何使用OpenCV检测图像帧中的对象? - How can I detect an object in image frame using OpenCV? OpenCV和Python:对象跟踪并将被跟踪的对象保存为图像 - OpenCV and Python: object tracking and saving the object being tracked as an image 如何使用 OpenCV 在照片中找到选定区域? - How can i find selected area in photo using OpenCV? 如何使用 numpy/openCV 检测超声图片中的图像区域? - How do I detect the image area in an ultrasound picture using numpy/openCV? 我正在尝试在跟踪的对象上绘制3D立方体(使用ORB和对象的单应性)-OpenCV Python - I'm trying to draw a 3D cube on a tracked object (using ORB and the object's homography) - opencv Python 如何使用 OpenCV 检测具有相同颜色的像素区域 - How to detect area of pixels with the same color using OpenCV 如何使用 opencv 和 dlib 检测前额区域? - How can i detect the forehead region using opencv and dlib? 如何使用OpenCV检测和跟踪人员? - How can I detect and track people using OpenCV? 如何使用OpenCV(python)检测此图像中的岩石? - How can I detect rocks in this image using OpenCV (python)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM