我想將我的圖像上的代碼轉換爲視頻。我的程序獲取一幅畫面,作品出來的平均RGB各9 * 9窗口,並輸出圖像:沒有視頻輸出OpenCV蟒蛇
輸入圖像:
輸出圖像:
這裏是我的代碼具有圖像輸入/輸出:
import numpy as np
import cv2
#Read in image
img = cv2.imread('images/0021.jpg')
scale = 9
#Get x and y components of image
y_len,x_len,_ = img.shape
mean_values = []
for y in range(scale):
for x in range(scale):
#Crop image 3*3 windows
cropped_img=img[(y*y_len)/scale:((y+1)*y_len)/scale,
(x*x_len)/scale:((x+1)*x_len)/scale]
mean_val=cv2.mean(cropped_img)
mean_val=mean_val[:3]
cropped_img[:,:,:] = mean_val
print img.shape
cv2.imshow('mean_RGB',img)
cv2.waitKey(0)
當試圖使用上的視頻I得到的視頻輸出相同的代碼,但它爲空(0字節)。
下面是代碼:
import numpy as np
import cv2
cap = cv2.VideoCapture('videos/kondo2.avi')
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter('videos/output.avi',fourcc, 15.0, (800,600),True)
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
y_len,x_len,_ = frame.shape
scale = 9
for y in range(scale):
for x in range(scale):
cropped_frame=frame[(y*y_len)/scale:((y+1)*y_len)/scale,
(x*x_len)/scale:((x+1)*x_len)/scale]
mean_val=cv2.mean(cropped_frame)
mean_val=mean_val[:3]
cropped_frame[:,:,:] = mean_val
out.write(frame)
cap.release()
out.release()
cv2.destroyAllWindows()
謝謝您的閱讀:)
嘗試帶有雙反斜線(\\)路徑名,還以絕對路徑名儘量保證是沒有問題的。 – GPPK 2015-03-03 09:25:43
@GPPK路徑不應該是問題,因爲'output.avi'視頻被保存到正確的位置,它只是空的沒有幀。 – 2015-03-03 09:30:03
夠公平的。我總是發現最好從靜態路徑名開始,以減少錯誤的可能性。這是問題的時間。 – GPPK 2015-03-03 09:33:45