frame_corner_point.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import cv2
  2. # Function to get the four corner coordinates of a frame
  3. def get_corner_coordinates(frame):
  4. height, width = frame.shape[:2]
  5. top_left = (0, 0)
  6. top_right = (width, 0)
  7. bottom_left = (0, height)
  8. bottom_right = (width, height)
  9. return top_left, top_right, bottom_left, bottom_right
  10. # Path to the video file
  11. video_path = "path/to/your/video.mp4"
  12. # Open the video
  13. cap = cv2.VideoCapture(video_path)
  14. # Check if video opened successfully
  15. if not cap.isOpened():
  16. print("Error: Could not open video.")
  17. exit()
  18. # Read the video frame by frame
  19. while True:
  20. ret, frame = cap.read()
  21. # If frame is read correctly ret is True
  22. if not ret:
  23. print("Can't receive frame (stream end?). Exiting ...")
  24. break
  25. # Get corner coordinates
  26. corners = get_corner_coordinates(frame)
  27. print("Corner coordinates:", corners)
  28. # You can also display the frame with corner points
  29. # for corner in corners:
  30. # cv2.circle(frame, corner, 5, (0, 0, 255), -1)
  31. # cv2.imshow('Frame with Corners', frame)
  32. # Press 'q' to exit the loop
  33. if cv2.waitKey(1) == ord("q"):
  34. break
  35. # When everything done, release the video capture object
  36. cap.release()
  37. cv2.destroyAllWindows()