请问如何检测视频中的圆圈并从中获取坐标

发布于 2025-01-10 09:34:32 字数 446 浏览 0 评论 0原文

我有一个像下面这样的代码,可以从图片中检测球,但是我可以问一下如何将其转换为检测视频版本并获取每一帧的球坐标吗?

import cv2
import numpy as np
img = cv2.imread('maze1.jpg')#讀取圖片
img=cv2.resize(img, (0,0), fx=0.2,fy=0.2)
GrayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#灰度化
GrayImage= cv2.medianBlur(GrayImage,5)#中值模糊
ret,th1 = cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY
th2 = cv2.adaptiveThreshold(GrayImage,255,cv2.ADAPTIVE_THRESH_MEAN_C,  cv2.THRESH_BINARY,3,5)  

I have a code like this below which can detect the ball from the picture, but may I ask how could I transform it to detect the video version and get the ball's coordinate at each frame?

import cv2
import numpy as np
img = cv2.imread('maze1.jpg')#讀取圖片
img=cv2.resize(img, (0,0), fx=0.2,fy=0.2)
GrayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#灰度化
GrayImage= cv2.medianBlur(GrayImage,5)#中值模糊
ret,th1 = cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY
th2 = cv2.adaptiveThreshold(GrayImage,255,cv2.ADAPTIVE_THRESH_MEAN_C,  cv2.THRESH_BINARY,3,5)  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮了一弯 2025-01-17 09:34:32

openCV 通常处理视频的方式是读取每一帧,分析和/或修改该帧,然后显示它。

因此,上述方法可以应用于视频的每个单独帧,或者如果足以满足您的需求或者您的目标解决方案和平台存在性能问题,则可以应用于每个“第 n”帧。

如果您查看 openCv 文档中的一些示例,您将看到这种类型的循环 - 例如:

while True:
    //This example is reading a frame from a camera but
    //you cxan read from a file also
    ret, frame = capture.read()
    if frame is None:
        break
    
    //Do your work on the farme here - this example is 
    //applying a mask, in your case you would detect 
    //your object. You can also add a counter and only
    //do the work every 'nth' frame 
    fgMask = backSub.apply(frame)
    
    
    cv.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)
    cv.putText(frame, str(capture.get(cv.CAP_PROP_POS_FRAMES)), (15, 15),
               cv.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))
    
    //Now display the modified frame - in your case you 
    //might highlight the detected circle or you could
    //just display it unchnaged
    cv.imshow('Frame', frame)
    cv.imshow('FG Mask', fgMask)
    
    keyboard = cv.waitKey(30)
    if keyboard == 'q' or keyboard == 27:
        break

您可以在此处看到完整的掩码示例和其他示例:https://docs.opencv.org/3.4/d1/dc5/tutorial_background_subtraction.html

The way openCV usually works with video is to read each frame, analyse and/or modify the frame, and then display it.

So your approach above can be applied to each individual frame of the video, or to every 'nth' frame if that is good enough for your needs or you have performance issues with your target solution and platform.

If you look at some of the examples in the openCv docs you will see this type of loop - e.g.:

while True:
    //This example is reading a frame from a camera but
    //you cxan read from a file also
    ret, frame = capture.read()
    if frame is None:
        break
    
    //Do your work on the farme here - this example is 
    //applying a mask, in your case you would detect 
    //your object. You can also add a counter and only
    //do the work every 'nth' frame 
    fgMask = backSub.apply(frame)
    
    
    cv.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)
    cv.putText(frame, str(capture.get(cv.CAP_PROP_POS_FRAMES)), (15, 15),
               cv.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))
    
    //Now display the modified frame - in your case you 
    //might highlight the detected circle or you could
    //just display it unchnaged
    cv.imshow('Frame', frame)
    cv.imshow('FG Mask', fgMask)
    
    keyboard = cv.waitKey(30)
    if keyboard == 'q' or keyboard == 27:
        break

You can see the full masking example and other examples here: https://docs.opencv.org/3.4/d1/dc5/tutorial_background_subtraction.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文