python - 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()
解决方案
您正在使用
y
而不初始化它。您使用
cv2.CAP_PROP_FRAME_WIDTH
的好像它是宽度,但事实并非如此。告诉OpenCV函数返回什么只是一个“定义” 。当您
frame
应该使用高度作为第一个索引时,您首先按宽度进行索引。
推荐阅读
- html - html的mailto url有什么限制
- c# - 如何在 EF6 原始 sql 函数中传递标量参数?
- php - 回声中的 PHP $_POST 函数 x()
- c# - 使用 modelbuilder.HasKey() 会导致实体框架新迁移出错
- netsuite - NetSuite SCA - 对页脚所做的编辑未显示
- django - Django Rest Framework中更新方法的区别?
- python-3.x - 忽略命令中的异常无:discord.ext.commands.errors.CommandNotFound:找不到命令“makePoll”
- r - 如何读取同一文件夹中多个 .txt 文件的第一行并写入 R 中的另一个 .txt 文件?
- netbeans - Netbeans JavaDB 无法启动
- azure - Azure devops Octopus 部署版本名称 semver 检查