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.VideoWriterexpecting a 3-channel color video (the default behavior if you don't specifyisColor=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 isheight 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
- Ignore
VideoCapture'sisColor: On Linux, this parameter does nothing. Always read color frames and manually convert to grayscale withcv2.cvtColor(frame, cv2.COLOR_BGR2GRAY). - Set
isColor=FalseinVideoWriter: This is non-negotiable for grayscale output. It explicitly tells OpenCV to expect 1-channel frames instead of 3-channel color. - Codec Compatibility: If
XVIDdoesn't work for your desired output format, try these alternatives:- For
.avifiles:MJPG - For
.mp4files:MP4V(update the output filename tooutput_gray_video.mp4too)
- For
- Debug Frame Shapes: Print
frame.shape(should be(height, width, 3)for color) andgray_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()returnsTrue. - Double-check that the
frame_widthandframe_heightmatch between the input video and yourVideoWritersetup. - Try a different codec—some codecs have limited support for grayscale on Linux.
内容的提问来源于stack exchange,提问作者AKASH NIGAM
相关产品推荐
相关产品推荐

