簡體   English   中英

VideoWriter.write中的Python OpenCV錯誤(-215)

[英]Python OpenCV Error(-215) in VideoWriter.write

我正在嘗試編寫python3-opencv3代碼以讀取彩色視頻並將其轉換為灰度並保存回去。 (嘗試練習以學習python和opencv)

當我在ubuntu中工作時,我發現cv2.VideoCapture isColor標志將不起作用(僅適用於Windows)


import numpy as np
import cv2
cap = cv2.VideoCapture('output.avi')
ret, frame = cap.read()
print('ret =', ret, 'W =', frame.shape[1], 'H =', frame.shape[0], 'channel =', frame.shape[2])
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
height, width = gray.shape[:2]
print(height,width)
cv2.imshow('frame',gray)
FPS= 20.0
FrameSize=(frame.shape[1], frame.shape[0]) 
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('Video_output.avi', fourcc, FPS, (width,height))

while(ret):
    ret, frame = cap.read()
    if cv2.waitKey(1) & 0xFF == ord('q') or ret == False:
        break
    print('ret =', ret, 'W =', frame.shape[1], 'H =', frame.shape[0], 'channel =', frame.shape[2])
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Save the video
    out.write(gray) 
    cv2.imshow('frame',gray)
cap.release()
out.release()
cv2.destroyAllWindows()

這是給我的錯誤:(-215)img.cols == width && img.rows == height * 3在函數寫入

我猜,問題出在幀大小和灰度圖像轉換上,但我無法弄清楚? 我曾嘗試與高度的widht不同組合,但沒有一個是能夠正確執行程序。

有人可以幫忙嗎?

根據評論中的要求:

Traceback (most recent call last):
  File "/home/akash/Coded/VideoCode/Test3", line 70, in <module>
    out.write(gray) 
cv2.error: /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/videoio/src/cap_mjpeg_encoder.cpp:834: error: (-215) img.cols == width && img.rows == height*3 in function write

OpenCV錯誤-215表示“斷言失敗”。 斷言本身已在消息中列出(短語“斷言失敗”本身也可能也應如此;您可以在OpenCV的錯誤跟蹤器中打開與此有關的故障單)。 (更新: 我對此更改的拉取請求已於06.03接受,並在opencv 3.4.2中發布。)

從斷言可以推斷出,它期望幀是3色通道。 要制作灰度視頻, 您需要將isColor=False傳遞給VideoWriter構造函數

out = cv2.VideoWriter('Video_output.avi', fourcc, FPS, (width,height), False)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM