首页 > 解决方案 > opencv没有捕获所有帧

问题描述

我试图在整个视频中检测球的位置。一开始,我裁剪视频,因为我的原始剪辑顶部有一些随机的东西。我不知道为什么,但程序并没有捕捉到所有的帧——它在第一帧之后就停止了。我在另一台电脑上试了一下,效果很好。我不知道出了什么问题。

import cv2
import numpy as np

# Open the video
cap = cv2.VideoCapture('video-4.mp4')

while(1):
    success, frame = cap.read()
    # Take each frame
    if success:
        crop_img = frame[100:3000, 100:3000].copy()
        gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
        circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=1.5, minDist=505,param1=75, param2=30, minRadius=8, maxRadius=10)
        # ensure at least some circles were found
        if circles is not None:
            # convert the (x, y) coordinates and radius of the circles to integers
            circles = np.round(circles[0, :]).astype("int")
            # loop over the (x, y) coordinates and radius of the circles
            for (x, y, r) in circles:
                print(x,y,r)
                # draw the circle in the output image, then draw a rectangle
                # corresponding to the center of the circle
                cv2.circle(crop_img, (x, y), r, (0, 255, 0), 4)
                cv2.rectangle(crop_img, (x - 5, y - 5),
                            (x + 5, y + 5), (0, 128, 255), -1)
        cv2.namedWindow("hi", cv2.WINDOW_NORMAL)
        cv2.imshow('hi', crop_img)
        cv2.waitKey(0)
    else:
        break
    
cv2.destroyAllWindows()

在此处输入图像描述

标签: pythonopencv

解决方案


  • 您正在使用y而不初始化它。

  • 您使用cv2.CAP_PROP_FRAME_WIDTH的好像它是宽度,但事实并非如此。告诉OpenCV函数返回什么只是一个“定义” 。

  • 当您frame应该使用高度作为第一个索引时,您首先按宽度进行索引。


推荐阅读