You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用OpenCV的VideoCapture读取图像序列时遇imshow断言失败错误求助

Hey there, let's troubleshoot that frustrating OpenCV assertion error you're hitting when trying to read your image sequence (0.jpg to 1052.jpg). That size.width>0 && size.height>0 error almost always means the frame you're trying to display is empty—so OpenCV can't find or load the image properly. Here are the most common fixes to get this working smoothly:

1. Fix your VideoCapture path pattern

When using VideoCapture for numbered image sequences, you need to use a wildcard format that OpenCV understands. For sequential files like 0.jpg, 1.jpg..., the correct syntax uses %d as a placeholder for the number.

Here's a corrected code snippet:

import cv2

# Use the right pattern for your sequence (no leading zeros here)
cap = cv2.VideoCapture("%d.jpg")

while cap.isOpened():
    ret, frame = cap.read()
    # Always check if the frame was loaded successfully first!
    if not ret:
        print("Can't load frame—either the sequence ended or a file is missing. Exiting...")
        break
    cv2.imshow('Image Sequence', frame)
    # Press 'q' to quit early
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

If your filenames have leading zeros (like 0000.jpg), adjust the pattern to match the digit count—for example, %04d.jpg for 4-digit numbers.

2. Double-check your working directory

Make sure your Python script is running in the same folder as your images. If not, use an absolute path in the VideoCapture call. For example:

cap = cv2.VideoCapture("/Users/jeanne/Documents/image_sequence/%d.jpg")

To confirm your current working directory, run this quick check:

import os
print(os.getcwd())

If the images aren't in that folder, either move the script or update the path.

3. Check for missing or corrupted images

The error might pop up if one image in the sequence is missing (like skipping 500.jpg) or corrupted. Open a few random images (especially near the start and end of your sequence) in a viewer to confirm they load properly.

If there's a gap in numbering, cap.read() will return ret=False when it hits the missing file, leading to an empty frame. You'll need to fix the numbering or add code to handle gaps.

4. Add explicit frame validation

Even with the right path, it's good practice to catch empty frames before trying to show them. The check for ret in the code above does this—it stops the script from attempting to display a frame that didn't load, which is exactly what's causing that assertion error.

5. Fallback: Load images manually

If VideoCapture is being uncooperative, you can loop through filenames directly for more control:

import cv2
import os

image_dir = "./"  # Replace with your image folder path
for i in range(0, 1053):  # Covers 0 to 1052 inclusive (1053 total images)
    img_path = os.path.join(image_dir, f"{i}.jpg")
    frame = cv2.imread(img_path)
    if frame is None:
        print(f"Warning: Could not load {img_path}")
        continue
    cv2.imshow('Image Sequence', frame)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()

This makes it easy to spot exactly which file is causing issues if something goes wrong.

内容的提问来源于stack exchange,提问作者Jeanne

火山引擎 最新活动