我從我的pic目錄中讀取所有圖片,然后將它們轉換為帶有邊緣檢測的灰度,然后再將它們canny全部寫入視頻。
但是,當我使用我的視頻軟體播放它時,它顯示為綠色背景,我無法從中讀取視頻幀。有人可以告訴我如何解決嗎?
示例代碼
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
fourcc = cv.VideoWriter_fourcc(*"I420")
out = cv.VideoWriter("t2.avi", fourcc, 1, (640, 480), 0)
for pic in glob.glob1("./pic/", "A*"):
img = cv.imread(f"./pic/{pic}", -1)
edge = cv.Canny(img, 100, 200)
edge = cv.resize(edge, (640, 480))
out.write(edge)
out.release()
# Cant read video frame here:
cap = cv.VideoCapture("t2.avi")
ret, frame = cap.read()
if ret:
plt.imshow(frame)
else:
print("end")
cap.release()
視頻播放綠色背景

uj5u.com熱心網友回復:
它看起來像I420FOURCC 和灰度格式之間的兼容性問題。
替換fourcc = cv.VideoWriter_fourcc(*"I420")為:
fourcc = cv.VideoWriter_fourcc(*"GREY")
筆記:
- 我在 Windows 10 中使用 OpenCV 4.5.5,它與
"GREY".
我不確定它是否適用于所有平臺和版本。
I420應用彩色視頻。
您可以使用I420彩色視頻:
替換out = cv.VideoWriter("t2.avi", fourcc, 1, (640, 480), 0)為:
out = cv.VideoWriter("t2.avi", fourcc, 1, (640, 480), 1)
edge寫入前轉換為BGR:
edge = cv.cvtColor(edge, cv.COLOR_GRAY2BGR)
out.write(edge)
"GREY"使用FOURCC的代碼示例:
import numpy as np
import cv2 as cv
#import matplotlib.pyplot as plt
import glob
#fourcc = cv.VideoWriter_fourcc(*"I420")
fourcc = cv.VideoWriter_fourcc(*"GREY")
out = cv.VideoWriter("t2.avi", fourcc, 1, (640, 480), 0)
for pic in glob.glob1("./pic/", "A*"):
img = cv.imread(f"./pic/{pic}", -1)
edge = cv.Canny(img, 100, 200)
edge = cv.resize(edge, (640, 480))
out.write(edge)
out.release()
# Cant read video frame here:
cap = cv.VideoCapture("t2.avi")
while True:
ret, frame = cap.read()
if ret:
#plt.imshow(frame)
cv.imshow('frame', frame)
cv.waitKey(1000)
else:
print("end")
cap.release()
break
cv.destroyAllWindows()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471823.html
上一篇:不同模板大小的模板匹配
