Steven Oh
Steven Oh

Reputation: 394

opencv not capturing all frames

I am trying to detect the position of the ball throughout a video. In the beginning, I crop the video because my original clip has some random things at the top. I don't know why but the program doesn't capture all the frames - it sort of stops after the first frame. I tried it on another computer and it works. I have no idea what's wrong.

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()

enter image description here

Upvotes: 1

Views: 831

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

  • You are using y without initialising it.

  • You are using cv2.CAP_PROP_FRAME_WIDTH as though it is the width but it isn't. It is just a "define" to tell an OpenCV function what to return.

  • You are indexing frame by width first, when you should use height as the first index.

Upvotes: 1

Related Questions