You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu下Python3-OpenCV3视频转灰度保存报错(-215)求助

Fixing OpenCV Grayscale Video Saving Error on Ubuntu

Hey there! Let's break down why you're hitting that -215 error and how to fix it—this is a common gotcha with OpenCV on Linux systems.

First, Why isColor Isn't Working

You're spot-on about the isColor flag in cv2.VideoCapture: it's Windows-only. On Ubuntu (and other Linux distros), this parameter gets completely ignored, so your code is still reading full-color BGR frames (3 channels) even if you set isColor=False. That's the first piece of the puzzle.

The Root Cause of the -215 Error

The error img.cols == width && img.rows == height*3 happens because:

  • You probably created your cv2.VideoWriter expecting a 3-channel color video (the default behavior if you don't specify isColor=False).
  • But you're trying to write 1-channel grayscale frames to it. The dimensions don't match—color frames are height x width x 3, while grayscale is height x width x 1—so OpenCV throws this mismatch error.

Step-by-Step Fix

Here's a complete, working code example that addresses both issues, with clear explanations:

import cv2

# Open the input video file (use 0 instead of the filename for webcam)
cap = cv2.VideoCapture("input_color_video.mp4")

# Grab critical video metadata: width, height, and FPS
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)

# Define a Linux-friendly codec (XVID, MJPG, or MP4V all work well)
fourcc = cv2.VideoWriter_fourcc(*"XVID")

# Create the VideoWriter with isColor=False (this is the key fix!)
# This tells OpenCV we're writing single-channel grayscale frames
out = cv2.VideoWriter(
    "output_gray_video.avi",
    fourcc,
    fps,
    (frame_width, frame_height),
    isColor=False  # Don't skip this line—this resolves the dimension mismatch
)

# Process each frame in the video
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break  # Exit loop when we reach the end of the video
    
    # Manually convert color frame to grayscale (since isColor doesn't work on Linux)
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    # Write the grayscale frame to the output video
    out.write(gray_frame)
    
    # Optional: Show the grayscale frame for real-time debugging
    cv2.imshow("Grayscale Preview", gray_frame)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break  # Quit early if you press 'q'

# Clean up resources (always do this to avoid memory leaks!)
cap.release()
out.release()
cv2.destroyAllWindows()

Key Tips to Avoid Future Issues

  1. Ignore VideoCapture's isColor: On Linux, this parameter does nothing. Always read color frames and manually convert to grayscale with cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY).
  2. Set isColor=False in VideoWriter: This is non-negotiable for grayscale output. It explicitly tells OpenCV to expect 1-channel frames instead of 3-channel color.
  3. Codec Compatibility: If XVID doesn't work for your desired output format, try these alternatives:
    • For .avi files: MJPG
    • For .mp4 files: MP4V (update the output filename to output_gray_video.mp4 too)
  4. Debug Frame Shapes: Print frame.shape (should be (height, width, 3) for color) and gray_frame.shape (should be (height, width) for grayscale) to confirm the conversion worked as expected.

Quick Troubleshooting Checks

If you still run into errors:

  • Make sure your input video path is correct, and cap.isOpened() returns True.
  • Double-check that the frame_width and frame_height match between the input video and your VideoWriter setup.
  • Try a different codec—some codecs have limited support for grayscale on Linux.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:08:50