如何调整OpenCV代码提取更多视频帧并修正帧90°逆时针倾斜问题
Hey there! Let's work through your two OpenCV video processing issues step by step: getting more frames out of your 24-second video and fixing that annoying 90° counterclockwise tilt in the extracted frames.
Your original code reads frames at the video's native frame rate, which is why you're getting ~742 frames for a 24-second clip (that's roughly 31 FPS). To extract more frames than the video's native count, we can sample frames at a fixed time interval instead of relying on vidcap.read() to step through the video's built-in frames. This lets you control exactly how many frames you want (e.g., 60 FPS for 1440 total frames in 24 seconds).
The tilted frames are easy to fix with OpenCV's built-in rotation function. A 90° counterclockwise tilt just needs a 90° clockwise rotation to revert to the correct orientation. OpenCV's cv2.rotate() is optimized for these 90/180/270° rotations, so it's fast and clean.
Modified Full Code
Here's the combined solution that addresses both problems:
import cv2 # Initialize video capture vidcap = cv2.VideoCapture('20180530_115209.mp4') # Get basic video info to understand the source native_fps = vidcap.get(cv2.CAP_PROP_FPS) total_native_frames = vidcap.get(cv2.CAP_PROP_FRAME_COUNT) video_duration = total_native_frames / native_fps print(f"Original Video Details: FPS={native_fps:.2f}, Total Frames={int(total_native_frames)}, Duration={video_duration:.2f}s") # Set your desired frame extraction rate (higher = more frames) target_extraction_fps = 60 # Calculate time between each frame in milliseconds frame_interval_ms = 1000 / target_extraction_fps count = 0 current_time_ms = 0 # Loop through the video by time intervals while current_time_ms <= video_duration * 1000: # Jump to the exact time point we want to extract vidcap.set(cv2.CAP_PROP_POS_MSEC, current_time_ms) success, frame = vidcap.read() if not success: break # Exit loop if we can't read a frame (end of video) # Correct the 90° counterclockwise tilt with a clockwise rotation corrected_frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) # Save the corrected frame cv2.imwrite("sushant_2/image/frame%d.jpg" % count, corrected_frame) print(f"Extracted frame: {count}") count += 1 current_time_ms += frame_interval_ms # Clean up resources vidcap.release() print(f"Done! Total frames extracted: {count}")
Key Explanations
Frame Extraction Logic
- Instead of reading frames sequentially, we use
cv2.CAP_PROP_POS_MSECto jump to specific time points in the video. This lets us sample frames more densely than the video's native FPS. - Adjust
target_extraction_fpsto control how many frames you get: higher values mean more frames (e.g., 120 FPS would give 2880 frames for your 24-second video).
Tilt Correction
cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)directly fixes the 90° counterclockwise tilt. If you prefer, you could also use a 270° counterclockwise rotation (it's the same result), but the clockwise option is more intuitive here.- For reference, if you ever need to use manual rotation matrices (for arbitrary angles), you could do this, but it's overkill for 90° steps:
height, width = frame.shape[:2] rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), 270, 1) corrected_frame = cv2.warpAffine(frame, rotation_matrix, (height, width))
内容的提问来源于stack exchange,提问作者Harshit Mishra

